Thread: Issues printing out a string array

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Issues printing out a string array

    Hi there,

    I have a small issue when I am printing out an array from a user inputted string. The programs function is to have the user input a string and convert all the lower case letters to uppercase without using the toupper function but a self created one. This all works fine but when i print out the array in sequence it always starts with the @ symbol. Is there something that I am doing incorrectly?

    Code:
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int checkupper(char*s);
    
    int main(void)
    {
    
       // declare the user char arrays to store the strings
        char user_x1[256] ;
        char convert;
        int length;
        int i,e;
        int ar_size;
        
        printf("Enter a String: ");
        fgets( user_x1, 256, stdin );  //fgets (char string, int size, file *stream
        length = strlen(user_x1);
        printf("%d",length);
        ar_size=length-1;
        printf("\n\nlength of the string: %d\n",length);
        printf("\n\narray size of the string: %d\n",ar_size);
    
        system("cls");
          
          for(i=0;i<=ar_size;i++) //for loop to CALL FUNCTION TO CONVERT TO UPPER
             {
               convert = checkupper(user_x1);
             }
        printf("User converted string: %c",user_x1);
              for(i=0;i<=ar_size;i++) 
             {
               printf("%C ",user_x1[i]);
             }        
             
             
         
    
        system("pause");
        return 0;
    }
    
    int checkupper(char*con)
        {
           while(*con)*con>96&*con<123?*con-=32:0,con++;
        }
    Thanks in advance.....

  2. #2
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    i couldn't follow the logics of your program. you basically had a for loop that iterated through a char array and upon each iteration you sent the char array to checkupper that still iterated through the char array once again? consider the following.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int checkupper(char*s);
    void converttoupper(char * c);
    
    int main(void)
    {
    
       // declare the user char arrays to store the strings
        char user_x1[256] ;
        //char convert;
        int length;
        //int i,e;
        //int ar_size;
    
        printf("Enter a String: ");
        fgets( user_x1, 256, stdin );  //fgets (char string, int size, file *stream
        length = strlen(user_x1);
        //printf("%d",length);
        //ar_size=length-1;
        //printf("\n\nlength of the string: %d\n",length);
        //printf("\n\narray size of the string: %d\n",ar_size);
    
        system("cls");
        user_x1[length] = '\0';
        converttoupper(user_x1);
        printf("%s\n", user_x1);
    
    /*
          for(i=0;i<=ar_size;i++) //for loop to CALL FUNCTION TO CONVERT TO UPPER
             {
               convert = checkupper(user_x1);
             }
        printf("User converted string: %c",user_x1);
              for(i=0;i<=ar_size;i++)
             {
               printf("%C ",user_x1[i]);
             }
    
    
    */
    
        //system("pause");
        getchar();
        return 0;
    }
    
    void converttoupper(char * c)
    {
        while(*c)
        {
            if(123 > (int)(*c) && (int)(*c) > 96)
                *c -= 32;
            c++;
        }
    
        return;
    }
    
    int checkupper(char*con)
        {
           while(*con)*con>96&*con<123?*con-=32:0,con++;
        }
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Thanks for that...I was just getting confused with my coding. I see where I went wrong and with your additional code i understand what happened...

    rgds

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Nasty...

    Code:
           while(*con)*con>96&*con<123?*con-=32:0,con++;
    Better...

    Code:
           while(*con)
              { if ((*con > 96) & (*con < 123 )
                      *con-=32 ;
                 con++; }

  5. #5
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Quote Originally Posted by CommonTater View Post
    Nasty...
    Code:
           while(*con)
              { if ((*con > 96) & (*con < 123 ) //whoops
                      *con-=32 ;
                 con++; }
    i assume u meant && :P
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Checkupper is not returning any value.
    Actually it should not be called checkupper at all isn't it?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by codeprada View Post
    i assume u meant && :P
    Hmmmm.... Yep, looks like I did ....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM