Thread: Why does this code fail after a full stop

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    14

    Why does this code fail after a full stop

    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();
    }
    Last edited by Valour549; 10-16-2018 at 07:42 AM.

  2. #2
    Registered User
    Join Date
    Sep 2018
    Posts
    14
    Oh nevermind I figured it out xD

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Did you also figure out

    That gets() is bad.
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    That conio.h is obsolete.

    That void main is wrong.
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    > This is a program which replaces a certain word in a string, which I found here.
    Having read half a dozen, they're all awful examples.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code fail to compile in mvs but run in codeblock
    By khoavo123 in forum C Programming
    Replies: 1
    Last Post: 01-19-2012, 02:32 AM
  2. fail to count digit of an integer (fail at 9)
    By azsquall in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2008, 09:42 AM
  3. Why does this code fail?
    By Yasir_Malik in forum C Programming
    Replies: 12
    Last Post: 07-26-2007, 10:51 AM
  4. a code to filter full path
    By hanhao in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2004, 08:28 AM
  5. re:array of notation (full code)
    By lcpongkl in forum C Programming
    Replies: 2
    Last Post: 04-08-2003, 06:15 AM

Tags for this Thread