Nicolus Buck
Nicolus Buck

Reputation: 21

Converting lower case letters to upper case letters error : Getting just ( in the result

I am learning C . I have written a program which asks the user to enter their name and print-outs their name in Upper case , for example :- if a user enters Stack Overflow as his name , it print-outs STACK OVERFLOW . But when-ever I run the program it gives just ( as the output , Can you please figure it out whats wrong with the code ? I am using Code::Blocks (version 13.12) .

Here is my code :-

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char name[25] ;
    char name_up[25] ;
    int i ;

    fputs("Enter your name : " , stdout) ;
    fgets(name , 24 , stdin) ;

    for(i=0 ; i<strlen(name) ; i++)
    {
        if(name[i] == '\n')
        {
            break ;
        }
        else
        {
            if(islower(name[i]))
            {
                name[i] = toupper(name_up[i]) ;
            }
            else
            {
                name[i] = name_up[i] ;
            }
        }
    }

    printf("Entered name is : %s .\nEntered name in upper case is : %s ." , name , name_up) ;
    return 0;
}

Upvotes: 0

Views: 124

Answers (3)

Ivan Ivanovich
Ivan Ivanovich

Reputation: 946

change for(i=0 ; i<strlen(name) ; i++) to for(i=0 ; name[i] != '\n' ; i++) and delete block:

    if(name[i] == '\n')
    {
        break ;
    }

change it:

        if(islower(name[i]))
        {
            name[i] = toupper(name_up[i]) ;
        }
        else
        {
            name[i] = name_up[i] ;
        }

to:

        if(islower(name[i]))
        {   
            name_up[i] = toupper(name[i]) ;
        }
        else
        {   
            name_up[i] = name[i] ;
        }

Oh yes, add the line #include <ctype.h>

yeah, forgot to add a terminating null to the end of name_up after "for loop" add this: name_up[i] = '\0'; and may be: name[i] = '\0';

Upvotes: 1

David C. Rankin
David C. Rankin

Reputation: 84579

Well, you are missing string.h and ctype.h header files and you had your variables reversed:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main()
{
    char name[25] = {0};      /* initializing string arrays to null (zero) insures they */
    char name_up[25] = {0};   /* are null-terminated for strings less than 25 chars     */
    int i ;

    fputs("Enter your name : " , stdout) ;
    fgets(name , 24 , stdin) ;

    for(i=0 ; i<strlen(name) ; i++)
    {
        if(name[i] == '\n')
        {
            name[i] = 0;      /* strip newline from end of name    */
            name_up[i] = 0;   /* do not include newline in name_up */
            break ;
        }
        else
        {
            if(islower(name[i]))
            {
                name_up[i] = toupper(name[i]) ;    /* You assign name_up = name    */
            }                                      /* you had it reversed...       */
            else
            {
                name_up[i] = name[i] ;
            }
        }
    }

    printf("Entered name is : %s.\nEntered name in upper case is : %s.\n" , name , name_up) ;
    return 0;
}

output:

$./bin/chgcase
Enter your name : StackOverflow
Entered name is : StackOverflow.
Entered name in upper case is : STACKOVERFLOW.

Note: Always initialize your variables (including arrays). That will save you lots of grief in the long run. Also, always compile with Warnings enabled. That will show you where the areas of concern are in your code. E.g.:

gcc -Wall -Wextra -o bin/chgcase changecase.c

Upvotes: 3

Backs
Backs

Reputation: 330

you have to change the positions of name[i] and name_up[i]. Otherwise you would override the name array with the empty name_up array.

else
    {
        if(islower(name[i]))
        {
            name_up[i] = toupper(name[i]) ;
        }
        else
        {
            name_up[i] = name[i] ;
        }
    }

Upvotes: 0

Related Questions