CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2005
    Posts
    63

    Conversion to lower to upper case problem

    Hey, I'm using C language and I want to tell you my code for a function that needs to convert lowercase to uppercase:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>


    Code:
    void convertL(FILE *fileName)
    {
        ClearScreen();
        char name[15];
    	char *p;
    	printf("\nOption #3\n\n");
    	fflush(stdin);
    	printf("Enter a file name:  ");
        fgets(name, sizeof(name), stdin);
        fileName = fopen("text.dat", "w");
        if (fileName == NULL) {
            printf("There is an error opening the file.\n");
            exit(1);
        }
        
        char line [MAX_LINE];
        printf("Please enter a line of text:, max %d characters\n", sizeof(line));
        if (fgets(line, sizeof(line), stdin) != NULL) {
           fprintf(fileName, "%s\n", line);
           }
     
         char *o;
       
         printf ("Before conversion: %s\n", line);
    
         for (o = line; *o != '\0'; ++o)
         {
           *o = toupper(*o);
           ++o;
            }
    
         printf ("After conversion: %s\n", line);
    
         
         return;   
    
    }
    My output:

    Option #1

    Enter a file name: name
    Please enter a line of text:, max 80 characters
    hello there
    Before conversion: hello there

    After conversion: HeLlO ThErE
    Why isnt it making the whole entire text uppercase? It seems like it is skipping characters. What should I change/modify in my code to make it all uppercase??

    Thanks

    Orange
    Last edited by orangeboy443; December 11th, 2005 at 07:02 PM.

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Converstion to lower to upper case problem

    Why isnt it making the whole entire text uppercase? It seems like it is skipping characters.
    You are skipping characters. You increment the pointer 0 two times in your loop.
    Code:
    for (o = line; *o != '\0'; ++o)
    {
        *o = toupper(*o);
        ++o;
    }
    - petter

  3. #3
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Conversion to lower to upper case problem

    here is one easiest way is for you
    i think u know that the difference between small letter is 32 only so if u wann change small to capital just -32 from actual or if u wann capital to small add 32 and with the help of this logic simply you can perform your work either use fgetc to read a character or fgets to read a string just parse your string in this way that's all you have to do
    here is one simple code example for you

    Code:
    int main()
    {
    	char strPass[50];
    	int i=0;
    	puts("Enter your File name\n");
    	gets(strPass);
     
    	puts("Here is your File name\n");
     
    	for(;strPass[i]!='\0';i++)
    	{
    		printf("%c",strPass[i]-32);
    	}
    	printf("\n");
     
    return 0;
     
    }
    Last edited by humptydumpty; December 11th, 2005 at 07:52 PM.

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Conversion to lower to upper case problem

    Quote Originally Posted by humptydumpty
    here is one easiest way is for you
    i think u know that the difference between small letter is 32 only so if u wann change small to capital just -32 from actual or if u wann capital to small add 32
    Your code example don't behave correctly if the input contains other than alphabetical characters.

    - petter

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Conversion to lower to upper case problem

    The correct answer is to eliminate either of the ++o statements.

  6. #6
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Conversion to lower to upper case problem

    did u read the question
    op exactly told that change from lower to upper fine
    Second thing what is the behaviour of toupper if u get 1 or some special character

  7. #7
    Join Date
    Dec 2005
    Posts
    63

    Re: Conversion to lower to upper case problem

    ok i see where i made the mistake. I incremented it twice, thank you guys for your feedback. Much appreciated. Thanks

  8. #8
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Conversion to lower to upper case problem

    Quote Originally Posted by humptydumpty
    Second thing what is the behaviour of toupper if u get 1 or some special character
    VCs implementation of toupper checks if the character is in the range 'a' to 'z'. If it's outside that range it returns the original value.

    - petter
    Last edited by wildfrog; December 11th, 2005 at 09:55 PM.

  9. #9
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Conversion to lower to upper case problem

    no i am not saying that
    means to say if
    i take
    int i=5;
    and then use toupper() on this what's the resule is going to come.

  10. #10
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Conversion to lower to upper case problem

    Quote Originally Posted by humptydumpty
    no i am not saying that
    means to say if
    i take
    int i=5;
    and then use toupper() on this what's the resule is going to come.
    It would most probably give a compiler error related to argument type mismatch . Why would a function like toupper() take in an integer argument? Regards.

  11. #11
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Converstion to lower to upper case problem

    if
    i take
    int i=5;
    and then use toupper() on this what's the resule is going to come.
    toupper takes an integer. And if the integer is not in the ragne 97 ('a') to 122 ('z') then the it simply returns the 'input'. In you example it would return 5.

    - petter
    Last edited by wildfrog; December 12th, 2005 at 12:27 PM. Reason: toUpper --> toupper

  12. #12
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Converstion to lower to upper case problem

    There is no such function as toUpper

    And toupper takes an int argument because they wrote it that way. Perhaps they had in mind to allow for wide/multi-byte characters back then.

    I think in C there is also a concept of locale, it's just that you can only have one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured