Separate long string into multiple arrays

This is a discussion on Separate long string into multiple arrays within the C Programming forums, part of the General Programming Boards category; I have a program to write where a user enters up to 10 strings, ending the input with -1 to ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    26

    Separate long string into multiple arrays

    I have a program to write where a user enters up to 10 strings, ending the input with -1 to let the program know the user is finished. Then takes those strings and compares them to remove duplicates, then prints the individual strings as one entire string with each individual string separated by a white space.

    My problem is with the separation of the strings. with gets() you can get one of the individual strings and store it into an array.. And the next call of gets() should pick up where the last one left off, but I don't really know the most efficient way to do so. You can easily get each individual string using a for loop but I'm not sure how to increment the arrays so that each run of the loop stores a string to a different array.

    Any suggestions on how to approach it?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you need a two-dimensional array. And a for-loop that exits when the entered string is -1.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I think there's a terminology problem here. If I may, I will rewrite your need with the proper terms:

    Quote Originally Posted by cashmerelc
    I have a program to write where a user enters up to 10 strings, ending the input with -1 to let the program know the user is finished. The program then takes those strings and compares them to each other to remove duplicates, then prints the individual strings as one contiguous string with each individual string separated by a white space.

    My problem is with the separation of the strings. With gets() you can get one of the individual strings and store it into an element of the array.. And the next call of gets() should pick up where the last one left off, but I don't really know the most efficient way to do so. You can easily get each individual string using a for loop but I'm not sure how to increment the array index so that each run of the loop stores a string to a different array element.
    If this is correct, then a single dimension array is all that is needed.

    Todd

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But a string in C (as opposed to C++) is an array in itself. If you want to compare strings to see if they are equal, then you need multiple strings, and the only what to make that efficiently is by a 2D array [or, put another way, an array of strings - where a string in itself is an array].

    Oh, and use fgets() instead of gets(). gets() is the most famous "broken if the user is naughty/ignorant/clumsy" function in the whole of the C-library. I forgot to say that in the first place.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Yes, you are correct. I'm still getting used to the C concept of a string as an array of characters (or, I guess it's the other way around.)

    Code:
    char userdata[10][12] ;
    would be 10 arrays of 12 bytes each. A nice little 2D array.

    Todd

    EDIT: and in my rewrite above, "element" would be the inner (second) arrary of 12 bytes each.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Okay, I have written out some code that puts together several small strings into one long one, but the -1 character only seems to have an effect if you happen to make the -1 the 1st, 2nd, 3rd, 6th, etc string. And my duplicate elimination is not working whatsoever. Below is my incredibly long and probably unnecessary code.

    Code:
            fgets(one, i, stdin);
            nl = strrchr(one, '\n');
            if (nl) *nl = '\0';
            if (atoi(one) == -1){
                    printf("\n");
                    return 0;
            }
    
            fgets(two, i, stdin);
            nl = strrchr(two, '\n');
            if (nl) *nl = '\0';
            if (two[50] == one[50]){
                    two[50] = NULL;
            } else if (atoi(two) == -1){
                    printf("%s \n", one);
                    return 0;
            }
    
            fgets(three, i, stdin);
            nl = strrchr(three, '\n');
            if (nl) *nl = '\0';
            if (three[50] == two[50] || three[50] == one[50]){
                    three[50] = NULL;
            } else if (atoi(three) == -1){
                    printf("%s %s \n", one, two);
                    return 0;
            }
    
            fgets(four, i, stdin);
            nl = strrchr(four, '\n');
            if (nl) *nl = '\0';
            if (four[50] == three[50] || four[50] == two[50] || four[50] == one[50]){
                    four[50] = ' ';
            } else if (atoi(four) == -1){
                    printf("%s %s %s \n", one, two, three);
                    return 0;
    
            }
    
    
            fgets(five, i, stdin);
            nl = strrchr(five, '\n');
            if (nl) *nl = '\0';
            if (five[50] == four[50] || five[50] == three[50] || five[50] == two[50] || five[50] == one[50]){
                    five[50] = ' ';
            } else if (atoi(five) == -1){
                    printf("%s %s %s %s \n", one, two, three, four);
                    return 0;
    
            }
    
            fgets(six, i, stdin);
            nl = strrchr(six, '\n');
            if (nl) *nl = '\0';
            if (six[50] == five[50] || six[50] == four[50] || six[50]== three[50] || six[50] == two[50] || six[50] ==
    one[50]){
                    six[50] = ' ';
            } else if (atoi(six) == -1){
                    printf("%s %s %s %s %s \n", one, two, three, four, five);
                    return 0;
    
            }
    
    
            fgets(seven, i, stdin);
            nl = strrchr(seven, '\n');
            if (nl) *nl = '\0';
            if (seven[50] == six[50] || seven[50] == five[50] || seven[50] == four[50] || seven[50] == three[50] ||
    seven[50] == two[50] || seven[50] == one[50]){
                    seven[50] = ' ';
            } else if (atoi(seven) == -1){
                    printf("%s %s %s %s %s %s \n", one, two, three, four, five, six);
                    return 0;
    
            }
    
            fgets(eight, i, stdin);
            nl = strrchr(eight, '\n');
            if (nl) *nl = '\0';
            if (eight[50] == seven[50] || eight[50] == six[50] || eight[50] == five[50] || eight[50] == four[50] ||
    eight[50] == three[50] || eight[50] == two[50] || eight[50] == one[50]){
                    eight[50] = ' ';
            } else if (atoi(eight) == -1){
                    printf("%s %s %s %s %s %s %s \n", one, two, three, four, five, six, seven);
                    return 0;
    
            }
            fgets(nine, i, stdin);
            nl = strrchr(nine, '\n');
            if (nl) *nl = '\0';
            if (nine[50] == eight[50] || nine[50] == seven[50] || nine[50] == six[50] ||
     nine[50] == five[50] || nine[50] == four[50] || nine[50] == three[50] || nine[50] ==
    two[50] || nine[50] == one[50]){
                    nine[50] = ' ';
            } else if (atoi(nine) == -1){
                    printf("%s %s %s %s %s %s %s %s \n", one, two, three, four, five, six, seven, eight);
                    return 0;
    
            }
    
            fgets(ten, i, stdin);
            nl = strrchr(ten, '\n');
            if (nl) *nl = '\0';
            if (ten[50] == nine[50] || ten[50] == eight[50] || ten[50] == seven[50] || ten[50] == six[50] || ten[50] ==
    five[50] || ten[50] == four[50] || ten[50] == three[50]
    || ten[50] == two[50] || ten[50] == one[50]){
                    ten[50] = NULL;
            } else if (atoi(ten) == -1){
                    printf("%s %s %s %s %s %s %s %s %s \n", one, two, three, four, five, six, seven, eight, nine);
                    return 0;
     } else {
                    printf("%s %s %s %s %s %s %s %s %s %s \n", one, two, three, four, five, six, seven, eight, nine, ten);
    }
    
    }
    Also, the final printf (I thought) would just go ahead and print all ten strings, but if you just enter 10 strings without -1 as the 10th string, the program simply ends. Any ideas?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, that's not using a 2D array and a for-loop, is it? What if you were told that ten strings is not enoug, we need 1000 strings? That would become such a mess that you could barely read it, right?

    The right solution is to use an array of strings, aka a 2D char array that is ten deep on the left index, and however wide you need your max string length [apparently 50 at the moment] to be. Then you can do the whole program in about 10 sparse lines. And it would not matter if you do 5, 10, 100 or 1000000 words [it would obviously take slightly longer to read 1000000 words, and comparing them as you go along.

    By the way, you need to use strcmp to compare your strings. I'm assuming your strings are 50 long, you are also accessing the 51st element of a 50 long array - not a good idea.

    --
    Mats
    Last edited by matsp; 11-27-2007 at 01:59 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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