In the previous section we discussed the basics of C Strings. Now let us take a look at some real-life application Problems.
1. Write a C program to count vowels and consonants in a string.
#include
int main() {
char str[100];
int i, vowel = 0, constraint = 0;
printf("Enter a string:");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) {
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')
vowel++;
else if ((str[i]>='a'&&str[i]<='z') || (str[i]>='A'&&str[i]<='Z'))
constraint++;
}
printf("Vowels = %d\nConsonants = %d\n", vowel, constraint);
return 0;
}
// Enter a string: qubrica
// Vowels = 3
// Consonants = 4
| Index (i) | Character str[i] | Category | Logic | Vowels | Consonants |
|---|---|---|---|---|---|
| 0 | 'q' | Consonant | else if | 0 | 1 |
| 1 | 'u' | Vowel | if | 1 | 1 |
| 2 | 'b' | Consonant | else if | 1 | 2 |
| 3 | 'r' | Consonant | else if | 1 | 3 |
| 4 | 'i' | Vowel | if | 2 | 3 |
| 5 | 'c' | Consonant | else if | 2 | 4 |
| 6 | 'a' | Vowel | if | 3 | 4 |
| 7 | '\n' | Newline | Neither | 3 | 4 |
| 8 | '\0' | Null | Terminate | 3 | 4 |
2. Write a C program to find the frequency of a character in a string.
#include
int main() {
char input_text[120], target;
int index, occurrences = 0;
printf("Please provide a sentence: ");
scanf("%[^\n]", input_text);
printf("Which character are you looking for? ");
scanf(" %c", &target);
// Iterating through the text
for (index = 0; input_text[index] != '\0'; index++) {
if (input_text[index] == target) {
occurrences++;
}
}
printf("Total count of '%c': %d\n", target, occurrences);
return 0;
}
// Please provide a sentence: qubrica
// Which character are you looking for? i
// Total count of 'i': 1
| index | Character | Comparison (== 'i') | Action | occurrences |
|---|---|---|---|---|
| 0 | 'q' | False | Skip | 0 |
| 1 | 'u' | False | Skip | 0 |
| 2 | 'b' | False | Skip | 0 |
| 3 | 'r' | False | Skip | 0 |
| 4 | 'i' | True | Increment | 1 |
| 5 | 'c' | False | Skip | 1 |
| 6 | 'a' | False | Skip | 1 |
| 7 | '\0' | N/A | Terminate | 1 |
3. Write a C program to remove all spaces from a string.
#include
int main() {
char str[100];
int i, j = 0;
printf("Enter a string: ");
scanf("%[^\n]", str);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] != ' ')
str[j++] = str[i];
}
// Null-terminate the modified string
str[j] = '\0';
printf("String without spaces: %s\n", str);
return 0;
}
// Enter a string: C Program
// String without spaces: CProgram
| Index | str[i] | Condition (!= ' ') | Action | Resulting str (First j chars) | j value |
|---|---|---|---|---|---|
| 0 | 'C' | True | str[0] = 'C' | C | 1 |
| 1 | " " | False | Skip | C | 1 |
| 2 | 'P' | True | str[1] = 'P' | CP | 2 |
| 3 | 'r' | True | str[2] = 'r' | CPr | 3 |
| 4 | 'o' | True | str[3] = 'o' | CPro | 4 |
| 5 | 'g' | True | str[4] = 'g' | CProg | 5 |
| 6 | 'r' | True | str[5] = 'r' | CProgr | 6 |
| 7 | 'a' | True | str[6] = 'a' | CProgra | 7 |
| 8 | 'm' | True | str[7] = 'm' | CProgram | 8 |
| 9 | 'm' | True | str[8] = 'm' | CProgramm | 9 |
| 10 | 'i' | True | str[9] = 'i' | CProgrammi | 10 |
| 11 | 'n' | True | str[10] = 'n' | CProgrammin | 11 |
| 12 | 'g' | True | str[11] = 'g' | CProgramming | 12 |
| 13 | '\0' | N/A | Terminate | CProgramming\0 | 12 |
4. Write a C program to check whether one string is a substring of another.
#include
int main() {
char str[100], sub[100];
int i, j, found = 0;
printf("Enter main string: ");
scanf("%[^\n]", str);
printf("Enter substring: ");
scanf(" %[^\n]", sub);
for (i = 0; str[i] != '\0'; i++) {
for (j = 0; sub[j] != '\0'; j++) {
if (str[i + j] != sub[j])
break;
}
if (sub[j] == '\0') {
found = 1;
break;
}
}
if (found)
printf("Substring found\n");
else
printf("Substring not found\n");
return 0;
}
// Enter main string: qubrica
// Enter substring: rica
// Substring found
| Outer Index (i) | str[i] | Inner Index (j) | sub[j] | Comparison (str[i+j] == sub[j]) | Action | found |
|---|---|---|---|---|---|---|
| 0 | 'q' | 0 | 'r' | 'q' == 'r' (False) | break inner loop | 0 |
| 1 | 'u' | 0 | 'r' | 'u' == 'r' (False) | break inner loop | 0 |
| 2 | 'b' | 0 | 'r' | 'b' == 'r' (False) | break inner loop | 0 |
| 3 | 'r' | 0 | 'r' | 'r' == 'r' (True) | Continue inner | 0 |
| 3 | 1 | 'i' | 'i' == 'i' (True) | Continue inner | 0 | |
| 3 | 2 | 'c' | 'c' == 'c' (True) | Continue inner | 0 | |
| 3 | 3 | 'a' | 'a' == 'a' (True) | Continue inner | 0 | |
| 3 | 4 | '\0' | N/A | Inner loop ends | 1 |
5. Write a C program to convert all lowercase letters in a string to uppercase.
#include
int main() {
char str[100];
int i;
printf("Enter a string: ");
scanf("%[^\n]", str);
for (i = 0; str[i] != '\0'; i++) {
// Check if the character is a lowercase letter
if (str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 32;
}
printf("Uppercase string: %s\n", str);
return 0;
}
// Enter a string: qubrica
// Uppercase string: QUBRICA
| Index (i) | Character str[i] | ASCII Value | Condition ('a' to 'z') | Calculation (-32) | New Character |
|---|---|---|---|---|---|
| 0 | 'q' | 113 | True | 113 - 32 = 81 | 'Q' |
| 1 | 'u' | 117 | True | 117 - 32 = 85 | 'U' |
| 2 | 'b' | 98 | True | 98 - 32 = 66 | 'B' |
| 3 | 'r' | 114 | True | 114 - 32 = 82 | 'R' |
| 4 | 'i' | 105 | True | 105 - 32 = 73 | 'I' |
| 5 | 'c' | 99 | True | 99 - 32 = 67 | 'C' |
| 6 | 'a' | 97 | True | 97 - 32 = 65 | 'A' |
| 7 | '\0' | 0 | False | N/A | Terminate |
6. Write a C program to count the number of words in a string.
console.log( 'Code is Poetry' );#include
int main() {
char str[100];
int i, count = 0;
printf("Enter a string: ");
scanf("%[^\n]", str);
for (i = 0; str[i] != '\0'; i++) {
// Increment count when a space is followed by a character
if (str[i] == ' ' && str[i+1] != ' ' && str[i+1] != '\0')
count++;
}
// Account for the first word if the string doesn't start with a space
if (str[0] != ' ' && str[0] != '\0')
count++;
printf("Number of words = %d\n", count);
return 0;
}
//Enter a string: I love Qubrica
// Number of words = 3
| Index | Character | Logic Check | Result | count |
|---|---|---|---|---|
| 0 | 'i' | str[0] != ' ' | First word detected | 1 |
| 1 | ' ' | str[1]==' ' && str[2]=='l' | Space-to-Character transition | 2 |
| 2 - 5 | love | str[i] != ' ' | Characters skipped (no space) | 2 |
| 6 | ' ' | str[6]==' ' && str[7]=='q' | Space-to-Character transition | 3 |
| 7 - 13 | qubrica | str[i] != ' ' | Characters skipped (no space) | 3 |
| 14 | '\0' | str[14] == '\0' | Loop termination | 3 |