what is wrong need to change lowercase to upper without toupper()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dcowboyz31
    New Member
    • May 2010
    • 3

    what is wrong need to change lowercase to upper without toupper()

    here is my function


    Code:
    char* upper( char* str){
    
    int length = strlen(str);
    
    char* string_new = ( char* )malloc(sizeof( int ) *( 256 ) );
    
    int i;
    
    printf( " This is the original sentence : %s \n" , str );
    
    for( i = 0 ; i < length ; i++ ){
    
    if( str[i] >= 97 && str[i] <= 122 ){
    
    string_new[i] = str[i] - 32 ;
    
    }
    else if( str[i] == 0 ){
    
    break;
    
    
    
    
    
    }
    printf( " %s \n " , string_new );
    return string_new;
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Use CODE tags, they make your code easier to read.

    Presumably you are not happy with the result of this function. You will get better help if you tell us (a) what you expected to get; and (b) how your actual result differs from your expectations.

    How did you decide that the size of string_new should be sizeof(int)*256 ?

    I see three magic numbers in your program: 97, 122, and 32. Where did these numbers come from? What do they mean?
    (Hint: I'm asking because there is a better way to express them.)

    What should your loop do for characters that are not 0 or between 97 and 122?

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      I don`t think the if and else if braces are needed also :
      Code:
      if(c>='a' && c<='z')c+='A'-'a';
      will change a lower case character to upper case

      Comment

      Working...