QUESTION-16 Find the Frequency of the Word ‘the’ in a given Sentence
Answer-
void main() { int count = 0, i, times = 0, t, h, e, space; char string[100]; puts("Enter a string:"); gets(string); while (string[count] != '\0') { count++; } for (i = 0; i <= count - 3; i++) { t =(string[i] == 't' || string[i] == 'T'); h =(string[i + 1] == 'h' || string[i + 1] == 'H'); e =(string[i + 2] == 'e'|| string[i + 2] == 'E'); space =(string[i + 3] == ' ' || string[i + 3] == '\0'); if ((t && h && e && space) == 1) times++; } printf("Frequency of the word 'the' is %d\n", times); }
QUESTION-17 Read a String and find the Sum of all Digits in the String
Answer-
void main() { char string[80]; int count, nc = 0, sum = 0; printf("Enter the string containing both digits and alphabet \n"); scanf("%s", string); for (count = 0; string[count] != '\0'; count++) { if ((string[count] >= '0') && (string[count] <= '9')) { nc += 1; sum += (string[count] - '0'); } } printf("NO. of Digits in the string = %d\n", nc); printf("Sum of all digits = %d\n", sum); }
QUESTION-18 Copy One String to Another using Recursion
Answer-
void copy(char [], char [], int); int main() { char str1[20], str2[20]; printf("Enter string to copy: "); scanf("%s", str1); copy(str1, str2, 0); printf("Copying success.\n"); printf("The first string is: %s\n", str1); printf("The second string is: %s\n", str2); return 0; } void copy(char str1[], char str2[], int index) { str2[index] = str1[index]; if (str1[index] == '\0') return; copy(str1, str2, index + 1); }
QUESTION-19 Check whether two Strings are Anagrams
Answer-
int find_anagram(char [], char []); int main() { char array1[100], array2[100]; int flag; printf("Enter the string\n"); gets(array1); printf("Enter another string\n"); gets(array2); flag = find_anagram(array1, array2); if (flag == 1) printf(""%s" and "%s" are anagrams.\n", array1, array2); else printf(""%s" and "%s" are not anagrams.\n", array1, array2); return 0; } int find_anagram(char array1[], char array2[]) { int num1[26] = {0}, num2[26] = {0}, i = 0; while (array1[i] != '\0') { num1[array1[i] - 'a']++; i++; } i = 0; while (array2[i] != '\0') { num2[array2[i] -'a']++; i++; } for (i = 0; i < 26; i++) { if (num1[i] != num2[i]) return 0; } return 1; }
QUESTION-20 Print the Words Ending with Letter S
Answer-
char str[100]; void main() { int i, t, j, len; printf("Enter a string : "); scanf("%[^\n]s", str); len = strlen(str); str[len] = ' '; for (t = 0, i = 0; i < strlen(str); i++) { if ((str[i] == ' ') && (str[i - 1] == 's')) { for (j = t; j < i; j++) printf("%c", str[j]); t = i + 1; printf("\n"); } else { if (str[i] == ' ') { t = i + 1; } } } }