21. Program to accept a string and print each word of the string separately also print total number of words
#include<stdio.h> #include<conio.h> // Linux user - Remove this #include<string.h> int main() { // Declaring variable str=string, word=to store the word char str[50], word[50]; /* Declaring variable cw=count words, i=to iterate loop, j=works as index position of word */ int cw=0, i, j=0; // Inputing string printf("Enter any string : "); gets(str); /* We are concatinating a space here assuming user does not give space at the last */ strcat(str," "); // Printing string printf("Entered string : %s",str); //Determing total number of words and displaying it for(i=0;str[i]!='\0';i++) { if(str[i]!=' ') { word[j++]=str[i]; } else { word[j]='\0'; printf("\nWord : %s",word); cw++; j=0; } } // Printing total words printf("\nTotal number of words : %d",cw); getch(); // Linux user - Remove this return 0; }
22. Program to accept a string and display vowels frequency( total number of vowels)
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring variable str=string char str[50]; /* Declaring variable cv=count vowel, i=to iterate loop ( i works as index position) */ int cv=0, i; // Inputing string printf("Enter any string : "); gets(str); // Checking if vowels and counting them for(i=0;str[i]!='\0';i++) { if(str[i]=='a' || str[i]=='A' || str[i]=='e' || str[i]=='E' || str[i]=='i' || str[i]=='I' || str[i]=='o' || str[i]=='O' || str[i]=='u' ||str[i]=='U') { cv++; } } // Printing total number of vowels printf("Entered String : %s",str); printf("\nTotal number of vowels : %d",cv); getch(); // Linux user - Remove this return 0; }
23. Program to accept a string and display frequency of each vowel along with vowel
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring variable str=string char str[50]; // Declaring variable ca=count vowel a int ca=0, ce=0, ci=0, co=0, cu=0; // Declaring variable i=to iterate loop ( i works as index position) int i; // Inputing string printf("Enter any string : "); gets(str); // Determining frequency of each vowel for(i=0;str[i]!='\0';i++) { if(str[i]=='a' || str[i]=='A') ca++; if(str[i]=='e' || str[i]=='E') ce++; if(str[i]=='i' || str[i]=='I') ci++; if(str[i]=='o' || str[i]=='O') co++; if(str[i]=='u' || str[i]=='U') cu++; } // Printing string printf("Entered String : %s",str); // Printing frequency of string printf("\nFrequency of 'a' or 'A' : %d",ca); printf("\nFrequency of 'e' or 'E' : %d",ce); printf("\nFrequency of 'i' or 'I : %d",ci); printf("\nFrequency of 'o' or 'O' : %d",co); printf("\nFrequency of 'u' or 'U' : %d",cu); getch(); // Linux user - Remove this return 0; }
24. Program to enter a word and check whether it is palindrome or not using string function
// palindrom - When word = reverse of word then it is called palindrome // Example : madam - palindrome, Madam - not palindrome, MadaM - palindrome #include<stdio.h> #include<conio.h> // Linux user - Remove this #include<string.h> int main() { // Declaring variable str=string char word[50], word1[50]; // Inputing string printf("Enter any word : "); scanf("%s",&word); // Copying word to word1 strcpy(word1,word); // checking if palindrome or not if(strcmp(word,strrev(word1))==0) { printf("Entered word is a palindrome "); } else printf("Entered word is not palindrome "); getch(); // Linux user - Remove this return 0; }
25. Program to enter a word and check whether it is palindrome or not without using string function
#include<stdio.h> #include<conio.h> // Linux user - Remove this int main() { // Declaring variable str=string char word[50], revword[50]; /* Declaring variable i=to iterate loop, l=length, c=count the number of matched character */ int i, j, l=0, c; // Inputing string printf("Enter any word : "); scanf("%s",&word); // finding length while(word[l]!='\0') l++; // Reversing string j=0; for(i=l-1;i>=0;i--) { revword[j]=word[i]; j++; } revword[j]='\0'; //checking if palindrome or not c=0; for(i=0;word[i]!='\0';i++) { if(revword[i]==word[i]) c++; } if(c==l) { printf("Word is a palindrome "); } else printf("Word is not palindrome "); getch(); // Linux user - Remove this return 0; }