Comparing char array for two strings

This is a discussion on Comparing char array for two strings within the C++ Programming forums, part of the General Programming Boards category; I have a command line structure, and I was wondering how I could compare a char array for two separate ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    Comparing char array for two strings

    I have a command line structure, and I was wondering how I could compare a char array for two separate characters.

    So, basically:
    if ( argv[x] == 'A' && argv[x] == 'Z' )
    do_something(argv[x], argv[x]);

    I've tested that (not that \exact\ code), and it doesn't work. Well, if it does, I'm using it wrong.

    I want to pass two command line options onto another function, but can't figure out a way to check to see if one is accompanying the other so that I can send them together, because alone they won't work.

    So, how would I go about doing that? I've set up about a million for statements, and realized they were all destined for uselessness and I canít think of anything else to do.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Compare C-style strings with strcmp.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    How about a translation in the King's English? Your post may as well have been in Chinese as far as I'm concerned.
    Last edited by 7stud; 02-15-2006 at 10:20 PM.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Ah, sorry, I just re-wrote this twice to try and make it coherent.

    I just want to see if 'a' and 'z' were used when calling the program, and I was wondering if I could search for two separate characters in the same array.

    Example:
    Code:
    for ( int j = 0; j < argc; j++ )
    {
        if ( argv[j] == 'A' && argv[j] == 'Z' )
            func( argv[j+1], argv[j+1] );
        else if ( argv[j] == 'A' && argv[j] != 'Z' )
        {
            cout << endl << "You must specify some extra thing" << endl;
            help();
        }
    }
    I know the above won't work, but I'm just trying to show what I'm asking

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Please don't post paraphrased code, post actual code. Otherwise you may get paraphrased solutions rather than actual solutions.

    If argv happens to be the typical second parameter to main, you might notice that it is not a pointer to a char, but a pointer to a pointer to a char -- a pointer to a string. Comparing C-style strings is not done using ==, as mentioned previously.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I was wondering if I could search for two separate characters in the same array.
    Of course. One way would be to search the whole array for one char, and then search the whole array for the other char. But that's pretty inefficient, so during one pass of the array, you could count how many of each letter you found. If each count was greater than 1, then both letters were present in the array.

    However, as Dave_Sinkula pointed out, argv[] is an array of pointers. Furthermore, argv[0] is a pointer to the program name, and so it does not point to any user input. The pointer argv[1] is going to be the first pointer that could possibly point to user input. A pointer to a string points to the first character in the string, so you can use pointer arithmetic or array notation to access the individual characters in the string. If you know that a variable named "myVar" is a pointer to a char array, how would you access the first character in the array? Now substitute argv[1] for myVar.

    This is not going to work:
    Code:
    if( argv[j] == 'A' && argv[j] == 'Z')
    because:

    1) The value of a pointer is never going to be equal to a character. A pointer stores an address, which looks something like 8567A9D.

    2) Something cannot be equal to both 'A' and 'Z', so the if statement will always fail.

    Comparing C-style strings is not done using ==, as mentioned previously.
    I don't think anything in his original post or his revised post should be interpreted to require comparing c-style strings.
    Last edited by 7stud; 02-16-2006 at 01:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-19-2009, 11:56 AM
  2. Replies: 4
    Last Post: 03-18-2009, 06:01 PM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 08:36 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM

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