Write a menu driven program in 'C' which performs the following operations on strings.Write separate function for each option -Check if one string is substring of another string -Count number of occurrences of a character in the string. -Exit.
#includevoid substring(void); void occurance(void); void main() { int choice; clrscr(); do { printf("\nPRESS 1 TO CHECK WHEATHER STRING IS SUBSTRING OR NOT."); printf("\nPRESS 2 TO COUNT OCCURRENCES OF CHARACTER IN STRING."); printf("\nPRESS 3 TO EXIT."); printf("\nENTER YOUR CHOICE::> "); scanf("%d",&choice); fflush(stdin); switch(choice) { case 1: substring(); break; case 2: occurance(); break; case 3: exit(0); } }while(choice != 3); getch(); } void substring() { int i,j,count = 0; char str[80],substr[80]; printf("ENTER ANY STRING ::>\n"); gets(str); printf("ENTER ANY STRING ::>\n"); gets(substr); for(i = 0 ; substr[i] != '\0'; i++) { count++; } for(i = 0; str[i] != '\0'; i++) { j = 0; while(str[i] == substr[j] && substr[j] != '\0') { j++; i++; } if(count == j) { printf("IT IS SUBSTRING"); break; } } if(count != j) { printf("IT IS NOT SUBSTRING"); } } void occurance() { int i,count = 0; char str[80],ch; printf("ENTER ANY STRING ::>\n"); gets(str); printf("ENTER ANY CHARACTER ::>\n"); scanf("%c",&ch); fflush(stdin); for(i = 0; str[i] != '\0'; i++) { if(str[i] == ch) { count++; } } printf("%c OCCURS %d TIMES.",ch,count); }
Write a menu driven program in 'C' which performs the following operations on strings.Write separate function for each option -Check if one string is substring of another string -Count number of occurrences of a character in the string. -Exit.
Reviewed by
on
April 24, 2015
Rating: