Thread: Help with multi-dimensional string arrays

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    6

    Help with multi-dimensional string arrays

    Allright, my assignment was to read in user data in the form name,age,sport. I then had to reverse the letters in the name, and then print all of the inputted data out last-first in the order sport age reversedname. For some reason my code to reverse the name string isn't doing anything. I used a multidimensional string array for each object (name, age, and sport) and I think an error with that might be why.

    Please tell me what I'm doing wrong...I've spent a I am sillyI am sillyI am sillyI am silly-ton of time on this proggy and I'm sick of it.

    Here's the section of code that should reverse the name. The full program is below it in case you want to compile it, or if I've messed up somewhere else. This does compile, btw. Thanks for any help you can give me.

    Code:
    /*Reversing the name by the use of two temp strings*/
            for(x=1; x>=i; x++){
                    char tempStr1[strlen(name[x])];
                    char tempStr2[strlen(name[x])];
    
                    for(j; j<strlen(name[x]); j++)
                    tempStr1[j] = name[x][j];
    
                    for(y=0; y<strlen(tempStr1); y++){
                            tempStr2[y] = tempStr1[strlen(tempStr1)-y];
                                     }
                    for(k; k<strlen(name[x]); k++)
                    name[x][j] = tempStr2[j];
            }

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    
            char input[50];
            char name[10][50];
            char age[10][50];
            char sport[10][50];
            int i=1, j=1,k=1;
            int x, y, z, w;
    
            /*Speaking to the user*/
            printf("Enter up to ten people  The format is name,age,sport.");
            printf("\nInvalid input will be ignored.\n");
    
    
            /*Loop for input from the user*/
            fgets(input, sizeof(input), stdin);
    
            while(strlen(input) !=1 && i<10){
                    input[strlen(input)-1] = '\0';
                    sscanf(input, "%s,%s,%s", &name[i], &age[i], &sport[i]);
    
                    /*Handling of lack of entry. If they skip something, it doesn't
                            increment the counter so it is written over*/
    
                    if(name[i] != 0 && age[i] != 0
                            && sport[i] != 0 ) i++;
                    fgets(input, sizeof(input), stdin);
    
            }
    
            printf("Thanks\n\n");
    
            /*Reversing the name by the use of two temp strings*/
            for(x=1; x>=i; x++){
                    char tempStr1[strlen(name[x])];
                    char tempStr2[strlen(name[x])];
    
                    for(j; j<strlen(name[x]); j++)
                    tempStr1[j] = name[x][j];
    
                    for(y=0; y<strlen(tempStr1); y++){
                            tempStr2[y] = tempStr1[strlen(tempStr1)-y];
                                     }
                    for(k; k<strlen(name[x]); k++)
                    name[x][j] = tempStr2[j];
            }
    
            /*Print out the info in format sport age reverseName*/
    
            for (i; i>=1; i--){
                    printf("%s %s %s\n", &sport[i], &age[i], &name[i]);
                    }
    
            /*End the program*/
            return (0);
    
    }

  2. #2
    Hello,

    The best way to accomplish something is to have blueprint of what you are working with. For instance, you are trying to reverse a string. That means you will need a temporary string. You will also need a for loop, one perferbally.

    This loop will start at the length of name - 1. Array subscripts start at 0. If a string returned 6 letters long, your loop won't stop until it hits 6, rather it needs to hit 5. If you count 0, 1, ..., 5 the answer will be 6, thus why we "- 1" from our string length. This for loop will decrease in value until it hits 0.

    While this loop runs, you will take the location of name and set it to your temporary variable. Your temporary variable location must be incremented while decrementing the for loop.

    This is a general concept. There are many topics on this, and a Forum Search can save you time.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    6
    I have the general idea down, its the specifics that is the problem.

    What I really need is someone to look at the piece of code that is the reverser (that first part) and tell me if my syntax is right. For instance, is strlen(tempstr1-1) right if it's a multi-dimensional string array? Sorry if I wasn't specific enough.
    Last edited by nsaP; 12-13-2004 at 06:16 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You have a lot of problems. First, remember that %s reads up to whitespace, not a comma. Your use of 1-based indexing will cause no end to your suffering, so you should avoid it. Come to think of it, there's too much wrong to describe it all. So I'll give you a working version to compare with:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      char input[50];
      char name[10][50];
      char age[10][50];
      char sport[10][50];
      int i=0, j,k;
      int x;
    
      /*Speaking to the user*/
      printf("Enter up to ten people  The format is name,age,sport.");
      printf("\nInvalid input will be ignored.\n");
    
      /*Loop for input from the user*/
      fgets(input, sizeof(input), stdin);
    
      while(strlen(input) != 1 && i<10){
        input[strlen(input)-1] = '\0';
        sscanf(input, "%[^,],%[^,],%[^,]", name[i], age[i], sport[i]);
    
        /*Handling of lack of entry. If they skip something, it doesn't
        increment the counter so it is written over*/
        if(name[i] != 0 && age[i] != 0
          && sport[i] != 0 ) i++;
        fgets(input, sizeof(input), stdin);
      }
    
      printf("Thanks\n\n");
    
      /*Reversing the name by the use of two temp strings*/
      for(x=0; x<i; x++){
        char tempStr1[50];
        char tempStr2[50];
    
        for (j = 0; name[x][j] != ' '; j++)
          tempStr1[j] = name[x][j];
        tempStr1[j] = '\0';
        while (name[x][j] == ' ')
          ++j;
        for (k = 0; name[x][j] != '\0'; j++, k++)
          tempStr2[k] = name[x][j];
        tempStr2[k] = '\0';
        strcpy(name[x], tempStr2);
        strcat(name[x], ", ");
        strcat(name[x], tempStr1);
      }
    
      /*Print out the info in format sport age reverseName*/
      while (--i >= 0){
        printf("%s %s %s\n", &sport[i], &age[i], &name[i]);
      }
    
      /*End the program*/
      return (0);
    }
    It's still ugly, but far more idiomatic and it actually works.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    6
    Thanks for the help, but when I run that when I hit return with nothing in it, it says "Segmentation Fault" instead of printing anything.

    Yeah, I'm a C noob. Sorry. I really hate it. Java was so much easier for me.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >it says "Segmentation Fault" instead of printing anything.
    It's entirely possible that I missed something while fixing your code (it was very broken), but testing it with two popular compilers gives me a degree of confidence. Did you compile exactly what I posted, or did you change something?
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    6
    Copy/pasting exactly and it still isn't working. Compiles fine but says that after displaying "Thanks". Thanks anyway.

    I understand everything you did but the [^,] part when reading in the file. Is there any way I could make %s work, cause that's all I know?
    Last edited by nsaP; 12-13-2004 at 06:53 PM.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Oh, duh! Change this:
    Code:
    printf("%s %s %s\n", &sport[i], &age[i], &name[i]);
    To this:
    Code:
    printf("%s %s %s\n", sport[i], age[i], name[i]);
    That's the only part I can think of off hand that would cause you problems. Either that or I need sleep.

    >Is there any way I could make %s work, cause that's all I know?
    No, not without other constructs to help.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    6
    Allright, the problem was those variables (the tempstrings) that were declared inside the code. I moved them out, and it works...kinda.

    It still doesn't reverse the name, and I get a weird character thing.

    I think I hate C.

    Here's what comes out:

    Enter up to ten people The format is name,age,sport.
    Invalid input will be ignored.
    Bob,12,soccer
    Kevin,17,football

    Thanks

    football 17 Ð*
    12 Ð*
    Last edited by nsaP; 12-13-2004 at 07:41 PM.

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    6
    Thanks guys, but I went down the hall to another guy who has the same teacher and found out I'm going about this all wrong. Using a struct is so much easier and works, lol.

    Thanks for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Pointers to multi dimensional arrays.
    By bartybasher in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2003, 02:41 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM