|
-
December 11th, 2005, 06:50 PM
#1
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:
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
Last edited by orangeboy443; December 11th, 2005 at 07:02 PM.
-
December 11th, 2005, 07:40 PM
#2
Re: Converstion to lower to upper case problem
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
-
December 11th, 2005, 07:45 PM
#3
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;
}
Last edited by humptydumpty; December 11th, 2005 at 07:52 PM.
-
December 11th, 2005, 08:19 PM
#4
Re: Conversion to lower to upper case problem
 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
-
December 11th, 2005, 08:36 PM
#5
Re: Conversion to lower to upper case problem
The correct answer is to eliminate either of the ++o statements.
-
December 11th, 2005, 08:52 PM
#6
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
-
December 11th, 2005, 08:59 PM
#7
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
-
December 11th, 2005, 09:52 PM
#8
Re: Conversion to lower to upper case problem
 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
Last edited by wildfrog; December 11th, 2005 at 09:55 PM.
-
December 11th, 2005, 10:05 PM
#9
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.
-
December 12th, 2005, 01:19 AM
#10
Re: Conversion to lower to upper case problem
 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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
December 12th, 2005, 09:00 AM
#11
Re: Converstion to lower to upper case problem
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
Last edited by wildfrog; December 12th, 2005 at 12:27 PM.
Reason: toUpper --> toupper
-
December 12th, 2005, 12:04 PM
#12
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|