Click to See Complete Forum and Search --> : Converstion to lower to upper case problem


orangeboy443
December 11th, 2005, 05:50 PM
Hey, I'm using C language and I want to tell you my code for a function that needs to convert lowercase to uppercase:


#include <stdio.h>
#include <ctype.h>
#include <conio.h>






void convertL(FILE *fileName)
{
ClearScreen();
char name[15];
char *p;
printf("\nOption #3\n\n");
fflush(stdin);
printf("Enter a file name: ");
fgets(name, sizeof(name), stdin);
fileName = fopen("text.dat", "w");
if (fileName == NULL) {
printf("There is an error opening the file.\n");
exit(1);
}

char line [MAX_LINE];
printf("Please enter a line of text:, max %d characters\n", sizeof(line));
if (fgets(line, sizeof(line), stdin) != NULL) {
fprintf(fileName, "%s\n", line);
}

char *o;

printf ("Before conversion: %s\n", line);

for (o = line; *o != '\0'; ++o)
{
*o = toupper(*o);
++o;
}

printf ("After conversion: %s\n", line);


return;

}



My output:


Option #1

Enter a file name: name
Please enter a line of text:, max 80 characters
hello there
Before conversion: hello there

After conversion: HeLlO ThErE



Why isnt it making the whole entire text uppercase? It seems like it is skipping characters. What should I change/modify in my code to make it all uppercase??

Thanks

Orange

wildfrog
December 11th, 2005, 06:40 PM
Why isnt it making the whole entire text uppercase? It seems like it is skipping characters.You are skipping characters. You increment the pointer 0 two times in your loop.

for (o = line; *o != '\0'; ++o)
{
*o = toupper(*o);
++o;
}

- petter

humptydumpty
December 11th, 2005, 06:45 PM
here is one easiest way is for you
i think u know that the difference between small letter is 32 only so if u wann change small to capital just -32 from actual or if u wann capital to small add 32 and with the help of this logic simply you can perform your work either use fgetc to read a character or fgets to read a string just parse your string in this way that's all you have to do
here is one simple code example for you


int main()
{
char strPass[50];
int i=0;
puts("Enter your File name\n");
gets(strPass);

puts("Here is your File name\n");

for(;strPass[i]!='\0';i++)
{
printf("%c",strPass[i]-32);
}
printf("\n");

return 0;

}

wildfrog
December 11th, 2005, 07:19 PM
here is one easiest way is for you
i think u know that the difference between small letter is 32 only so if u wann change small to capital just -32 from actual or if u wann capital to small add 32Your code example don't behave correctly if the input contains other than alphabetical characters.

- petter

NMTop40
December 11th, 2005, 07:36 PM
The correct answer is to eliminate either of the ++o statements.

humptydumpty
December 11th, 2005, 07:52 PM
did u read the question
op exactly told that change from lower to upper fine
Second thing what is the behaviour of toupper if u get 1 or some special character

orangeboy443
December 11th, 2005, 07:59 PM
ok i see where i made the mistake. I incremented it twice, thank you guys for your feedback. Much appreciated. Thanks

wildfrog
December 11th, 2005, 08:52 PM
Second thing what is the behaviour of toupper if u get 1 or some special characterVCs implementation of toupper checks if the character is in the range 'a' to 'z'. If it's outside that range it returns the original value.

- petter

humptydumpty
December 11th, 2005, 09:05 PM
no i am not saying that
means to say if
i take
int i=5;
and then use toupper() on this what's the resule is going to come.

exterminator
December 12th, 2005, 12:19 AM
no i am not saying that
means to say if
i take
int i=5;
and then use toupper() on this what's the resule is going to come.It would most probably give a compiler error related to argument type mismatch :). Why would a function like toupper() take in an integer argument? Regards.

wildfrog
December 12th, 2005, 08:00 AM
if
i take
int i=5;
and then use toupper() on this what's the resule is going to come.
toupper takes an integer. And if the integer is not in the ragne 97 ('a') to 122 ('z') then the it simply returns the 'input'. In you example it would return 5.

- petter

NMTop40
December 12th, 2005, 11:04 AM
There is no such function as toUpper

And toupper takes an int argument because they wrote it that way. Perhaps they had in mind to allow for wide/multi-byte characters back then.

I think in C there is also a concept of locale, it's just that you can only have one.