1. Input a text string from user and count number of spaces in it For example if
ID: 3702153 • Letter: 1
Question
1. Input a text string from user and count number of spaces in it For example if user enters I am a student in KFU. Then you answer should be Number of spaces in text is 5 2. Change exercise 1 and find frequency of a character in it the character should be given by user( For example if user enters a string I am a student in KFU And then user enter a character a Frequency of a is 2 (appears twe times in string) 3. Input the first and second part of your name and combine the two parts. Display the combined name. Use streat(s1, s2) for this assignment 4. Input a text string from user and count vowels in the text. Display the vowel count to user 5. Input the names of two people in your program and compare the names whether they are same names or not. User stremp(s1, s2) 6. Input your name as text string and print your name in reverse order, For example if name is abedef Then you answer should be fedcba 7. Input a word from user, reverse the word and compare with original word, check whether reversed word is same as original or not. Such words whose reverse is same as original are called Palindromes e.g. racecar, deed, madam etc. 8. Input a text string from user and delete vowels in the text. Display the text to user after deletion ofall vowelsExplanation / Answer
#include<stdio.h>
#include<string.h>
void f1(){
char ch=getchar();
printf("Enter text:");
char s[100];
gets(s);
int cnt=0,i=0;
while(s[i]){
if(s[i]==' ')cnt++;
i++;
}
printf("Number of spaces in text is %d ",cnt);
}
void f2(){
char ch=getchar();
char s[100];
printf("Enter text:");
gets(s);
printf("Enter character:");
scanf("%c",&ch);
int i=0,cnt=0;
while(s[i]){
if(s[i]==ch)cnt++;
i++;
}
printf("Frequency of %c is %d ",ch,cnt);
}
void f3(){
char ch=getchar();
char firstname[100],lastname[100];
printf("Enter firstname:");
scanf("%s",firstname);
printf("Enter lastname:");
scanf("%s",lastname);
strcat(strcat(firstname," "),lastname);
printf("Name is %s ",firstname);
}
int isVowel(char c){
if(c>='a' && c<='z'){
return (c=='a' ||c=='e' ||c=='i' || c=='o' ||c=='u');
}
if(c>='A' && c<='Z'){
return (c=='A' ||c=='E' ||c=='I' || c=='O' ||c=='U');
}
return 0;
}
void f4(){
char ch=getchar();
printf("Enter text:");
char s[100];
gets(s);
int cnt=0,i=0;
while(s[i]){
if(isVowel(s[i]))cnt++;
i++;
}
printf("Number of vowels in text is %d ",cnt);
}
void f5(){
char ch=getchar();
printf("Enter name of person 1:");
char name1[100],name2[100];
gets(name1);
printf("Enter name of person 2:");
gets(name2);
if(strcmp(name1,name2)==0){
printf("Names are same ");
}else{
printf("Names are not same ");
}
}
void reverse(char *s){
int n=strlen(s),i=0,j=n-1;
while(i<j){
char temp=s[i];
s[i]=s[j];
s[j]=temp;
i++,j--;
}
}
void f6(){
char ch=getchar();
printf("Enter your name:");
char s[100];
gets(s);
reverse(s);
printf("Reverse name:%s ",s);
}
void f7(){
char ch=getchar();
printf("Enter a word:");
char s[100],revs[100];
gets(s);
strcpy(revs,s);
reverse(revs);
if(strcmp(s,revs)==0){
printf("String is a palindrome ");
}else{
printf("String is not a palindrome ");
}
}
void f8(){
char ch=getchar();
printf("Enter text:");
char s[100];
gets(s);
int i=0,j=0;
char without_vowel[100];
while(s[i]){
if(!isVowel(s[i])){
without_vowel[j++]=s[i];
}
i++;
}
without_vowel[j]=0;
strcpy(s,without_vowel);
printf("Without vowels:%s ",s);
}
int main(){
//it is a menu driven program
int assignment;
scanf("%d",&assignment);
switch (assignment)
{
case 1:f1();break;
case 2:f2();break;
case 3:f3();break;
case 4:f4();break;
case 5:f5();break;
case 6:f6();break;
case 7:f7();break;
case 8:f8();break;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.