String Operations in C - Programs
with Output
10 Example Programs with
Explanations and Outputs
1. String Length using strlen()
• Explanation: This program uses strlen() to find
the length of a string.
• Code:
• #include <stdio.h>
• #include <string.h>
• int main() {
• char str[50] = "Hello World";
• printf("Length: %lu", strlen(str));
2. Copy String using strcpy()
• Explanation: strcpy() copies the content of one
string to another.
• Code:
• #include <stdio.h>
• #include <string.h>
• int main() {
• char src[50] = "C Programming";
• char dest[50];
3. String Concatenation using
strcat()
• Explanation: strcat() joins two strings together.
• Code:
• #include <stdio.h>
• #include <string.h>
• int main() {
• char str1[50] = "Hello ";
• char str2[50] = "World";
• strcat(str1, str2);
4. Compare Strings using strcmp()
• Explanation: strcmp() compares two strings
lexicographically.
• Code:
• #include <stdio.h>
• #include <string.h>
• int main() {
• char str1[] = "apple";
• char str2[] = "banana";
5. Reverse a String
• Explanation: strrev() reverses the order of
characters in a string.
• Code:
• #include <stdio.h>
• #include <string.h>
• int main() {
• char str[50] = "Hello";
• strrev(str);
6. Convert String to Uppercase
• Explanation: toupper() converts each
character to uppercase.
• Code:
• #include <stdio.h>
• #include <ctype.h>
• int main() {
• char str[50] = "hello";
• for(int i = 0; str[i]; i++) {
7. Convert String to Lowercase
• Explanation: tolower() converts each character
to lowercase.
• Code:
• #include <stdio.h>
• #include <ctype.h>
• int main() {
• char str[50] = "HELLO";
• for(int i = 0; str[i]; i++) {
8. Count Vowels in a String
• Explanation: This program counts vowels in a
string.
• Code:
• #include <stdio.h>
• #include <string.h>
• #include <ctype.h>
• int main() {
• char str[50] = "Hello World";
9. Check Palindrome String
• Explanation: A palindrome reads the same
forwards and backwards.
• Code:
• #include <stdio.h>
• #include <string.h>
• int main() {
• char str[50] = "madam";
• char rev[50];
10. Remove Spaces from String
• Explanation: This program removes all spaces
from the string.
• Code:
• #include <stdio.h>
• #include <string.h>
• int main() {
• char str[50] = "Hello World";
• char noSpace[50];