two dimensional arrays-name swap

This is a discussion on two dimensional arrays-name swap within the C Programming forums, part of the General Programming Boards category; I'm trying to make a program takes the input to a name such as : Doe, John...and then have it ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    5

    two dimensional arrays-name swap

    I'm trying to make a program takes the input to a name such as :
    Doe, John...and then have it have John Doe at the output. Any help would be appreciated. I'm using turbo C as a compiler and here is what I have thus far......thanks

    #include <stdio.h>
    #include <string.h>
    void pause(void);
    int main()
    {
    char word[30][21];
    char reverse[30][21];
    int length
    int w1count;
    char run;
    char c;

    clrscr();



    printf("The following program will reverse a name ");
    printf("the user enters.\n\n");
    printf("Do you want to run this program? Y for yes, N for no.\n\n");
    scanf("%c", &run);

    if(run == 'n' || run == 'N')
    {
    return 0;
    }
    else

    clrscr();

    printf("Please enter a name. Ex. Doe, John.\n");




    for (count = 0 ; ; )
    {
    scanf("%s", word[count]);
    length = strlen(word[count]) - 1;
    c = word[count][length];
    if ((c != ','))
    count++;
    else
    break;
    }

    clrscr();
    printf("The original name entered was: \n\n");

    for (count = 0 ; ; )
    {
    printf("%s ", word[count]);
    length = strlen(word[count]) - 1;
    c = word[count][length];
    if ( (c != ',')) )
    count++;
    else
    break;
    }



    printf("\n\nThe modified name is: \n\n");

    count = 0;

    again:
    length = strlen(word[count]) - 1;

    c = word[count][length];
    if ( (c !=',')) )

    {
    reverse[count][length] = word[count][length];

    for (w1count = 1 ; w1count <= length ; w1count++)
    {
    reverse[count][w1count - 1] = word[count][length - w1count];
    printf("%c", reverse[count][w1count - 1]);

    }

    printf("%c ", reverse[count][length]);

    }

    else

    {

    ((reverse[0][0] >= 'a') && (reverse[0][0] <= 'z'))
    reverse[0][0] = reverse[0][0] - 32;
    printf("%c", reverse[count][w1count]);
    }
    }

    c = word[count][length];

    if (( c != '.') && (c != '?') && (c != '!'))
    {

    count++;

    printf(" ");

    goto again;
    }

    count++;

    printf("\n\nPress any key to continue...");
    for (;
    if ( kbhit() )
    break;
    getch();
    }

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    This post looks hauntingly familiar. Dunno what you're trying to do with that code or if this code works, but give it a shot.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      char name[2][60], *delim;
      int i, j, index;
    	
      j=0;
    	
      printf("Enter name (last, first): ");
      fgets(name[0], 60, stdin);
    	
      for(i=0; name[0][i] != '\0'; i++)
        if(name[0][i] == '\n')
          name[0][i] = '\0';
    			
      if((delim = strchr(name[0], ',')) == NULL)
      {
        printf("ERROR: Deliminator [,] is missing from %s\n", name[0]);
        return -1;
      }	
    	
      i=0;	
      index = delim - name[0] + 2;	
    	
      while(name[0][index] != '\0')
        name[1][j++] = name[0][index++];	
    	
      name[1][j++] = ' ';
    		
      while(name[0][i] != ',')
        name[1][j++] = name[0][i++];
    	
      name[1][j] = '\0';	
    	
      printf("\nold name: %s\n", name[0]);
      printf("new name: %s\n", name[1]);
    	
      return 0;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    5

    thanks!

    hey thanks a lot for that ! that helped me out soo much...but one last question if you will how would i go about storing 25 names and making it able to have each name capable of 50 characers long...i get the idea, just not the know how.... strings ? or what. thanks for the help thus far

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: thanks!

    Originally posted by slimbwoozha
    hey thanks a lot for that ! that helped me out soo much...but one last question if you will how would i go about storing 25 names and making it able to have each name capable of 50 characers long...i get the idea, just not the know how.... strings ? or what. thanks for the help thus far
    Simply change the dimensions of the array you're using.

    [number of names][length of name]

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    i tried increasing the array values...to [30] and [60],....but do you know how i would go about asking the user how many names they would like to type in at the beginning of the program...then wouldn't i have to do some other kind of loop regarding how many names the user wants to enter? thanks

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Dynamic allocation could be used. I don't believe C allows non constants to be used in static arrays, but someone in the know should be able to give you a more definitive answer.

    HTH
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    sorry ronin...i'm just not following you at all. sorry - this is just a hmwk assignment i'm working on and i can't seem to get the jist of it ..

  8. #8
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Perhaps it is I that didn't understand your question.

    Do you mean that you created a static array that your user doesn't change, but just fills it up to x location?

    Code:
    char names[50][80];
    int count, maxnames;
    
    printf("how many names to enter: ");
    scanf("%d%*c", &maxnames);
    
    for(count=0; count < maxnames; count++)
         fgets(names[count], 80, stdin);
    Is that it?
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    i'm not the best programmer in the world- haha well i'm not quite sure what a static array is...but i guess to put it in "stupid" terms i'm just trying to get a program that can have 25 names...and 50 characters long...then the user would type in how many names they want to enter....then after they type in say 5 for example..then type -1 to get out of the loop...and the screen would clear.....then the screen would display all the names they way the user entered then (doe, john) and then a press any key at the bottom...once they pressed any key the next thing to pop up would show all the names in reversed order... (john doe) i guess i'm just having trouble making all this "click" together in my head as to how to make it work correctly

  10. #10
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Hey!! don't doubt yourself. I don't know what I'm doing 90% of the time, so that's why I come here... to learn.

    Ok, here's another part to play with.

    Code:
    #include <stdio.h> 
    #include <string.h>
    
    #define MAXNAMES 25
    #define MAXLEN 50 
    
    
    int main(void)
    {
       char names[MAXNAMES][MAXLEN];  /* size is known */
       int count, numNames, i;
       
       printf("A maximum of %d names can be entered\n", MAXNAMES);
       printf("How many names would you like to enter: ");
       scanf("%d%*c", &numNames);
       
       for(count = 0; count < numNames; count++)
       {
          printf("Enter name %d (-1 quits): ", count+1);      
          fgets(names[count], MAXLEN, stdin); 
          
          if(strncmp(names[count], "-1", 2) == 0)
             break; 
          
          for(i=0; names[count][i] != '\0'; i++)
          	if(names[count][i] == '\n')
          		names[count][i] = '\0';
       } 
       
       i = 0;
       
       while(i < count)
       {
          printf("names [%d] = %s\n", i, names[i]);
          i++;
       }  
       
       return 0;
    }
    I *think* this is similar to what you need in terms of entry.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  2. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 03:28 PM
  3. passing two dimensional arrays
    By Nova_Collision in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2003, 01:47 PM
  4. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 09:12 PM
  5. Two dimensional arrays
    By Jax in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 12:53 PM

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