Find and replace Strings.

This is a discussion on Find and replace Strings. within the C Programming forums, part of the General Programming Boards category; Hey, I am a novice in C Programming. And I have this program as an exercise, but I am wrong ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    15

    Find and replace Strings.

    Hey, I am a novice in C Programming. And I have this program as an exercise, but I am wrong somewhere in the program. I know it must a poo-poo but can't figure out what it is.

    Code:
    /* Program to find if string 'str1' exists in string 'str' and replace the found characters with 'str2' */
    #include<stdio.h>
    #include<string.h>
    int main(){
      char *str[]={
        "We will teach you how to...",
        "Move a mountain",
        "Level a building",
        "Erase the past",
        "Make a million",
        ".. all through C"
      };
      char str1[25],str2[25]; 
      int i=0,j=0,k=0,n;
      printf("\n\n");
      scanf("%s %s",&str1,&str2);
      n=strlen(str1);
      
      for(i=0;*str[i]!='\0';i++,j++){
        if(*str[i]!=str1[j]){
          continue;
        }
        else if(*str[i+1]==str1[j+1] && *str[i+2]==str1[j+2]){
          for(k=0;k<n;k++,i++){
    	*str[i]=str2[k];
          }
          break;
        }
      }
      printf("\n\n");
      for(i=0;*str[i]!='\0';i++){
        printf("%c",*str[i]);
      }
      return 0;
    }
    This is the output:
    Code:
    We
    you
    Segmentation fault
    'We' is str1;
    'you' is str2;

    Even my logic could be completely wrong. I am embarrassed. Help me out.

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    First off, there's a mistake in this line:

    Code:
    scanf("&#37;s %s",&str1,&str2);
    Should be:

    Code:
    scanf("%s %s",str1,str2);
    No need for the & specifier when dealing with arrays - if you were reading in characters or numbers into single variables it would be different, e.g.

    Code:
    int number = 0;
    scanf ("%d", &number);
    Although you really shouldn't be using scanf to read in strings - fgets is much safer - see my sig for an example of how to use fgets.

    Secondly, you really need to look at your loop more closely:

    Code:
    for(i=0;*str[i]!='\0';i++,j++){
        if(*str[i]!=str1[j]){
          continue;
        }
    If you enter the word "We" to be stored in str1, take a look at where the individual characters of that word go:

    Code:
    str1[0] = 'W'
    str1[1] = 'e'
    str1[2] = '\0'
    In your loop code you use the variable j to access the individual elements of str1. However, you should not access any elements after str1[2]. You should insert an extra check so that you do not go past the termination character '\0' in str1. Something involving comparing j against the value n should be useful.

    Also, you might want to look at comparisons involving *str[i]. When you declared str you actually created an array of strings, such that str[i] will access the i-th string. Try out something like the following to see my point:

    Code:
    printf ("%s\n", str[3]); /*Should print 'Erase the past'*/
    However, when you add the * symbol in front of it, you only get the first character of string number i. Again for demonstration, try this:

    Code:
    printf ("%c\n", *str[3]); /*Should print 'E' only*/
    Personally I like to think of arrays of strings as 2d arrays of characters, so I might do something like this to go through each element of the array:

    Code:
    for (i = 0; i < 6; i++) /*The number 6 is here because we happen to have declared an array of 6 strings*/
    {
        for (j = 0; str[i][j] != '\0'; j++)
        {
            /*Insert code to compare current character with characters being sought*/
        }
    }
    That should point you in the right direction at least.

    Lastly, you probably want make your code more general, so that you can replace words other than We. At the moment your attempt appears to try to find the first character in str that matches up with the first character of str1, and if it does, it checks the next 2 characters. It would probably be nicer to use a loop for this bit, if you can figure out a way to do that.
    Last edited by Richie T; 08-01-2008 at 11:35 PM. Reason: Clarification on array of strings as a 2d array of characters
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    char *str[]=

    should be
    Code:
    const char *str[]
    because this is array of pointer to the string literals that could not be changed

    why you do not use functions like strcmp?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find and replace within a string?
    By clancyPC in forum C Programming
    Replies: 3
    Last Post: 03-20-2009, 08:28 AM
  2. Replies: 10
    Last Post: 06-10-2008, 03:17 AM
  3. please help!...find and replace
    By amy589 in forum C++ Programming
    Replies: 26
    Last Post: 10-13-2006, 07:42 PM
  4. Replies: 5
    Last Post: 05-25-2004, 05:36 PM
  5. How to replace strings in a file?
    By Doh in forum C Programming
    Replies: 6
    Last Post: 11-26-2002, 12:16 PM

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