2 Replies - 1003 Views - Last Post: 23 October 2011 - 07:40 PM Rate Topic: -----

#1 bstfrmdamidaeast   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 18-September 11

How to replace a string with another string?

Posted 23 October 2011 - 06:34 PM

The code I have right now gets user input of words that are seperated by a space. I stored those words in an array just for keepsake and also it will probaby be needed later.


After the user inputs some words, the user needs to input a body of text which can be at the minimum 1 word and at the most whatever he/she likes. The way I am doing it right now is that I am putting that body of text into an array also and seperating them by a space.

The problem of the assignment is that I have to replace any of the words from the first user input, let's call them forbidden words, and find them in the body of text. After I find them in the body of text, I have to replace the words with X's filling up the length of the word and print out the body of text after it's edited. So say i have a word that is not allowed like "World", and "World" is in the my body of text : "Hello World", then the output should read "Hello XXXXX".

My problem right now is that I don't know how to proceed with this. I was thinking of going through the arrays and hten matching to see if any two arrays match, but that seemed like too much and not very efficient. I also could not replace X's. Anyone know how I could move forward?

I am reaching the final stretch of my assignment, so any help would be greatly appreciated.
Here is the Code:

#include <stdio.h>
#include <string.h>
/* #include <conio.h> */

char *mygetline(char *line, int size)
{
   if ( fgets(line, size, stdin) )
   {
      /* check for trailing '\n' */
      char *newline = strchr(line, '\n'); 
      if ( newline )
      {
         /* overwrite the '\n' with 
            a terminating null 
         */
         *newline =  '\0'; 
      }
   }
   return line;
}
 
int main(void)
{
  char text[1000] = "";
  char * pch;
  char * pbody;
  char words[100][100];
  char body[100][100];
  int numWords = 0;
  int numBody = 0;
  int i;
  /* pch = strtok (text," "); */

  fputs("Input Forbidden words: ", stdout);
  fflush(stdout);
  

  printf("text = \"%s\"\n", mygetline(text, sizeof text));

  /* Convert to words */
  for ( pch = strtok (text, " ") ;
        numWords < 100 && pch != NULL ;
        pch = strtok (NULL, " "), numWords++ )
  {
    strcpy( words[numWords], pch );
  }

  /* print array of strings */
  for(i=0; i<20; printf("%s ", words[i]), i++);
  
  
  
  fputs("Input body of text words: ", stdout);
  fflush(stdout);
  printf("text = \"%s\"\n", mygetline(text, sizeof text));
  
  for ( pbody = strtok (text, " ") ;
        numWords < 100 && pbody != NULL ;
        pbody = strtok (NULL, " "), numBody++ )
  {
    strcpy( body[numBody], pbody );
  }

  /* print array of strings */
  for(i=0; i<20; printf("%s ", body[i]), i++);
  
  getchar();

  return (0);
}



I am also on AIM:bstfrmdamideast, so if you want to hit me up on there and are willing to help, that works also.

This post has been edited by bstfrmdamidaeast: 23 October 2011 - 06:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to replace a string with another string?

#2 bstfrmdamidaeast   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 18-September 11

Re: How to replace a string with another string?

Posted 23 October 2011 - 06:56 PM

this is a C program by the way.
Was This Post Helpful? 0
  • +
  • -

#3 bstfrmdamidaeast   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 18-September 11

Re: How to replace a string with another string?

Posted 23 October 2011 - 07:40 PM

If anyone can help, please let me know. I may not be available in the next couple of hours, but i will be available for contact at like 9am EST.

This post has been edited by bstfrmdamidaeast: 23 October 2011 - 07:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1