To replace a word by another word in a given string

Thursday, December 4th, 2008

#include<stdio.h>
#include<conio.h>
int stlen(char str[50])
{
int len = 0;
while(str[len]!=’\0′)
len++;
len–;
return len;
}
void stcat(char str1[50], char str2[50])
{
int i = 0,len = 0;
while(str1[len]!=’\0′)
len++;
while(str2[i]!=’\0′)
{
str1[len] = str2[i];
i++;
len++;
}
str1[len] = ‘\0′;
}
void main()
{
char str1[50], str2[50], str3[50], temp[50];
int len1, len2, len3, i, j, match, k;
clrscr();
printf(“\n\n\t ENTER A SENTENCE…: “);
gets(str1);
len1 = stlen(str1);
printf(“\n\n\t ENTER A STRING WHICH YOU WANT TO DELETE…: “);
gets(str2);
len2 = stlen(str2);
printf(“\n\n\t ENTER A NEW STRING WHICH YOU WANT TO INSERT …: “);
gets(str3);
len3 = stlen(str3);
for(i=0;i<=len1;i++)
{
match = 1;
for(j=0;j<=len2;j++)
if(str2[j]!=str1[i+j])
{
match = 0;
break;
}
if(match)
{
for(k=0,j=i+len2+1;j<=len1;j++,k++)
temp[k] = str1[j];
temp[k] = ‘\0′;
for(j=0;j<=len3;j++)
str1[i+j] = str3[j];
str1[i+j] = ‘\0′;
stcat(str1,temp);
len1 = len1 – len2 +len3;
i = i + j;
}
}
printf(“\n\n\t OUTPUT IS…: “);
puts(str1);
getch();
}
Download exe and source code here.

Avatar of Ranjith

Author Name :
Ranjith

Total : 6 Comments


6 Responses to “To replace a word by another word in a given string”

  1. soha sheikh says:

    mujhe koi ye program smjha do,means every step kaise kaise ho rha ek array box example me le k plz plz!
    Q-wap input a string then find out occurence of each character?
    void main()
    {
    int i ,j,k,c;
    printf(“enter a string”);
    gets(a);
    for(i=0;i<strlen(a);i++)
    {
    c=1;
    for(j=i+1;j<strlen(a);j++)
    {
    c++;
    for(k=j;k<strlen(a);k++)
    a[k]=a[k+1];
    j–;
    }
    }
    printf("\n%c=%dtimes",a[i],c);
    }
    getch();
    }

  2. faras raoof says:

    buddy, try “debugging” using the “watch” option .if you are using turbo C, go to menu bar, select debugger , add “watch” values and have the pleasure of learning it yourself :)

  3. faras raoof says:

    #include
    #include
    #include
    void main()
    {
    int k=0,i,j,l,t;
    char a[20],b[20],c[20],d[20];
    char name[5][6];
    clrscr();
    printf(“enter the main string\n”);
    gets(a);
    printf(“enter the word to be deleted\n”);
    gets(b);
    printf(“enter the word to be replaced with\n”);
    gets(c);
    l=strlen(a);
    for(i=0;i<l;i++)
    {
    j=0;
    while(a[i]!=' '&& a[i]!='')
    {
    d[j]=a[i];
    i++;
    j++;
    }
    d[j]='';
    strcpy(name[k],d);
    k++;
    }
    for(k=0;k<5;k++)
    {
    t= strcmp(name[k],b);
    if(t==0)
    {
    strcpy(name[k],c);
    }
    }
    for(k=0;k<5;k++)
    {
    printf(" %s",name[k]);
    }
    getch();
    }

  4. faras raoof says:

    and by the way , that program uses the concept of string as a two dimensional array . the limitation is that you got to decide the maximum number of letters in the word and the exact number of words in the sentence at the beginning of the program ……in the above case, being 5 words(exactly) and maximum of 6 letters in the word.

  5. Ganeshkumar says:

    sir,
    i need c program to delete substing like following string1=”concatenate”,string2=”cat”
    string2 ll be deleted from string1 so result should be string1=”conenate”.can u give me the coding for thos sir…

  6. dinu says:

    please give a better idea about a program to replace two words

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: