This is a program which replaces a certain word in a string, which I found here.
But when I'm playing around with it, I found that it fails to replace anything which is followed by a full stop. So any words that are meant to be replaced fails to be replaced if said word is at the end of a sentence. I've looked through the code and can't understand why?
Code:
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char text[100],word[10],rpwrd[10],str[10][10];
int i=0,j=0,k=0,w,p;
clrscr();
printf("PLEASE WRITE ANY TEXT.\n");
printf("GIVE ONLY ONE SPACE AFTER EVERY WORD\n");
printf("WHEN COMPLETE PRESS Ctrl-Z \n");
gets(text);
printf("\nENTER WHICH WORD IS TO BE REPLACED\n");
scanf("%s",word);
printf("\nENTER BY WHICH WORD THE %s IS TO BE REPLACED\n",word);
scanf("%s",rpwrd);
p=strlen(text);
for (k=0; k<p; k++)
{
if (text[k]!=' ')
{
str[i][j] = text[k];
j++;
}
else
{
str[i][j]='\0';
j=0; i++;
}
}
str[i][j]='\0';
w=i;
for (i=0; i<=w; i++)
{
if(strcmp(str[i],word)==0)
strcpy(str[i],rpwrd);
printf("%s ",str[i]);
}
getch();
}