Thread: Need Help in C programming

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

    Question Need Help in C programming

    Hello,
    I am completely new to C programming.
    I am writing a c program to Prompts the user (altogether 10 times) to enter a word not longer than 9 characters. And it needs the modification of each word by changing lower case to upper and then the reverse of the word is concatenated with the word.FInally , show them in unsorted way and sorted way. 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. It shows 3 errors : One is Expression synatax error, One is Type mismatch in parameter 'dest' and 'src'.
    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)
    {
       char words[10][19];
       char c;
       int i, j;
       int k;
       char swap;
    
       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';
             } 
          }
       }
    
       for ( i = 0; i < 10; i++ )
       {
          for ( j = 0; j < 10; j++ )
          {
             k = (int)(words[i][j]);
             if ( k > 64 && < 91 )
             {
                words[i][j]= tolower(words[i][j] );
             }
    
             else if ( k >96 && <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,words[j]);
                strcpy(words[j],words[j+1]);
                strcpy(words[j+1],swap);
             }
          }
          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 01:40 PM. Reason: Dave's Beautify-O-Matic(tm) for a limited time only.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unreadable code. If you expect any help, you need to indent the code so people can read it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
             if ( k > 64 && k < 91 )
    Code:
             else if ( k >96 && k <123 );
    Code:
       char swap;
       /* ... */
             if ( strcmp(words[j],words[j+1])>0 )
             {
                strcpy(swap,words[j]);
                strcpy(words[j],words[j+1]);
                strcpy(words[j+1],swap);
             }
    You need more than a single character to hold a string.
    [edit]
    Code:
             if ( words[i][j] == '\n' )
             {
                words[i][j] = '\0';
                break; /* add this? */
             } /* end of if */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
             if ( k > 64 && < 91 )
             {
                words[i][j]= tolower(words[i][j] );
             }
    
             else if ( k >96 && <123 );
             {
                words[i][j]= toupper(words[i][j] );
             }
    It would read better to not use hardwired numbers, and to be completely portable ctype.h provides very nice character testing functions, like: isupper, and islower.
    Code:
             if ( isupper(k) )
             {
                words[i][j]= tolower(words[i][j] );
             }
    
             else if ( islower(k) );
             {
                words[i][j]= toupper(words[i][j] );
             }
    A common mistake here:
    Code:
             if ( strcmp(words[j],words[j+1])>0 )
             {
                strcpy(swap,words[j]);
                strcpy(words[j],words[j+1]);
                strcpy(words[j+1],swap);
             }
    The 'swap' variable can not hold a string, because you declare it as char. Therefore you overwrite memory there on the stack destroying the values of 'k', 'i', 'j' and 'c'. strcpy doesn't know it is bad to write there, but 'swap' can only hold at least 1 byte.
    Code:
    char toupper(char let)
    {
       int d;
       d = (int)let;
       let = (char)d-32;
       return let;  /* All of the above statements can become: return let-32; */
    }
    
    char tolower(char let)
    {
       int d;
       d = (int)let;
       let = (char)d+32;
       return let;
    }
    Unnecessary copying and casting. It makes only confusion in the function body.
    You copy the char to an int, and then cast the int to char at the next statement.
    Sometimes things can be confusingly simple. If you need to calculate 100-44, would you write 100 on another paper, then write 100 on the solution paper which already has 100 written on it, and then put 44 below 100 and do the subtraction? Think about that a bit.
    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.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    Thanks alot for helping me!!!
    it's really help

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    ctype.h also provides functions called toupper() and tolower(). If you really feel the need to write your own (non-portable) functions to do this, it would be a good idea to name them something else.

Popular pages Recent additions subscribe to a feed