Top 10 String programs in C

Let's dive into the fascinating world of C programming with strings. Below are 15 important programs that deal with strings.

1. Length of a String 📏

c
#include <stdio.h> #include <string.h> int main() { char str[100]; printf("Enter a string: "); gets(str); int length = strlen(str); printf("Length of the string: %d\n", length); return 0; }

2. Concatenate Two Strings 🧲

c
#include <stdio.h> #include <string.h> int main() { char str1[50], str2[50]; printf("Enter the first string: "); gets(str1); printf("Enter the second string: "); gets(str2); strcat(str1, str2); printf("Concatenated string: %s\n", str1); return 0; }

3. Compare Two Strings 🤔

c
#include <stdio.h> #include <string.h> int main() { char str1[50], str2[50]; printf("Enter the first string: "); gets(str1); printf("Enter the second string: "); gets(str2); int result = strcmp(str1, str2); if (result == 0) { printf("Strings are equal.\n"); } else { printf("Strings are not equal.\n"); } return 0; }

4. Copy One String to Another 📝

c
#include <stdio.h> #include <string.h> int main() { char source[50], destination[50]; printf("Enter a string: "); gets(source); strcpy(destination, source); printf("Copied string: %s\n", destination);  
return 0;
}

5. Reverse a String ↩️

c
#include <stdio.h> #include <string.h> int main() { char str[100]; printf("Enter a string: "); gets(str); strrev(str); printf("Reversed string: %s\n", str); return 0; }

6. Convert String to Uppercase 🔠

c
#include <stdio.h> #include <string.h> int main() { char str[100]; printf("Enter a string: "); gets(str); strupr(str); printf("Uppercase string: %s\n", str); return 0; }

7. Convert String to Lowercase 🔡

c
#include <stdio.h> #include <string.h> int main() { char str[100]; printf("Enter a string: "); gets(str); strlwr(str); printf("Lowercase string: %s\n", str); return 0; }

8. Check for Palindrome 🔄

c
#include <stdio.h>
#include <string.h> int main() { char str[100]; printf("Enter a string: "); gets(str); int len = strlen(str); int isPalindrome = 1; for (int i = 0; i < len / 2; i++) { if (str[i] != str[len - i - 1]) { isPalindrome = 0; break; } } if (isPalindrome) { printf("Palindrome!\n"); } else { printf("Not a palindrome.\n"); } return 0; }

9. Count Vowels and Consonants 🗣️

c
#include <stdio.h>
#include <string.h> int main() { char str[100]; printf("Enter a string: "); gets(str); int vowels = 0, consonants = 0; int len = strlen(str); for (int i = 0; i < len; i++) { char ch = str[i]; if (ch >= 'a' && ch <= 'z') { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { vowels++; } else { consonants++; } } else if (ch >= 'A' && ch <= 'Z') { if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { vowels++; } else { consonants++; } } } printf("Vowels: %d, Consonants: %d\n", vowels, consonants); return 0; }

10. Count Words in a String 📊

c
#include <stdio.h> #include <string.h> int main() { char str[100]; printf("Enter a string: "); gets(str); int words = 1; int len = strlen(str); for (int i = 0; i < len; i++) { if (str[i] == ' ') { words++; } } printf("Number of words: %d\n", words); return 0; }

11. Replace Substring 🔄

c
#include <stdio.h> #include <string.h> int main() { char str[100], old[50], new[50]; printf("Enter a string: "); gets(str); printf("Enter the substring to replace: "); gets(old); printf("Enter the new substring: "); gets(new); char *pos = strstr(str, old); if (pos != NULL) { int index = pos - str; memmove(pos + strlen(new), pos + strlen(old), strlen(pos + strlen(old)) + 1); strncpy(pos, new, strlen(new)); } printf("Modified string: %s\n", str); return 0; }

12. Remove Spaces from a String

c
#include <stdio.h> #include <string.h> int main() { char str[100]; printf("Enter a string: "); gets(str); int len = strlen(str); for (int i = 0; i < len; i++) { if (str[i] == ' ') { memmove(&str[i], &str[i + 1], len - i); len--; i--; } } printf("String without spaces: %s\n", str); return 0; }

13. Extract Substring 📤

c
#include <stdio.h> #include <string.h> int main() { char str[100]; int start, length; printf("Enter a string: "); gets(str); printf("Enter the starting position: "); scanf("%d", &start); printf("Enter the length of the substring: "); scanf("%d", &length); char substr[length + 1]; strncpy(substr, str + start, length); substr[length] = '\0'; printf("Extracted substring: %s\n", substr); return 0; }

14. Frequency of Each Character 📊

c
#include <stdio.h> #include <string.h> int main() { char str[100]; printf("Enter a string: "); gets(str); int frequency[128] = {0}; int len = strlen(str); for (int i = 0; i < len; i++) { frequency[str[i]]++; } printf("Character frequencies:\n"); for (int i = 0; i < 128; i++) { if (frequency[i] > 0) { printf("%c: %d\n", i, frequency[i]); } } return 0; }

15. Check Anagram ✨

Determines if two entered strings are anagrams (contain the same characters with the same frequency).

c
#include <stdio.h> #include <string.h> int main() { char str1[50], str2[50]; printf("Enter the first string: "); gets(str1); printf("Enter the second string: "); gets(str2); int len1 = strlen(str1); int len2 = strlen(str2); if (len1 != len2) { printf("Not anagrams.\n"); return 0; } int frequency[128] = {0}; for (int i = 0; i < len1; i++) { frequency[str1[i]]++; frequency[str2[i]]--; } for (int i = 0; i < 128; i++) { if (frequency[i] != 0) { printf("Not anagrams.\n"); return 0; } } printf("Anagrams!\n"); return 0; }

Feel free to explore and modify these programs to deepen your understanding of C string manipulation! Happy coding! 🚀💻 #CStringManipulation #CProgramming #LearnToCod