Printing string arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tango Charlie
    New Member
    • Mar 2007
    • 3

    Printing string arrays

    Anyone who can help,

    I"m starting to pull my hair out about this...I had to use scanf to read in the strings. I have been working on this lil project for 3 days and can't figure out how to print the strings after they have been swapped. Mine goes a lil something like this...

    printf("\nThese are the names that you entered: \n\n");
    for (i = 0; i < N_STRINGS; ++i)
    printf("%s\n", s[i]);
    for (i = 0; i < N_STRINGS; ++i)
    for (k = i + 1; k < N_STRINGS; ++k)
    if (strcmp(s[i], s[k]) == 1){
    swap(s[i], s[k]);
    }
    printf("\nThe names that you entered are arranged alphabeticaly: \n\n");
    for (i = 0; i < N_STRINGS; ++i)
    printf("%s\n", s[i]);
    printf("\n");
    return 0;
    }

    void swap(char *p, char *q)
    {
    char tmp = *p;

    *p = *q;
    *q = tmp;
    }

    The first char of each string are the only things alphabetized; I need the whole string swapped. Please help!!
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    Originally posted by Tango Charlie
    Anyone who can help,

    I"m starting to pull my hair out about this...I had to use scanf to read in the strings. I have been working on this lil project for 3 days and can't figure out how to print the strings after they have been swapped. Mine goes a lil something like this...

    printf("\nThese are the names that you entered: \n\n");
    for (i = 0; i < N_STRINGS; ++i)
    printf("%s\n", s[i]);
    for (i = 0; i < N_STRINGS; ++i)
    for (k = i + 1; k < N_STRINGS; ++k)
    if (strcmp(s[i], s[k]) == 1){
    swap(s[i], s[k]);
    }
    printf("\nThe names that you entered are arranged alphabeticaly: \n\n");
    for (i = 0; i < N_STRINGS; ++i)
    printf("%s\n", s[i]);
    printf("\n");
    return 0;
    }

    void swap(char *p, char *q)
    {
    char tmp = *p;

    *p = *q;
    *q = tmp;
    }

    The first char of each string are the only things alphabetized; I need the whole string swapped. Please help!!
    In function swap try using strcpy();
    char * temp = malloc (your Max size)
    strcpy(temp,p);
    strcpy(p,q)
    strcpy(q,temp)
    free temp;

    Comment

    • Tango Charlie
      New Member
      • Mar 2007
      • 3

      #3
      Originally posted by svlsr2000
      In function swap try using strcpy();
      char * temp = malloc (your Max size)
      strcpy(temp,p);
      strcpy(p,q)
      strcpy(q,temp)
      free temp;
      Thanks a million! I couldn't get the swap() to work so I inserted the strcpy() in its place. The following worked well...

      Code:
      printf("\nThese are the names that you entered: \n\n");
      	for (i = 0; i < N_STRINGS; ++i)
      		printf("%s\n", s[i]);
      	for (i = 0; i < N_STRINGS; ++i)
      		for (k = i + 1; k < N_STRINGS; ++k)
      			if (strcmp(s[i], s[k]) == 1){
      				strcpy_s(temp[k], s[i]);
      				strcpy_s(s[i], s[k]);
      				strcpy_s(s[k], temp[k]);
      			}
      	printf("\nThe names that you entered are arranged alphabeticaly: \n\n");
      	for (i = 0; i < N_STRINGS; ++i)
      		printf("%s\n", s[i]);
      		printf("\n");
      	return 0;
      Thanks again, svlsr!
      Last edited by sicarie; Mar 26 '07, 05:23 PM. Reason: Added '/' in final [/code] tag

      Comment

      Working...