find word and insert another word after that in text

This is a discussion on find word and insert another word after that in text within the C Programming forums, part of the General Programming Boards category; Hi, I have text (string) and I want to find a given word (it's ok!) and then insert another given ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    14

    find word and insert another word after that in text

    Hi,
    I have text (string) and I want to find a given word (it's ok!) and then insert another given word after the first word.
    The original string is beeing copied into a new string.
    But something is going wrong!!!

    Where is my mistake?

    Thanks in advance...

    (I have some patches...)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    //insert "new_word" after each occurence of "word"
    int main(){
    char A[100]="In the sentence words the and the.";
    char B[200]="";
    char *word="the";
    char *new_word="com";
    char *p;
    int word_count=0;
    int i,j,word_index;
    int found,position;
    
    i=0;
    j=0;
    found=0;
    
    while(A[i]!='\0'){
       word_index=0;
       position=i;
       while(A[i]==word[word_index]){//we found same letters
          B[j]=A[i];//copy A to B letter by letter
          printf("A[%d]=%c B[%d]=%c \n",i,A[i],j,B[j]);//getchar();
          i++;j++;word_index++;//increment indices
          printf("A[%d]=%c B[%d]=%c \n",i,A[i],j,B[j]);//getchar();
          printf("A: %s\n",A);
          printf("B: %s\n",B);
       }
       if (word_index==strlen(word)) {//word was found
           found=1;
           printf("position=%d\n",position);
           printf("A[%d]=%c B[%d]=%c \n",i,A[i],j,B[j]);//getchar();
           B[j]=' ';
           j++;
           strcat(B,new_word);//copy new_word to B[] after word
           printf("A[%d]=%c B[%d]=%c \n",i,A[i],j,B[j]);//getchar();
           printf("A: %s\n",A);
           printf("B: %s\n",B);
           j=j+strlen(new_word)+1;//increment j
           B[j]=' ';
           printf("A: %s\n",A);
            printf("B: %s\n",B);
           i++;j++;
           continue;//exit while loop and continue parsing
       }
       else{
            B[j]=A[i];//else continue copying A to B
            printf("A[%d]=%c B[%d]=%c \n",i,A[i],j,B[j]);//getchar();
            printf("A: %s\n",A);
            printf("B: %s\n",B);
            i++;j++;
            }
    }
    if(!found) printf("\"%s\" not found!\n",word);
    puts(A);
    puts(B);
    getchar();
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,063
    Do you want to insert the new word or replace the old word? In other words, what is the expected ending values for A and B in your example?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    Quote Originally Posted by c99tutorial View Post
    Do you want to insert the new word or replace the old word? In other words, what is the expected ending values for A and B in your example?
    We find "the" and after each occurence of "the" we add the word "com".
    output:
    A: (the same)
    B:"In the com sentence words the com and the com."

    thanks in advance..

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,063
    If you insert "com" after every instance of "the", then shouldn't the result be like this:

    B: "In thecom sentence words thecom and thecom."

    Where does the extra space come from?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    Quote Originally Posted by c99tutorial View Post
    If you insert "com" after every instance of "the", then shouldn't the result be like this:

    B: "In thecom sentence words thecom and thecom."

    Where does the extra space come from?
    I mean that "com" is a new word..the word "the" remains the same...

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,063
    Yes, so if you find instances of "the" and insert "com" afterwards, it would do this:

    before: "In the sentence words the and the."
    after: "In thecom sentence words thecom and thecom."

    The total number of spaces in the string should remain the same, because spaces are not involved in the search and insertion strings ("the" and "com")

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    Yes, I agree with you...
    But my program doesn't work properly....where is my mistake??

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,063
    Quote Originally Posted by xiromdim View Post
    where is my mistake??
    Did you write out pseudocode first before writing the code? Did you consider a simple base case. For example, in general there are multiple times when the substring occurs. But to begin with you can consider what you need to do if it only occurs once. It will help in building an iterative solution

    Example:

    s: "The est population"
    t: ""
    needle: "est"
    insertion: "imated"

    With this, the expected output after running should have this result
    t: "The estimated population"

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    ok! I fixed it!
    I made it with pointers, whose, IMO, are better implementation for manipulating a C string...
    I'm expecting your comments....
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    //insert "new_word" after each occurence of "word"
    int main(){
    char A[100]="in the the";
    char B[200]="";
    char *word="the";
    char *new_word="com";
    char *a,*b,*w,*nw;
    int word_count=0;
    int i,j,word_index;
    int found,position;
    
    a=A;
    b=B;
    w=word;
    nw=new_word;
    found=0;
    
    while(*a!='\0'){
    	w=word;
       //position=i;
       while(*a==*w&& *a!='\0'){//we found same letters
          *b=*a;//copy A to B letter by letter
          //printf("A[%d]=%c B[%d]=%c \n",i,A[i],j,B[j]);getchar();
          a++;b++;w++;//increment indices
          //printf("A[%d]=%c B[%d]=%c \n",i,A[i],j,B[j]);getchar();
       }
       if (*w=='\0') {//word was found
           found=1;
           //printf("position=%d\n",position);
           //printf("A[%d]=%c B[%d]=%c \n",i,A[i],j,B[j]);getchar();
           *b=' ';
           b++;
           strcat(B,new_word);//copy new_word to B[] after word
           //printf("A[%d]=%c B[%d]=%c \n",i,A[i],j,B[j]);getchar();
           b=b+strlen(new_word);//increment j
           *b=' ';
           a++;b++;
           continue;
       }
       else{//we are here, that means *a!=*w, word dont match...
            *b=*a;//else continue copying A to B
            //printf("A[%d]=%c B[%d]=%c \n",i,A[i],j,B[j]);getchar();
            a++;b++;
            }
    }
    if(!found) printf("\"%s\" not found!\n",word);
    puts(A);
    puts(B);
    getchar();
       
    return 0;   
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  2. How to find the longest word in text file?
    By alionas in forum C Programming
    Replies: 7
    Last Post: 03-07-2011, 12:05 PM
  3. find word in a text file
    By 26friends26 in forum C++ Programming
    Replies: 7
    Last Post: 03-01-2010, 08:37 PM
  4. reading text-and-numbers file word by word
    By bored_guy in forum C Programming
    Replies: 22
    Last Post: 10-26-2009, 10:59 PM
  5. Help reading text file word by word
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2002, 05:13 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21