Thread: lower to upper

  1. #1
    Unregistered
    Guest

    lower to upper

    This is a program that prints 5 user inputs of characters. I want to change the user input to upper case using toupper. I have tryied to place the function in all possible lines but it does not work

    I also want to print the output in 2 columns....how can i do that??

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

    int main (void)
    {
    int k,cnt;
    char line[81];
    char *t[5];

    for (k=0; k<5; k++)
    {
    gets(line);
    if feof(stdin) break;
    t[k]=(char*) malloc(1+strlen(line));
    if (t[k]==NULL)
    {
    printf("out of storage");
    break;
    }
    strcpy(t[k], line);
    }

    cnt=k;
    printf("count=%d\n",cnt);

    for (k=0; k<cnt; k++)
    {
    printf("%d %s\n", k,t[k]);
    free(t[k]);
    }
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    for ( i = 0 ; i < strlen(line) ; i++ ) line[i] = toupper( line[i] );

  3. #3
    Unregistered
    Guest
    but where do i put it in my program???

    That's my problem!!!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Somewhere between reading the line in, and printing it out

    > strcpy(t[k], line);
    It really doesn't matter whether its before or after this (for example) - either will work and both are equally valid solutions

  5. #5
    Unregistered
    Guest
    I put it after strcpy....

    but it still print in lower case!!!!

  6. #6
    Unregistered
    Guest
    Here are my latest codes:


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

    int main (void)
    {
    int k,i,cnt;
    char line[81];
    char *t[5];

    for (k=0; k<5; k++)
    {
    gets(line);
    if feof(stdin) break;
    t[k]=(char*) malloc(1+strlen(line));
    if (t[k]==NULL)
    {
    printf("out of storage");
    break;
    }
    strcpy(t[k], line);

    for(i=0;i<strlen(line);i++) //<<<============
    line[i]=toupper(line[i]);
    }

    cnt=k;
    printf("count=%d\n",cnt);

    for (k=0; k<cnt; k++)
    {
    printf("%d %s\n", k,t[k]);
    free(t[k]);
    }
    return 0;
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    mmm - try before then

  8. #8
    wherethehellismypw
    Guest
    I think maybe the problem is that the second for loop is nested inside the first...so you are toupper()ing each char and then you are replacing it with another line read from gets()...or something like that.

    fyodor

  9. #9
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    here...

    PHP Code:
    void capitalize (char input[20])
    {
        
    int i;
        for (
    i=0input[i]; i++)
        {
            if (
    input[i]>='a' && input[i]<='z')
            {
                
    input[i]=input[i]-32;
            }
        }



  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The problem with unreg's latest code is simply that the string is copied from the line variable to the newly malloc'ed memory, but then the toupper() function makes the line variable upper case. The copy of the in the new memory is left untouched. Hence Salem's comment:
    >mmm - try before then
    meaning, convert the string to uppercase BEFORE copying it to the new memory.

    Doing this:
    >input[i]=input[i]-32;
    to make something upper case is not guaranteed to work. Just because ASCII has it's upper and lower case characters 32 apart, doesn't mean that other character sets do too. EBCDIC for example, is completely different.

    This is why you should stick to the standard library functions for doing this type of thing.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    std

    by standard, do you mean using "toupper" ?
    are people still using EBCDIC ?

    how do we know which functions are standard and which are not ? 'cause i want to write code that runs on most compilers (if not all of them)
    i can't compile some programs in unix (gcc). it compiles
    well with borland/turbo c++, etc..

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >by standard, do you mean using "toupper" ?
    Yes.

    >are people still using EBCDIC ?
    Yes. It's not "old fashioned", it's just format that isn't used in the "home environment" or the PC world, so a lot of people don't come in contact with it. An IBM mainframe will use it for example (it was after all, design by IBM).

    >how do we know which functions are standard and which are not ?
    Find a good website or reference book that details each functions compatability. A standard function will conform to one of the ANSI standards.

    >i can't compile some programs in unix (gcc). it compiles well with borland/turbo c++, etc
    Then the chances are you're using something non-standard.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if (input[i]>='a' && input[i]<='z')
    {
        input[i]=input[i]-32;
    }
    Depending on your character set, this may be invalid. If you are going to write your own, you have to assume you know the way your character set works. If you don't, then your code may or may not work.

    Assuming that the alphabet is displayed from a to z, numericly incrementing, you can do:

    Code:
    int alpha( int a )
    {
        if( a >= 'a' && a <= 'z' )
        {
            return a+('a'<'A'?('A'-'a'):-('a'-'A'));
        }
        return a;
    }
    That should do the trick.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. to lower or to upper that is the question!
    By verbity in forum C++ Programming
    Replies: 20
    Last Post: 04-25-2007, 06:42 PM
  3. help...Lower and Upper case problem only
    By j4k50n in forum C Programming
    Replies: 9
    Last Post: 04-19-2007, 12:39 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. upper case to lower case problem
    By Jasonymk in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 05:35 AM