Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

gotenks05

macrumors member
Original poster
Jan 1, 2009
78
0
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
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;
}
 

CylonGlitch

macrumors 68030
Jul 7, 2009
2,956
268
Nashville
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))
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
Code:
void capitalize(char string[])
{
    while ( *string )
    {
        if ( (*string >= 'a') && (*string <= 'z') )
        {
            *string += 'A' - 'a';
        }

        string++;
    }
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.