C program

Write a program that will read a line and delete from it all occurences of the word 'the'

Questions by vishv

Showing Answers 1 - 1 of 1 Answers

tuxman

  • Dec 22nd, 2010
 

#include<stdio.h>
#include<string.h>
main()
{    
    char str[100],substr[4];
    int i,j,k,l,m;
    printf("Enter the string:");
    gets(str);
    for(i=0;i<(strlen(str)-2);i++)
    {
        k=i;
        for(j=0;j<3;j++)
        {
            substr[j]=str[k];
            k++;        
        }
        substr[3]='';
        if((strcmp(substr,"the")==0 && str[i-1]==' ' && str[i+3]==' ') || (strcmp(substr,"the")==0 && str[i-1]==' ' && str[i+3]=='') || (strcmp(substr,"the")==0 && i<1 && str[i+3]==' '))
        {
            printf("The detectedn");
            m=i;
            for(l=0;l<3;l++)
            {
                str[m]=' ';
                m++;        
            }
        }            
    }
    printf("The new string is "%s"",str);
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions