PDA

View Full Version : [C programming] change case of letters




gotenks05
Oct 20, 2009, 03:54 PM
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:

void capitalize(char string[]);

this is the function definition:

#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:

#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

#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
Oct 20, 2009, 04:32 PM
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

ChrisA
Oct 20, 2009, 06:44 PM
Much simpler way to do this: try using the toupper() function.

lloyddean
Oct 20, 2009, 08:52 PM
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.


#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
Oct 20, 2009, 10:30 PM
Lets assume a nice managed input method.


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

if ((*String_to_upper >= 65) && (*String_to_upper <= 90))

lloyddean
Oct 21, 2009, 12:26 AM
void capitalize(char string[])
{
while ( *string )
{
if ( (*string >= 'a') && (*string <= 'z') )
{
*string += 'A' - 'a';
}

string++;
}
}