Conversion to lower to upper case problem
Hey, I'm using C language and I want to tell you my code for a function that needs to convert lowercase to uppercase:
Code:
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
Code:
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:
Quote:
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
Re: Converstion to lower to upper case problem
Quote:
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.
Code:
for (o = line; *o != '\0'; ++o)
{
*o = toupper(*o);
++o;
}
- petter
Re: Conversion to lower to upper case problem
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
Code:
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;
}
Re: Conversion to lower to upper case problem
Quote:
Originally Posted by humptydumpty
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
Your code example don't behave correctly if the input contains other than alphabetical characters.
- petter
Re: Conversion to lower to upper case problem
The correct answer is to eliminate either of the ++o statements.
Re: Conversion to lower to upper case problem
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
Re: Conversion to lower to upper case problem
ok i see where i made the mistake. I incremented it twice, thank you guys for your feedback. Much appreciated. Thanks
Re: Conversion to lower to upper case problem
Quote:
Originally Posted by humptydumpty
Second thing what is the behaviour of toupper if u get 1 or some special character
VCs 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
Re: Conversion to lower to upper case problem
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.
Re: Conversion to lower to upper case problem
Quote:
Originally Posted by humptydumpty
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.
Re: Converstion to lower to upper case problem
Quote:
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
Re: Converstion to lower to upper case problem
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.