How to 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 sub-string of another string
- Count number of occurrences of a character in the string
- Exit
Solution:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void substring();
void char_occurrence();
void main()
{
char str[30],str1[30];
int ch;
clrscr();
do
{
printf("\n****MENU****");
printf("\n1:Sub String");
printf("\n2:Character Occurrences");
printf("\n3:Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:substring();
break;
case 2:char_occurrance();
break;
case 3:exit(0);
break;
}
}
while(ch!=3);
getch();
}
void substring()
{
char str[30],ch[30];
fflush(stdin);
printf("\nEnter the first string: ");
gets(str);
fflush(stdin);
printf("\nEnter the second string: ");
gets(ch);
if(strstr(str,ch))
{
printf("\nThe given string is sub string");
}
else
{
printf("\nThe given string is not sub string");
}
}
void char_occurrennce()
{
char str2[30],ch;
int i,count=0;
fflush(stdin);
printf("\nEnter the string: ");
gets(str2);
fflush(stdin);
printf("\nEnter the character from string to find occurrence: ");
scanf("%c",&ch);
for(i=0;str2[i]!='\0';i++)
{
if(ch==str2[i])
{
count++;
}
}
printf("\nThe occurrence of character is: %d",count);
}
//OUTPUT:
//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit
//Enter your choice: 1
//Enter the first string: vinit
//Enter the second string: vinit
//The given string is sub string
//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit
//Enter your choice: 2
//Enter the string: vinit
//Enter the character from string to find occurrence: v
//The occurrence of character is: 1
//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit
//Enter your choice: 3
Learn More :
Count
- C Program String - Alphabet Count Example
- C Program String Count Example
- Write the procedure , which is one of a sum , a product and a geometric average in the panel for the NxM elements are located on opposite diagonal and main diagonal . Remind ! Counting only odd elements !
- C Program Array NxM Elements Geometric/Arithmetic
- C Program to Count Number of Lines in Input until Interrupted by CTRL + Z
- Returns: array of decoded values. [0] - count of values
- Max count of chars, Max count of nums and Max count of signs
- C Program to Count Vowels, Consonants and Spaces in a string
- C Program to count number of sentences words and letters in a paragraph
- Un-sortiertes Array and Sortiertes Array
- C Program to Count Change Program
- LED ON OFF For One Sec/Count and Display on the Attached Serial Monitor
- C program to accept n number from user,store these numbers into an array & count the no of occurrences of each number
String
- Write a c program to check given string is palindrome number or not.
- C Program to Convert Text String to Binary
- C Program String - Alphabet Count Example
- C Program String Example
- C Program String Count Example
- C Program String Example
- C Program to Print a String Without Use Semicolon.
- C Program To Find Length Of A String Including Blank Spaces, Tabs, And Other Special Characters
- C Program To Find Length Of String And Concatenate Two Strings
- C Program To Find The Length, Reverse Of A String And To Check It Is Palindrome or Not
- C Program To Print String In Formatted Output Form
- C Program To Reverse The String Using Pointer
- C Program Concatenating Two Strings Into A Third System
- C Program Date from string DDDD-DD-DD
- BUILDS A STRING FROM THE LSB BITS OF EACH PIXEL
- C Program Finds Sub-String Search in String
- C Program to concatenate two strings without using string functions
- C Program to Count Vowels, Consonants and Spaces in a string
- C Program to convert a string to upper case
- C Program Performs a search and replace for a specified target and replacement string
- C Program Anagrams
- C Program to find string length without library function
- C Program to Check Given String is Palindrome or Not
- C Program to Converts a Number to a String.
- Design a C function that shortens a string to 8 characters
Perform
Characters
- C Program Character toupper() Example
- C Program Character Example
- C Program To Print Lines Of A Paragraph Having More Than 15 Characters
- C Program To Copy One Character Array Into Another
- C Program to Print ASCII equivalent of a character until terminated by key is ‘x’
- C Program to find the number of unique characters
- Printing ASCII Table with Numbers and corresponding characters
- C Program to Compare Two Character
- Design a C function that shortens a string to 8 characters
- File Handling (console to file) in C Program
Function
- How to pass one dimensional array to function in c.
- Write a c program which passes two dimension array to function.
- Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.
- C Program Character toupper() Example
- C Program Function Example
- Napisać funkcję obliczającą funkcję geometryczną w tablicy NxM elementowej z elementów o wartościach parzystych znajdujących się pod główną i ponad przeciwną przekątną.
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program To Convert Temperature In Celsius To Fahrenheit, Using Function
- C Program To Find Simple Interest
- C Function to Check Vowel
- Factorial Program In C Using Function
- C Program For Prime Number Using Function
- C Function to xorSwap and addSwap in C Programming
- C Program to concatenate two strings without using string functions
- C Function to read instructions from the file and obey them
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program to Implements a dictionary's functionality.
- = (int*)malloc(sizeof(int)) ??
- Design a C function that shortens a string to 8 characters
- C Program to Implements a dictionary's functionality
- C Program that prompts the user to input a string, (whitespaces allowed)
- Local sounds functions in C Program
- Function Get the resource that will be gathered from the zone name
- C Program to Find the Size of File using File Handling Function
- Average function in C
Operations
Menu Driven Program