Learning string programs helps beginners understand character handling, loops, arrays, and library functions in C.z
Table of Contents
Program to Find Length of a String
This program counts the total number of characters present in a string excluding the null character.
// C program to find the
// length of the string
#include stdio.h
int main()
{
char str[100];
int length = 0, i = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
while(str[i] != '\0')
{
length++;
i++;
}
printf("Length of string = %d", length);
return 0;
}
Output:
Explanation:Enter a string: Welcome to C Programming
Length of string = 25
The loop checks every character until the null character is reached. The total count gives the string length.
Program to Reverse a String
This program reverses the order of characters in a string using character swapping.
// C program to reverse a string
#include stdio.h
#include string.h
int main()
{
char str[100], temp;
int i, length;
printf("Enter string: ");
fgets(str, sizeof(str), stdin);
length = strlen(str);
for(i = 0; i length/2; i++)
{
temp = str[i];
str[i] = str[length-i-1];
str[length-i-1] = temp;
}
printf("Reversed string: %s", str);
return 0;
}
Output:
Explanation:Enter string: Welcome to C Programming
Reversed string:
gnimmargorP C ot emocleW
Characters from the beginning and end are swapped until the middle of the string is reached.
Program to Check Palindrome String
A palindrome string remains the same when reversed. This program checks whether a string reads the same from both directions.
// C program to check palindrome strings
#include stdio.h
#include string.h
int main()
{
char str[100], rev[100];
int i, length, isPalindrome = 1;
printf("Enter string: ");
fgets(str, sizeof(str), stdin);
length = strlen(str);
if(str[length - 1] == '\n')
{
str[length - 1] = '\0';
length--;
}
for(i = 0; i length; i++)
{
rev[i] = str[length - i - 1];
}
rev[i] = '\0';
for(i = 0; i length; i++)
{
if(str[i] != rev[i])
{
isPalindrome = 0;
break;
}
}
if(isPalindrome)
printf("Palindrome");
else
printf("Not Palindrome");
return 0;
}
Output:
Explanation:Enter string: Hello
Not Palindrome
The program creates a reversed version of the string and compares it with the original string.
Program to Count Vowels in a String
This program counts the total number of vowels present in a string.
// C program to count vowels in a string
#include stdio.h
int main()
{
char str[100];
int i = 0, count = 0;
printf("Enter string: ");
fgets(str, sizeof(str), stdin);
while(str[i] != '\0')
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u' ||
str[i]=='A' || str[i]=='E' ||
str[i]=='I' || str[i]=='O' ||
str[i]=='U')
{
count++;
}
i++;
}
printf("Total vowels = %d", count);
return 0;
}
Output:
Explanation:Enter string: Hello
Total vowels = 2
Each character is checked individually to determine whether it is a vowel.
Program to Copy One String to Another
This program copies all characters from one string into another string.
// C program to copy one
// string to another
#include stdio.h
int main()
{
char str1[100], str2[100];
int i = 0;
printf("Enter string: ");
fgets(str1, sizeof(str1), stdin);
while(str1[i] != '\0')
{
str2[i] = str1[i];
i++;
}
str2[i] = '\0';
printf("Copied string: %s", str2);
return 0;
}
Output:
Explanation:Enter string: C Programming
Copied string: C Programming
Characters are copied one by one from the first string to the second string.
Program to Concatenate Two Strings
This program joins two strings together into a single string.
// C program to concatenate two strings
#include stdio.h
#include string.h
int main()
{
char str1[100], str2[100];
int length, i = 0;
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
length = strlen(str1);
if(str1[length - 1] == '\n')
{
str1[length - 1] = '\0';
}
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
while(str1[i] != '\0')
{
i++;
}
int j = 0;
while(str2[j] != '\0' && str2[j] != '\n')
{
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0';
printf("Combined string: %s", str1);
return 0;
}
Output:
Explanation:Enter first string: Hello
Enter second string: World
Combined string: HelloWorld
The second string is added at the end of the first string character by character.
Program to Compare Two Strings
This program compares two strings and checks whether they are equal or not.
// C program to compare two strings
#include stdio.h
#include string.h
int main()
{
char str1[100], str2[100];
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
if(strcmp(str1, str2) == 0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}
Output:
Explanation:Enter first string: C programming
Enter second string: C Programming
Strings are not equal
The strcmp() function compares two strings. If both strings are the same, it returns 0.
Program to Convert String to Uppercase
This program converts all lowercase letters of a string into uppercase letters.
// C program to convert string to uppercase
#include stdio.h
int main()
{
char str[100];
int i = 0;
printf("Enter string: ");
fgets(str, sizeof(str), stdin);
while(str[i] != '\0')
{
if(str[i] = 'a' && str[i] = 'z')
{
str[i] = str[i] - 32;
}
i++;
}
printf("Uppercase string: %s", str);
return 0;
}
Output:
Explanation:Enter string: TutorialforGeeks
Uppercase string: TUTORIALFORGEEKS
ASCII value difference between lowercase and uppercase letters is 32, so subtraction converts the character.
Program to Convert String to Lowercase
This program converts all uppercase letters into lowercase letters.
// C program to convert string to lowercase
#include stdio.h
int main()
{
char str[100];
int i = 0;
printf("Enter string: ");
fgets(str, sizeof(str), stdin);
while(str[i] != '\0')
{
if(str[i] = 'A' && str[i] = 'Z')
{
str[i] = str[i] + 32;
}
i++;
}
printf("Lowercase string: %s", str);
return 0;
}
Output:
Explanation:Enter string: TutorialforGeeks
Lowercase string: tutorialforgeeks
Adding 32 to uppercase ASCII characters converts them into lowercase letters.
Program to Count Words in a String
This program counts the total number of words present in a string.
// C program to count words in a string
#include stdio.h
int main()
{
char str[100];
int i = 0, words = 1;
printf("Enter string: ");
fgets(str, sizeof(str), stdin);
while(str[i] != '\0')
{
if(str[i] == ' ')
{
words++;
}
i++;
}
printf("Total words = %d", words);
return 0;
}
Output:
Explanation:Enter string: Welcome to TutorialforGeeks
Total words = 3
The program counts spaces in the sentence because words are generally separated by spaces.
Common Mistakes in String Programs
- Forgetting to Remove Newline from fgets(): fgets() stores the newline character entered by the user, which can affect comparisons and output formatting.
- Using Small Character Arrays: Declaring very small arrays may cause overflow when larger strings are entered.
- Accessing Invalid Index Positions: Accessing characters outside the array range can produce undefined behavior.
- Forgetting Header Files: Functions like strlen(), strcmp(), and strcat() require the string.h header file.
- Comparing Strings Using ==: The == operator compares addresses instead of string contents. strcmp() should be used for string comparison.
Conclusion
String programs in C are essential for learning text processing and character manipulation. They improve problem-solving skills and provide practical understanding of loops and arrays. Practicing different string operations helps beginners build a strong foundation in C programming.Frequently Asked Questions
1. What is a string in C?2. Why is fgets() preferred over gets() ?A string is a collection of characters stored in a character array ending with the null character '\0'.
3. Which header file is used for string functions?fgets() is safer because it limits the number of characters entered by the user.
4. Why is strrev() avoided?The string.h header file contains standard string functions.
5. Which function finds string length?strrev() is not a standard C function and may not work in many compilers.
The strlen() function is used to calculate string length.
0 Comments