Thread: strstr Function: Search and Replacing New String

  1. #1
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112

    strstr Function: Search and Replacing New String

    LOOK!
    Code:
    #include<stdio.h>
    #include<string.h>
    #define MAX 25
    
    int main(void)
    {   
        int ch;
        
        char *str[] = {
                      "You Know",
                      "That i learning programming",
                      "Because im interested",
                      "And its cool",
                      "When i get confused for debugging",
                      "After i succeed, i feel relieved",
                      "So i will learning and using it",
                      "Even i have no life"
                      };
        
        int i;
        char str1[MAX];
        char str2[MAX];
        char *strloc;
        
           printf("%s",str[3]);
           printf("\nNow Choose Available String in [3]:\n");
           fgets(str1,MAX,stdin);
           printf("Then Insert The New String:\n");
           fgets(str2,MAX,stdin);
           
           char *p1,*p2;
           if ((p1 = strchr(str1,'\n')) != NULL) *p1 = '\0';
           if ((p2 = strchr(str2,'\n')) != NULL) *p2 = '\0';
           
           strloc = strstr(str[3],str1);
           strncpy(strloc,str2,strlen(str2));
           
           while((ch = getchar()) != '\n' && ch != EOF);
           getchar();
           return 0;
    }
    this is from exercise Let Us C book i get from online
    the objective located the available string in line and replacing with other string
    Example:
    char str[5] = "After i succeed, i feel relieved";
    if "succeed" string is replacing with new string like "failed"
    the new line has to be
    char str[5] = "After i failed, i feel relieved";

    im surfing the internet c string reference and i got the wonderful function in string.h that is
    strstr() function
    which located the same string and with
    strncpy() function
    the string will changed

    but seems strstr() function must using string constant (i use *strloc) in this case.

    that's the problem
    i get confused with line

    strloc = strstr(str[3],str1)

    str[3] was a string constant based on array, the declaration are *str[] which related to multidimensional array.

    i think that cause this program to be error.
    because when i using the reference from internet, the program was executed for sure.
    This code what i mean

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX 10
    
    int main ()
    {
      int ch;
      char str[] ="This is a simple string";
      char str1[MAX];
      char str2[MAX];
      
      printf("Insert The Same String:\n");
      fgets(str1,MAX,stdin);
      printf("Insert The Replacing String:\n");
      fgets(str2,MAX,stdin);
      
      char *p1,*p2;
      if ((p1 = strchr(str1,'\n')) != NULL) *p1 = '\0';
      if ((p2 = strchr(str2,'\n')) != NULL) *p2 = '\0';
      
      char *pch;
      pch = strstr(str,str1);
      strncpy (pch,str2,strlen(str2));
      puts(str);
      
      while((ch = getchar()) != '\n' && ch != EOF);
      getchar();
      return 0;
    }
    and rather than that i didnt have anymore idea to make that replacing string.
    anyhelp will appreciated
    Thank You!

    PS: This is only exercise for me, not a homework, so lets discuss this matter if u want to

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    this is from exercise Let Us C book i get from online
    I guess that explains why it's so terrible. Try reading K&R2.

    im surfing the internet c string reference and i got the wonderful function in string.h that is
    strstr() function
    which located the same string and with
    strncpy() function
    the string will changed
    You can implement strstr with strncpy or without it.

    but seems strstr() function must using string constant (i use *strloc) in this case.
    The 'const's that appear in strstr's parameter list simply guarantee that strstr won't modify any elements through either of the pointers you pass. You don't need to make your own types const -- they'll be converted to the correct pointer type automatically when you make the call.

  3. #3
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    I guess that explains why it's so terrible. Try reading K&R2.
    why terrible? let us c book is bad ?

    The 'const's that appear in strstr's parameter list simply guarantee that strstr won't modify any elements through either of the pointers you pass. You don't need to make your own types const -- they'll be converted to the correct pointer type automatically when you make the call.
    yeah, on a minute before i reading your reply, i realize that string constant cannot be changed or modified, i guess i will passing all string *str[] into the new array string like this.

    char ss[MAX];
    int i;
    for(i = 0; i < 7; i++)
    ss = str[i];


    but it still had weakness just by looking at it

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    why terrible? let us c book is bad ?
    Sure is.

    You know you can't assign to arrays right?

  5. #5
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Quote Originally Posted by Barney McGrew View Post
    You know you can't assign to arrays right?
    yeah, i always using strcpy() function to assigning the string and whats wrong with the book by that ?

    oh, im sorry. if u mean the

    char ss[MAX];
    int i;
    for(i = 0; i < 7; i++)
    strcpy(ss,*str[i]);


    i forget to using strcpy() .
    The weakness i mean is that the ss only contain single string once the loop is incremented its replaced on the other string. That's the weakness i mean.
    Last edited by loserone+_+; 09-23-2013 at 03:40 AM.

  6. #6
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    String, String, why don't you give me a call?
    String, String, the happiest sound of them all
    String, String, I stare at the phone on the wall
    And I sit all alone impatiently
    Won't you please understand the need in me
    So, String, String, why don't you give me a call?
    So, String, String, why don't you give me a call?

  7. #7
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    i would say swing swing rather than string - string

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linear search function accessing string in a struct
    By thekid in forum C Programming
    Replies: 11
    Last Post: 04-08-2013, 09:09 AM
  2. String search - strstr
    By nailer in forum C Programming
    Replies: 4
    Last Post: 06-03-2011, 12:56 PM
  3. binary search function on string array
    By gandolf989 in forum C Programming
    Replies: 6
    Last Post: 10-02-2007, 01:47 PM
  4. function for Search string
    By nurulsiddik in forum C Programming
    Replies: 4
    Last Post: 04-17-2003, 03:38 AM
  5. Replies: 10
    Last Post: 03-19-2003, 02:15 PM