Introduction to Strings in C
Strings are an important topic in C programming because they are used to store and process text such as names, sentences, usernames, passwords, and messages. In C, a string is stored as a character array and ends with the null character \0.
The best way to learn strings is through practical coding exercises. The following questions are designed for beginners and cover string traversal, character counting, reversing, palindrome checking, word counting, and string searching.
Q1: Find the Length of a String Without Using strlen()
Question: Write a C program to accept a string and find its length without using the built-in strlen() function.
Step 1: Create a character array to store the string.
Step 2: Read the string using fgets().
Step 3: Traverse the string using a loop until \0 or the newline character is found.
Step 4: Increase a counter for every character and display the final count.
#include <stdio.h>
int main() {
char str[100];
int i = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
while (str[i] != '\0' && str[i] != '\n') {
i++;
}
printf("Length = %d", i);
return 0;
}
Q2: Count Vowels and Consonants in a String
Question: Write a C program to count the number of vowels and consonants in a given string.
Step 1: Read the string from the user.
Step 2: Traverse every character using a loop.
Step 3: Check whether the character is an alphabet.
Step 4: If the character is a, e, i, o, or u, increase the vowel count. Otherwise, increase the consonant count.
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i, vowels = 0, consonants = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) {
char ch = tolower((unsigned char)str[i]);
if (ch >= 'a' && ch <= 'z') {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else {
consonants++;
}
}
}
printf("Vowels = %d\n", vowels);
printf("Consonants = %d", consonants);
return 0;
}
Q3: Reverse a String Without Using strrev()
Question: Write a C program to reverse a string without using the built-in strrev() function.
Step 1: Read the string using fgets().
Step 2: Find the length of the string using a loop.
Step 3: Start from the last character.
Step 4: Print each character in reverse order.
#include <stdio.h>
int main() {
char str[100];
int i, length = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
while (str[length] != '\0' && str[length] != '\n') {
length++;
}
printf("Reversed string: ");
for (i = length - 1; i >= 0; i--) {
printf("%c", str[i]);
}
return 0;
}
Q4: Check Whether a String Is a Palindrome
Question: Write a C program to check whether a string is a palindrome. A palindrome reads the same from both directions, such as madam or level.
Step 1: Read the string.
Step 2: Find the string length.
Step 3: Compare the first character with the last character, the second with the second-last, and so on.
Step 4: If all corresponding characters match, the string is a palindrome.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, length, palindrome = 1;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
length = strlen(str);
for (i = 0; i < length / 2; i++) {
if (str[i] != str[length - i - 1]) {
palindrome = 0;
break;
}
}
if (palindrome) {
printf("Palindrome string");
} else {
printf("Not a palindrome string");
}
return 0;
}
Q5: Count the Number of Words in a Sentence
Question: Write a C program to count the number of words present in a sentence.
Step 1: Read the complete sentence using fgets().
Step 2: Traverse the sentence character by character.
Step 3: Identify the beginning of each word by checking for a non-space character after whitespace.
Step 4: Increase the word count whenever a new word starts.
#include <stdio.h>
#include <ctype.h>
int main() {
char str[200];
int i, words = 0, inWord = 0;
printf("Enter a sentence: ");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) {
if (!isspace((unsigned char)str[i]) && inWord == 0) {
words++;
inWord = 1;
} else if (isspace((unsigned char)str[i])) {
inWord = 0;
}
}
printf("Number of words = %d", words);
return 0;
}
Q6: Find the Frequency of a Character in a String
Question: Write a C program that accepts a string and a character, then counts how many times that character occurs in the string.
Step 1: Read the string.
Step 2: Ask the user for the character to search.
Step 3: Traverse the complete string.
Step 4: Compare every character with the search character and increase the counter when they match.
#include <stdio.h>
int main() {
char str[100], ch;
int i, count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter character to search: ");
scanf(" %c", &ch);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ch) {
count++;
}
}
printf("'%c' occurs %d times", ch, count);
return 0;
}
Practice Challenge: Solve These Yourself
After completing the solved exercises, try these problems without looking at the solution: remove all spaces from a string, convert lowercase characters to uppercase, count digits and special characters, compare two strings without using strcmp(), copy one string into another without using strcpy(), find the first occurrence of a character, and find the most frequently occurring character in a string.
What You Learn from These String Exercises
These exercises help beginners understand character arrays, loops, conditional statements, string functions, character functions, indexing, searching, and basic problem-solving. Once these concepts are comfortable, students can move toward more advanced C programming problems involving functions, pointers, structures, and file handling.
Conclusion
Practicing C programming questions on strings is one of the best ways to strengthen programming fundamentals. Start with simple problems such as finding length and counting characters, then progress to reversing strings, palindrome checking, word counting, and character frequency. Try writing every program yourself first, test it with different inputs, and then compare your approach with the solution.