Argv to string?

This is a discussion on Argv to string? within the C Programming forums, part of the General Programming Boards category; I'm trying to write a program that will find a substring within a main string, and I'm pretty sure I ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Argv to string?

    I'm trying to write a program that will find a substring within a main string, and I'm pretty sure I can do it, if I can just figure out how to take the command line arguments (argv[1] & argv[2]) and turn them into normal strings.

    But I'm having trouble doing that, don't even know where to begin, really. I've been trying to us sscanf and read the characters one by one into a char array...but I don't think it's gonna work out, since sscanf doesn't clear the buffer at all...so i'd be reading from the beginning of the string each time. Doh!

    Can anyone point me in the right direction? Is there a better function to use?

    NOTE: I can't use any of the string.h defined functions.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by azrael View Post
    I'm trying to write a program that will find a substring within a main string, and I'm pretty sure I can do it, if I can just figure out how to take the command line arguments (argv[1] & argv[2]) and turn them into normal strings.
    The compiler's already done it for you by setting-up argv[] as an array of pointers to char strings.
    Quote Originally Posted by azrael View Post
    But I'm having trouble doing that, don't even know where to begin, really. I've been trying to us sscanf and read the characters one by one into a char array...but I don't think it's gonna work out, since sscanf doesn't clear the buffer at all...so i'd be reading from the beginning of the string each time. Doh!

    Can anyone point me in the right direction? Is there a better function to use?

    NOTE: I can't use any of the string.h defined functions.
    A place to start would be coming up with an algorithm for coding this task.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    well, I wanted to make the array I create more malleable, so I could run though it simply than using argv or something.
    I'm not all that good at pointers...Does this work? It compiles okay..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define STR_LENGTH 50
    
    int main(int argc, char *argv[]) 
    {
    	char phraseOne[STR_LENGTH], phraseTwo[STR_LENGTH];
    
    		int i = 0, k = 0, counter1 = 0, counter2 = 0;
    
    	while(argv[1][i]!='\0')
    	{
     		phraseOne[i] = argv[1][i]; // Copy the content of the second array
    		i++;
    		counter1++;
    	}
    	
    	while(argv[2][k]!='\0')
    	{
     		phraseTwo[k] = argv[2][k]; // Copy the content of the second array
    		k++;
    		counter2++;
    	}
    
    	return 0;
    }
    EDIT: changed for loops...made it stop when the main string is null..
    Last edited by azrael; 03-12-2009 at 11:19 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Quote Originally Posted by azrael
    well, I wanted to make the array I create more malleable, so I could run though it simply than using argv or something.
    What do you mean by "more malleable"?

    Quote Originally Posted by azrael
    Does this work?
    Only in special cases. You need to copy the characters from argv[1] and argv[2] until and including the null character. You may find it handy to use strncpy() instead of coding your own loop, but you would need to insert the null character yourself to account for the cases where the number of characters in argv[1] and argv[2] is not less than STR_LENGTH.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    I just didn't want to be fumbling with the argv thing...Seemed easier to copy it to its own string.

    I just changed the loops to work until its null. Is that better?

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I just didn't want to be fumbling with the argv thing...Seemed easier to copy it to its own string.
    If it's really that important to you, you could declare a new pointer and set it equal to argv[2], for instance. Since all of the string functions work by receiving the pointer instead of the array itself, that should be plenty.

    Of course, there's no reason you can't strcpy() the contents of the array pointed to by argv to another array you decalred yourself. There's also no reason I can think of based on what you've said why you would need to do that.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Sorry for the delay, my network connection seems to be fluctuating.

    Quote Originally Posted by azrael
    I just didn't want to be fumbling with the argv thing...Seemed easier to copy it to its own string.
    If you do not want to write argv[1] and argv[2], then just use pointers:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        if (argc > 2)
        {
            char *phraseOne = argv[1];
            char *phraseTwo = argv[2];
    
            printf("%s %s\n", phraseOne, phraseTwo);
            /* ... */
        }
    
        return 0;
    }
    Note that you can use array notation with pointers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Can't use strcopy. Not supposed to use string.h functions for this assignment.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Quote Originally Posted by azrael
    I just changed the loops to work until its null. Is that better?
    If you really want to copy over you need to loop until either the current character is null, or the maximum length of the destination string has been reached. Frankly, don't bother copying, and instead concentrate on your actual task. If you write a function to complete your task, writing argv[1] and argv[2] is hardly going to be confusing since the bulk of the work with the strings would be done with the function where the parameters are appropriately named.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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