Thread: C ...String replacement... command line arguments

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    C ...String replacement... command line arguments

    Hello,
    I am writing a program that uses command line argument for string replacement:
    a sample run of the program should be like:
    programname.out I like studying like hate
    outputs:
    I hate studying.

    here is the code I wrote :
    It is giving Segmentation fault error.
    could you please modify the code for me so it works.
    thanks
    Code:
    #include <stdio.h>
    #include <string.h>
    int main (int argc,char *argv[]) {
    char *p, *find, *replace;
    
    p = argv[1];
    find =argv[2];
    replace =argv[3];
    size_t len=strlen(p);
    size_t s_len=strlen(find);
    size_t r_len=strlen(replace);
    char *current=p;
    while ((current = strstr(p,find))) {
    memmove(current + r_len , current + s_len,len - (current - p) - s_len + 1);
    memcpy(current, replace, r_len);
    }
    ++p;
    printf("%s\n",argv[1]);
    }
    Additional Details
    This is another version that does not use command line arguments...
    It works fine :
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main () {
    
    char str[1000];
    char search[10];
    char replace[10];
    gets(str);
    gets(search);
    gets(replace);
    size_t len = strlen(str);
    size_t s_len = strlen(search);
    size_t r_len = strlen(replace);
    char *current = str;
    while (current = strstr(current, search)) /*assignment*/ {
    memmove(current + r_len , current + s_len,len - (current - str) - s_len + 1);
    memcpy(current, replace, r_len);
    ++current;
    }
    printf("%s",str);
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Can't figure out what's needed from your post so provide more explanation.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,676
    Spammed here as well

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Quote Originally Posted by rags_to_riches View Post
    Spammed here as well
    well I thought my chances of getting answers would be more...

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Well then how about providing a clear-cut explanation.

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well aside from the total lack of indentation, the solution you have looks to be 10x more complex than it needs to be. However I suspect the root of your problems is less in the replacement code and more in how you are interpreting the command line arguments. If you are on Linux (or other NIX-like system) I would suggest using getopt() to handle the command line arguments and also use quotes to separate them. In your example you are just doing:

    progname I like studying like hate

    But that will result in 5 command line arguments, one for each space-delimited token. Something like:
    progname "I like studying" like hate

    will work better since the arg 'I like studying' will be treated as a single item...with getopt() it makes it even cleaner so you can do:

    progname --source "I like studying" --search key --replace hate
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It works fine here, IF you give it three command line arguments. If you don't....

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,676
    Quote Originally Posted by andrew1400 View Post
    well I thought my chances of getting answers would be more...
    That is rude, and takes for granted the time spent by others here to help you when you're already getting help somewhere else. Which is why I try to let people know when I see this happening; because I am trying to be considerate of other people's time and effort.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Quote Originally Posted by rags_to_riches View Post
    That is rude, and takes for granted the time spent by others here to help you when you're already getting help somewhere else. Which is why I try to let people know when I see this happening; because I am trying to be considerate of other people's time and effort.

    I appreciate the help and time of people who r helping me here...I guess u r the one who is being rude!

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Quote Originally Posted by jeffcobb View Post
    Well aside from the total lack of indentation, the solution you have looks to be 10x more complex than it needs to be. However I suspect the root of your problems is less in the replacement code and more in how you are interpreting the command line arguments. If you are on Linux (or other NIX-like system) I would suggest using getopt() to handle the command line arguments and also use quotes to separate them. In your example you are just doing:

    progname I like studying like hate

    But that will result in 5 command line arguments, one for each space-delimited token. Something like:
    progname "I like studying" like hate

    will work better since the arg 'I like studying' will be treated as a single item...with getopt() it makes it even cleaner so you can do:

    progname --source "I like studying" --search key --replace hate

    Thanks for your help...
    I used " " for the input string and its all good now ...

    Code:
     
    
    #include <stdio.h>
    #include <string.h>
    int main (int argc,char *argv[]) {
    char *p, *find, *replace;
    
    p = argv[1];
    find =argv[2];
    replace =argv[3];
    size_t len=strlen(p);
    size_t s_len=strlen(find);
    size_t r_len=strlen(replace);
    char *current=p;
    while ((current = strstr(p,find))) {
    memmove(current + r_len , current + s_len,len - (current - p) - s_len + 1);
    memcpy(current, replace, r_len);
    }
    ++p;
    printf("%s\n",argv[1]);
    }

  11. #11
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by andrew1400 View Post
    I appreciate the help and time of people who r helping me here...I guess u r the one who is being rude!
    No dude, cross-posting has been wrong since the beginning of the Internet. I am sorry I helped you now.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,654
    @andrew1400
    Read this
    How To Ask Questions The Smart Way
    (and the rest of it)

    Revise your opinions on why people should bother to help you in future.

    You attitude so far sucks.
    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. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 12:18 PM
  2. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM