Question:7
C program to take input for a string, display the string in such a manner that all lower case alphabets are displayed as upper case and rest are displayed as it is.
Sol:
/* Using for loop */ #include<stdio.h> #include<ctype.h> int main() { char n[20]; int i=0; printf("Enter any string "); gets(n); for(i=0;n[i]!='\0';i++) { if(n[i]>='a' && n[i]<='z') printf("%c",n[i]-32); else printf("%c",n[i]); } return(0); }
/* Using while loop */ #include<stdio.h> #include<ctype.h> int main() { char n[20]; int i=0; printf("Enter any string "); gets(n); while(n[i]!='\0') { if(n[i]>='a' && n[i]<='z') printf("%c",n[i]-32); else printf("%c",n[i]); i++; } return(0); }
/* Output */ Enter any string Hello hi HELLO HI
Question:8
C program to take input for a string, display the string in toggle case (i.e. all upper case alphabets are displayed as lower case, lower case are displayed as upper case and rest are displayed as it is).
ASCII Values
A –Z : 65 – 90
a- z: 97 – 122
0-9 : 48 – 57
Sol:
/* Using for loop */ #include<stdio.h> #include<ctype.h> int main() { char n[20]; int i=0; printf("Enter any string "); gets(n); for(i=0;n[i]!='\0';i++) { if(n[i]>='a' && n[i]<='z') printf("%c",n[i]-32); else if(n[i]>='A' && n[i]<='Z') printf("%c",n[i]+32); else printf("%c",n[i]); } return(0); }
/* Using while loop */ #include<stdio.h> #include<ctype.h> int main() { char n[20]; int i=0; printf("Enter any string "); gets(n); while(n[i]!='\0') { if(n[i]>='a' && n[i]<='z') printf("%c",n[i]-32); else if(n[i]>='A' && n[i]<='Z') printf("%c",n[i]+32); else printf("%c",n[i]); i++; } return(0); }
/* Output */ Enter any string CoMpUtEr cOmPuTeR
Question:9
C program to take input for a string, further take input for a character to search, check and print whether the character is present in the string or not? (Searching)
Sol:
/* Using for loop */ #include<stdio.h> #include<ctype.h> int main() { char n[20],ch; int i=0,z=0; printf("Enter any string "); gets(n); printf("Enter the character to search "); scanf("%c",&ch); for(i=0;n[i]!='\0';i++) { if(n[i]==ch) { z=1; break; } } if(z==0) printf("character is not present"); else printf("character is present"); return(0); }
/* Using while loop */ #include<stdio.h> #include<ctype.h> int main() { char n[20],ch; int i=0,z=0; printf("Enter any string "); gets(n); printf("Enter the character to search "); scanf("%c",&ch); while(n[i]!='\0') { if(n[i]==ch) { z=1; break; } i++; } if(z==0) printf("character is not present"); else printf("character is present"); return(0); }
/* Output */ case:1 Enter any string hello Enter the character to search k character is not present case:2 Enter any string computer Enter the character to search p character is present