Sie sind auf Seite 1von 4

#include <stdio.

h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
void menu();
void pattern();
void spaces();
void words();
void stat();
void replace();

char string[]="";
char str1[50];

int main()
{
while(1)
{
printf("Enter any Sentence (Blank line to finish)\n");
fflush(stdin);
gets(str1);
if(!(*str1))
break;
strcat(string,str1);
strcat(string,". ");
}
string[strlen(string)-1]='\0'; //To remove the last " " character

menu();
return 0;
}

void menu()
{
while(1)
{
printf("Press any key to continue....\n");
getch();
system("cls");
printf("********************************************************\n");
printf("%s\n",string);
printf("********************************************************\n");
int choice=0;
printf("1. Search for a pattern in Paragraph\n");
printf("2. Count the number of (gaps or spaces) in Paragraph\n");
printf("3. Count number of (words) in Paragraph\n");
printf("4. Count number of (statements) in Paragraph\n");
printf("5. Find and Replace a substring\n");
printf("6. Exit\n");
fflush(stdin);
scanf("%d",&choice);
printf("********************************************************\n");
switch (choice)
{
case 1: pattern();
break;
case 2: spaces();
break;
case 3: words();
break;
case 4: stat();
break;
case 5: replace();
break;
case 6: exit(0);
default: printf("Enter proper Choice\n");
}
}
}

void pattern()
{
int count=0;
char str2[20];
printf("Enter the pattern you want to find\n");
fflush(stdin);
gets(str2);
int len=strlen(str2);
int j;
for(int i=0;string[i+len-1];i++)
{
int k=i; //auxiliary variable which increse by 1 on each character
match
for(j=0;j<=len-1;j++) //and reset to i if full substring is not
matched
{ //keeping i
unaffected
if(string[k]!=str2[j])
break;
k++;
}
if(j==len)
{
count++;
}
}
//checking whether substring is found or not
if(count>0)
{
printf("Substring (%s) Found\n",str2);
printf("Occurance: %d times\n",count);
}
else
printf("Pattern (%s) Not found\n",str2);
}
void spaces()
{
int i;
int space=0;
for(i=0;string[i];i++)
{
if(string[i]==' ')
{
space++;
}
}
printf("Number of spaces: %d\n",space);
}
void words()
{
int i;
int words=1;
for(i=0;string[i];i++)
{
if(string[i]==' ')
{
words++;
}
}
printf("Number of Words: %d\n",words);
}
void stat()
{
int i;
int stm=0;
for(i=0;string[i];i++)
{
if(string[i]=='.')
{
stm++;
}
}
printf("Number of statements: %d\n",stm);
}
void replace()
{
char temp[100],str2[20],str3[20];
printf("Enter the string to be replaced\n");
fflush(stdin);
gets(str2);
fflush(stdin);
printf("Enter the string to be put\n");
gets(str3);
fflush(stdin);

int index=-1,len2=strlen(str2),len3=strlen(str3);
for(int i=0;string[i];i++)
{
int k=i,j; //j need to be declared here otherwise j==len2 will not be
evaluated
for(j=0;j<len2;j++) //picking up the first char from main string
one by one
{
if(string[k]!=str2[j]) //comparing that char with first char of
str2
{
temp[++index]=string[i]; //if not matched then write
to new string, string[i]
break; //must not be confused with string[k]
because k will inc even if word match
} //partially and write forwarded char not
current char
else
{
k++; //if matched then check next char
}
}
if(j==len2) //when the substring get matched correctly (j must not be
out of scope)
{
for(int a=0;str3[a];a++)
{
temp[++index]=str3[a];
}
i=i+len2-1;
}

}
temp[++index]='\0';
strcpy(string,temp); //dest,source
printf("Modified String: \n%s\n",string);
}

Das könnte Ihnen auch gefallen