[C programming] change case of letters

Discussion in 'Mac Programming' started by gotenks05, Oct 20, 2009.

  1. macrumors member

    Joined:
    Jan 1, 2009
    #1
    I am working on a program that allows a user to enter a string and the program converts every letter, via a custom function, from lowercase to uppercase. It works somewhat well, but it will not display the entire string at runtime. How do I fix this?

    this is the function prototype:

    Code:
    void capitalize(char string[]);
    this is the function definition:

    Code:
    #include <stdio.h>
    
    void capitalize(char string[])
    {
    	int size = sizeof(string)/sizeof(char);
    	int count = 0, count2 = 0;
    
    	for (count; count <= size -1; count++)
    	{
    		if (string[count] >= 97 && string[count] < 123 && string[count != '\0')
    		{
    			string[count] -= 32;
    		}
    	}
    
    	for (count2; count2 <= size -1; count2++)
    	{
    		printf("%c", string[count2]);
    	}
    	printf("\n");
    }
    and here is the program source:

    Code:
    #include <stdio.h>
    #include "convert.h"
    int main()
    {
    	char line[sizeof(char)];
    
    	printf("Enter a string:  ");
    	scanf("%s", line);
    
    	capitalize(line);
    
    	return 0;
    }
    When trying to fix this myself, I tried specifying a size directly and through

    Code:
    #define <word> <size>
    However, only the first word showed up. I even tried editing the function definition, but that would not display the entire string either.
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Sizeof does not do what you think it does. Use strlen, assuming you're using null-terminated "c strings". Sizeof(char *) will return 4 or 8 depending on the pointer size on your platform.

    -Lee
     
  3. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #3
    Much simpler way to do this: try using the toupper() function.
     
  4. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    Given that what we're discussing is to be written in C you'll need to be careful of buffer overruns getting user input.

    The size of an input buffer is unknown, since the user can type in the full text of War and Peace if they want, thus making the use of 'scanf' a very poor choice.

    This example uses 'getchar' instead and implements its own primitive string like buffering.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    struct string_t
    {
        size_t  _length;
        size_t  _buff_length;
        char*   _buffer;
    };
    
    void string_init(string_t* pstr)
    {
        if ( pstr != NULL )
        {
            pstr->_length       = 0;
            pstr->_buff_length  = 256;
            pstr->_buffer       = (char*)realloc(pstr->_buffer, pstr->_buff_length);
        }
    }
    
    void string_append(string_t* pstr, int ch)
    {
        if ( pstr != NULL )
        {
            if ( pstr->_length >= pstr->_buff_length )
            {
                pstr->_buff_length *= 2;
                pstr->_buffer = (char*)realloc(pstr->_buffer, pstr->_buff_length);
            }
    
            pstr->_buffer[pstr->_length++] = ch;
        }
    }
    
    char* string_address(string_t* pstr)
    {
        if ( pstr != NULL )
        {
            if ( pstr->_length >= pstr->_buff_length )
            {
                pstr->_buff_length += 256;
                pstr->_buffer = (char*)realloc(pstr->_buffer, pstr->_buff_length);
            }
    
            pstr->_buffer[pstr->_length] = '\0';
    
            return pstr->_buffer;
        }
    
        return NULL;
    }
    
    size_t string_length(string_t* pstr)
    {
        if ( pstr != NULL )
        {
            return pstr->_length;
        }
    
        return 0;
    }
    
    int main(int argc, char* const argv[])
    {
        size_t      length, i;
        string_t    str;
        int         ch;
        
        string_init(&str);
        while ( '\n' != (ch = getchar()) )
        {
            string_append( &str, toupper(ch));
        }
    
        printf("You typed: %s", string_address(&str));
    
        return 0;
    }
    
     
  5. macrumors 68030

    CylonGlitch

    Joined:
    Jul 7, 2009
    Location:
    SoCal
    #5
    Lets assume a nice managed input method.

    Code:
    void to_upper_case(unsigned char *String_to_upper)
    {
        unsigned char *Start = String_to_upper;
        while(*String_to_upper != NULL)
        {
            if(*String_to_upper & 0x40)
                *String_to_upper |= 0x20;
            String_to_upper++;
        }
        printf("%s", Start);
    }
    
    NOTE: Converts it also makes the following conversions
    @ -> `
    [ -> {
    | -> \
    ] -> }
    ^ -> ~
    _ -> (del)

    This could be easily fixed by changing the if statement to
    Code:
    if ((*String_to_upper >= 65) && (*String_to_upper <= 90))
    
     
  6. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    Code:
    void capitalize(char string[])
    {
        while ( *string )
        {
            if ( (*string >= 'a') && (*string <= 'z') )
            {
                *string += 'A' - 'a';
            }
    
            string++;
        }
    }
    
     

Share This Page