sorting and revise problem

This is a discussion on sorting and revise problem within the C Programming forums, part of the General Programming Boards category; Hello! I have a question about to determine the word not longer than 9 characters. And it needs the modification ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    Question sorting and revise problem

    Hello!
    I have a question about to determine the word not longer than 9 characters. And it needs the modification of each word by changing lower case to upper (this part is ok working) and then the reverse of the word is concatenated with the word- i can only print out the word. it cant show like (my input : Chester , out FInally RETSEHccHESTER) , show them in unsorted way and sorted way by lexico-graphic ascending order - it is not working too. After that, the program terminates. I have made many attempts at writing this program and this is where i am at right now and it still doesnt work. Thanks in advance for the input.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char toupper(char);
    char tolower(char);
    
    int main(void)
    {
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char toupper(char);
    char tolower(char);
    
    int main(void)
    {
       char words[10][19];
       char c;
       int i, j;
       int k;
       char swap[10][19];
    
    while ( j != 10)
    {
       for ( i = 0; i < 10; i++ )
       {
    
          printf("enter the character \n");
          fflush(stdout);
    
          for ( j = 0; j < 10; j++ )
          {
    
             words[i][j] =fgetc(stdin);
             if ( words[i][j] == '\n' )
             {
                words[i][j] = '\0';
                break;
             }
           }
         }
          printf(" you have entered more than 9 characters! \n"); 
     }
    
       for ( i = 0; i < 10; i++ )
       {
          for ( j = 0; j < 10; j++ )
          {
             k = (int)(words[i][j]);
             if ( k >= 64 && k <= 91 )
             {
                words[i][j]= tolower(words[i][j] );
             }
    
             else if ( k >= 96 && k<= 123 )
             {
                words[i][j]= toupper(words[i][j] );
             }
          }
       }
    
       printf("unsort \n");
    
       for ( i = 0; i < 10; i++ )
       {
          printf("&#37;s \n ", words[i]);
          fflush(stdout);
       }
    
    printf("Sorted: \n");  
       for ( i=9; i>0; i-- )
       {
          for ( j=0; j<i; j++ )
          {
             if ( strcmp(words[j],words[j+1])>0 )
             {
                strcpy(swap[j],words[j]);
                strcpy(words[j],words[j+1]);
                strcpy(words[j+1],swap[j]);
             }
          }
          printf("%s \n", words[i]);
      } 
     
        return 0;
    }
    
    
    char toupper(char let)
    {
       int d;
       d = (int)let;
       let = (char)d-32;
       return let;
    }
    
    char tolower(char let)
    {
       int d;
       d = (int)let;
       let = (char)d+32;
       return let;
    }
    Last edited by liukinhei; 03-02-2008 at 03:01 PM.

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    I can only see that C seems to be confusing you seriously.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    Quote Originally Posted by xuftugulus View Post
    I can only see that C seems to be confusing you seriously.
    so i need to code this like
    Code:
    j: exit (10)
    printf(" you have entered more than 9 characters");
    is it like this??

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    You don't have to use 2-d array. First, reverse the word by swapping char at first/last index and increase/decrease by 1 until both indexes meet. And make memory space using malloc(2*strlen(arrary)+1) and insert the reverse word and the original word in proper position. Then call a function to make it uppercase.

Popular pages Recent additions subscribe to a feed

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