Searching arrays for strings

This is a discussion on Searching arrays for strings within the C++ Programming forums, part of the General Programming Boards category; I have only just found this board, and I've been impressed with what I have experienced. I hope people don't ...

  1. #1
    Zaarin
    Guest

    Searching arrays for strings

    I have only just found this board, and I've been impressed with what I have experienced. I hope people don't mind my questions too much.

    My problem is that I want to search an array for a string. The aim is to have the user type in an array and string and have the program search the array specified for the string specified, then return what row it is in.

    eg. an array called office[11][16] = {{"Pen"},{"Chalk"},{"Biro"},{"Crayon"},{"Ruler"},{ "Eraser"},{"Stapler"},{"Paper"},{"Whiteout"},{"Glu e"},{"Pencil"}};

    The user enters "office" (as the array to search) and "Chalk" as the string to find. The progam then displays that it was found in row 1 etc.
    ============================================
    The following is what I have, I'm aware that it is not really close to what it should be.

    any help/pointer would be appreciated.
    ============================================

    #include <iostream.h>
    #include <ctype.h>

    //declare variables
    //=================

    //misc. variables
    int position = 0;
    char array[16] = "";
    char string[16] = "";

    //loop controls
    int x = 0;
    int y = 0;

    //the array
    char office[11][16] = {{"Pen"},{"Chalk"},{"Biro"},{"Crayon"},{"Ruler"},{ "Eraser"},{"Stapler"},{"Paper"},{"Whiteout"},{"Glu e"},{"Pencil"}};

    //function prototypes
    //===================
    int LookFor(char, char);

    //functions
    //=========
    void main()
    {
    cout << "Enter the name of the array you wish to search: ";
    cin.getline(array, 16, '\n');
    cout << endl << "Enter the string you wish to find: ";
    cin.getline(string, 16, '\n');

    LookFor(array[16], string[16]); //call function
    }

    int LookFor(char array, char string)
    {
    while (y < 11)
    {
    while (x < 16)
    {

    if (office[x][y] == string)
    {
    position = x;
    cout << "Found string: " << string << " in" << " Row: " << position << endl;
    return 1;
    }
    else
    {
    }
    x = x++;
    }
    y = y++;
    x = 0;
    }
    cout << "If no data is listed above, then no match was found." << endl << endl;
    return 0;
    }

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    you form is ok but here are some pointers and problems
    1. x and y shouln't be global they should be decleared in the scope needed to use them.

    2.if (office[x][y] == string) is impossible use
    if(strcmp(office[x],string) == 0)

    #inlcude <string.h>
    int strcmp( const char *string1, const char *string2 )

    is a string comparison function from the C standard library
    it returns

    < 0 string1 less than string2
    0 string1 identical to string2
    > 0 string1 greater than string2

    and as such the y variable becomes unneccessary for this purpose
    and the array parameter in lookfor is never used so it is eliminated from the function...
    also a for loop could be better utilized here but i will leave it with the while to prevent more confusion...

    heres what lookfor should look like


    Code:
    int LookFor(char* string) 
    {
        int x = 0;
        while (x < 11) 
        { 
            if(strcmp(office[x],string) == 0) 
            {
                cout << "Found string: " << string << " in" << " Row: " <<  x << endl; 
                return 1; 
            } 
            else 
            {
                x++; 
            }
         }
    }
    hope this is helpful and not confusing...
    Last edited by no-one; 08-29-2001 at 12:22 AM.

  3. #3
    Unregistered
    Guest
    Thankyou very much no-one

  4. #4
    Zaarin
    Guest

    A bit more help please

    That helped me a great deal, but I've got to allow the user to enter the array name to search as well (I realise that there is only one option though).

    Here's my current code (with your modifications):

    #include <iostream.h>
    #include <string.h>

    //declare variables
    int position = 0;
    char array[16] = "";
    char string[16] = "";

    char office[11][16] = {{"Pen"},{"Chalk"},{"Biro"},{"Crayon"},{"Ruler"},{ "Eraser"},{"Stapler"},{"Paper"},{"Whiteout"},{"Glu e"},{"Pencil"}};

    //function prototypes
    int strcmp( const char *string1, const char *string2 );
    int LookFor(char*, char*);

    //functions
    void main()
    {
    cout << endl << "Enter the array you wish to search (case sensitive): ";
    cin.getline(array, 16, '\n');
    cout << endl << "Enter the string you wish to find (case sensitive): ";
    cin.getline(string, 16, '\n');

    LookFor(array, string); //call function
    }

    int LookFor(char *array, char *string)
    {
    int x = 0;
    while (x < 11)
    {
    if(strcmp(office[x], string) == 0)
    {
    cout << "Found string: " << string << " in" << " Row: " << x << endl;
    return 1;
    }
    else
    {
    x++;
    }
    }
    return -1;
    }

    =========
    In the line:
    if(strcmp(office[x], string) == 0)

    is it possible to have office use the array variable? I've tried, but it gives me a "error C2664: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast".

    As you probably guessed I'm kind of new to C++

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    If you have the line:

    #include <string.h>

    at the top of the file you don't need to have the line

    int strcmp( const char *string1, const char *string2 );

    in your program as it is there automatically by virtue of the process set in motion with the include line above. Therefore delete the above line. If that doesn't abort the error then you may need to change the process of filling the array of strings. but I think the first suggestion will work.

    You can return the value of x from LookFor() if the string is found and return the value -1 if it isn't. Remember that x represents the index of the string in the array. Index 0 is the first element of the array and index 1 is the second element of the array. Therefore, you may need to do some adjusting depending on how you wish to use the information.

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    guest is right on the first part...
    #include <string.h>
    instead of prototyping the function sorry for the confusion.

    there are a few possibilities here

    "error C2664: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'

    possible causes

    where parameter 1 is Array

    1. Array is a char not a char*
    2. Array is supposed to be a char and your not using the adress operator to pass the adress of the char.
    3. or your adressing a specific character in the array such as with Array[0] or such...

    whats the code this happens on?

    >is it possible to have office use the array variable?

    that depend on what you want to do with it...
    how do you want it used?

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    the variable called array is superfluous if you wish to search the array of strings called office for a user entered string. Here's a modified version of the code that should work.

    #include <iostream.h>
    #include <string.h>

    //declare variables
    int position = 0;
    char string[16] = "";

    char office[11][16] = {{"Pen"},{"Chalk"},{"Biro"},{"Crayon"},{"Ruler"},{ "Eraser"},{"Stapler"},{"Paper"},{"Whiteout"},{"Glu e"},{"Pencil"}};

    //function prototypes
    int LookFor(char*, char*);


    int main()
    {
    cout << endl << "Enter the string you wish to find (case sensitive): ";
    cin.getline(string, 16, '\n');

    position = LookFor(office, string); //call function and get return
    //you want to search office, not array, for string so pass office to
    //LookFor

    //analyze position
    if(position == -1)
    {
    cout << "the string " << string << " is not in the list." << endl;
    }
    else if(position > -1 && position < 11)
    {
    cout << "the string " << string << " is in position " << (x + 1) << " of the list." << endl;
    }

    return 0;
    }

    int LookFor(char *office, char *string)
    {
    int x = 0;
    while (x < 11)
    {
    if(strcmp(office[x], string) == 0)
    {
    return x;
    }
    else
    {
    x++;
    }
    }
    return -1;
    }

  8. #8
    Zaarin
    Guest
    What I want, as shown in the following code, is for the user to be able to enter BOTH the array to seach AND the string to search for.

    For example: I've only made an array called office, so that will be the only option at this stage. But the user has to enter the name of the array to search, and the string to search for:

    I want the line:
    if(strcmp(office[x], string) == 0)

    to be something like:
    if(strcmp(array[x], string) == 0)

    so that it uses the name of the array that the user previously entered.

    Thankyou guys, for you help so far...

    ================================
    The code:

    #include <iostream.h>
    #include <string.h>

    //declare variables
    int position = 0;
    char array[16] = "";
    char string[16] = "";

    char office[11][16] = {{"Pen"},{"Chalk"},{"Biro"},{"Crayon"},{"Ruler"},{ "Eraser"},{"Stapler"},{"Paper"},{"Whiteout"},{"Glu e"},{"Pencil"}};

    //function prototypes
    int LookFor(char*, char*);

    //functions
    void main()
    {
    cout << endl << "Enter the array you wish to search (case sensitive): ";
    cin.getline(array, 16, '\n');
    cout << endl << "Enter the string you wish to find (case sensitive): ";
    cin.getline(string, 16, '\n');

    LookFor(array, string); //call function
    }

    int LookFor(char *array, char *string)
    {
    int x = 0;
    while (x < 11)
    {
    if(strcmp(office[x], string) == 0)
    {
    cout << "Found string: " << string << ", in" << " Row: " << x << endl;
    return 1;
    }
    else
    {
    x++;
    }
    }
    return -1;
    }

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    why not just use strstr(). This finds a string within a string.... its in string.h or cstring
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    What I want, as shown in the following code, is for the user to be able to enter BOTH the array to seach AND the string to search for.
    You could create an array of structs -

    struct x
    {
    char array[x][y];
    char arrayname[z];
    }arrays[noOfArrays];

    Then when the user enters the array name to search you can loop through the array of x's until you find a match with arrayname and then search for the string in the actual array.

  11. #11
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    ..or for another alternative you could look up STL map.

  12. #12
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    so what your saying is look for "string" in this "array"?

    so that it will find the array(office for example) to search for the arguement string?

  13. #13
    Zaarin
    Guest
    Yes that's right no-one. I want the array name to be a variable, so that the user can enter which array to search.

  14. #14
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    ok got ya... i'll be back with a good way ASAP... i unfortunatly
    have a spliting headache now and i think my eyes are gonna pop out so i can't write good code when im like this i can't even see straight. so i'll probably have it tommorow... sorry.

  15. #15
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    sorry this took so long ive been real busy...

    the absolute best way you can do this is with an
    STL map nothing you will find will accomplish this better so...

    but i must apologize again because i don't know enough about the STL map to explain it to you but if you make a post about using the STL map im sure the better versed C++ programmers could help you understand it better than i could.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching Multiple Arrays?
    By Cell in forum C Programming
    Replies: 3
    Last Post: 03-19-2009, 04:45 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  5. Searching and Scanning Arrays
    By apoc632 in forum C Programming
    Replies: 7
    Last Post: 11-28-2003, 03:28 PM

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