C (programming language)

String in C Exercises & Solutions

String Manipulation in C: Exercises and Solutions

Strings are the fabric of information in C programming. Mastering their manipulation techniques is crucial for building strong programs. This guide dives into a selection of string exercises and their solutions, equipping you with the practical skills to navigate the world of string operations.

1. Basic String Processing

Exercise 1: Print String Length

Write a program to input a string and print its length.

Solution:

C

#include <stdio.h>
#include <string.h>

int main() {
  char str[100];
  int length;

  printf("Enter a string: ");
  fgets(str, 100, stdin); // read string from user

  // calculate length using strlen
  length = strlen(str);

  // remove newline character from length
  length--;

  printf("The string length is: %d\n", length);

  return 0;
}

Explanation:

  • We include stdio.h and string.h for input/output and string functions.
  • We declare a character array str to store the user-entered string and an integer length for its length.
  • fgets reads the string from user input, including the newline character.
  • strlen calculates the string length (excluding the null terminator).
  • We subtract 1 from length to account for the newline character.
  • Finally, we print the actual string length.

Exercise 2: Count Uppercase and Lowercase Characters

Write a program to count the number of uppercase and lowercase characters in a string.

Solution:

C

#include <stdio.h>
#include <ctype.h>

int main() {
  char str[100];
  int uppercaseCount = 0, lowercaseCount = 0;

  printf("Enter a string: ");
  fgets(str, 100, stdin);

  for (int i = 0; str[i] != '\0'; i++) {
    if (isupper(str[i])) {
      uppercaseCount++;
    } else if (islower(str[i])) {
      lowercaseCount++;
    }
  }

  printf("Uppercase characters: %d\n", uppercaseCount);
  printf("Lowercase characters: %d\n", lowercaseCount);

  return 0;
}

Explanation:

  • We include stdio.h and ctype.h for input/output and character classification functions.
  • We declare variables to store counts for uppercase and lowercase characters.
  • We iterate through the string using a loop until the null terminator is reached.
  • isupper and islower check if a character is uppercase or lowercase, respectively.
  • We increment the corresponding count based on the character type.
  • Finally, we print the counts for both uppercase and lowercase characters.

2. String Manipulation Advanced

Exercise 3: Concatenate Two Strings

Write a program to concatenate two strings and store the result in a new string.

Solution:

C

#include <stdio.h>
#include <string.h>

int main() {
  char str1[50], str2[50], str3[100];

  printf("Enter first string: ");
  fgets(str1, 50, stdin);

  printf("Enter second string: ");
  fgets(str2, 50, stdin);

  // remove newline characters
  str1[strlen(str1) - 1] = '\0';
  str2[strlen(str2) - 1] = '\0';

  // concatenate strings using strcat
  strcat(str3, str1);
  strcat(str3, str2);

  printf("Combined string: %s\n", str3);

  return 0;
}

Explanation:

  • We declare three character arrays to store the two input strings and the combined string.
  • We read the two strings from the user using fgets.
  • We remove the newline characters from the input strings before concatenation.
  • strcat appends the contents of str2 to the end of str3 (which already contains str1).
  • Finally, we print the combined string.

Exercise 4: Reverse a String

Write a program to reverse a string in-place.

Solution:

C

  int n = strlen(str);
  for (int i = 0; i < n / 2; i++) {
    char temp = str[i];
    str[i] = str[n - 1 - i];
    str[n - 1 - i] = temp;
  }
}

int main() {
  char str[100];

  printf("Enter a string: ");
  fgets(str, 100, stdin);

  // remove newline character
  str[strlen(str) - 1] = '\0';

  reverseString(str);

  printf("Reversed string: %s\n", str);

  return 0;
}

Explanation:

  • We define a function reverseString that takes a character array as input.
  • We use a loop that iterates half the length of the string.
  • Inside the loop, we swap the characters at opposite ends using a temporary variable to hold the value during the exchange.
  • This effectively reverses the order of characters within the original string.
  • The main function reads the string from the user, removes the newline character, calls the reverseString function, and then prints the reversed string.

Bonus Exercise:

  • Implement a function to compare two strings for case-insensitive comparison.

Remember, these are just a few examples. Explore further string manipulation exercises like finding sub-strings, replacing characters, and counting occurrences of specific letters to refine your string handling skills and unlock the full potential of string operations in C programming.

Feel free to ask if you have any questions or need me to elaborate on any specific aspects of string manipulation in C.

CodeForHunger

Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button