Wednesday 6 February 2013

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



#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

No comments:

Post a Comment