Changing character case

This is a discussion on Changing character case within the C Programming forums, part of the General Programming Boards category; I am writing this function on changing character strings to either all uppercase or all lowercase. I am supposed to ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    34

    Changing character case

    I am writing this function on changing character strings to either all uppercase or all lowercase. I am supposed to return wether or not a change was made. I am to recieve a string to change and number flag (myChangeType) to tell whether to change to capitals (eToUpper) or lowercase (eToLower)

    * Use the flag (myChangeType) to record whether or not any changes
    * were made.
    * myChangeType=eFalse means no change was made.
    * myChangeType=eTrue means a change was made.

    heres what i have so far... i dont know how to use the flag ...any help would be appreciated

    Code:
    int myStrChangeCase(unsigned char first_string [], myChangeType name)
    {
     int i;
     int myChangeType;
     
     /* Lowercase to Uppercase */
     for(i=0; first_string[i] !='\0'; i++);
     {	
    	 if( first_string[i] >= 'a' && first_string[i] <='z')
    		 first_string[i]= first_string[i] - (32);
     }
       
     /* Uppercase to Lowercase */
     for(i=0; first_string[i] !='\0'; i++);
     {
    	if( first_string[i] >= 'A' && first_string[i] <='Z')
    		 first_string[i]= first_string[i] + (32);
     }
     
     return myChangeType; 
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should be using the functions isupper, islower, toupper and tolower. Not all character sets have a through z the same. Also, it's not set in the standard that the difference between A and a is 32. There is no such specification.

    Also, what's the point in returning the change type, since you're passing it to the function? I suspect you want something like:
    Code:
    if( changeType == LowerMe )
    {
        ...lowering code...
    }
    else
    {
        ...raising code...
    }
    Otherwise the way you have it, everything gets converted through both cases loops.

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

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Can someone please clean up my code
    By ki113r in forum C Programming
    Replies: 10
    Last Post: 09-12-2007, 11:03 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21