-
lower to upper
This is a program that prints 5 user inputs of characters. I want to change the user input to upper case using toupper. I have tryied to place the function in all possible lines but it does not work
I also want to print the output in 2 columns....how can i do that??
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main (void)
{
int k,cnt;
char line[81];
char *t[5];
for (k=0; k<5; k++)
{
gets(line);
if feof(stdin) break;
t[k]=(char*) malloc(1+strlen(line));
if (t[k]==NULL)
{
printf("out of storage");
break;
}
strcpy(t[k], line);
}
cnt=k;
printf("count=%d\n",cnt);
for (k=0; k<cnt; k++)
{
printf("%d %s\n", k,t[k]);
free(t[k]);
}
return 0;
}
-
for ( i = 0 ; i < strlen(line) ; i++ ) line[i] = toupper( line[i] );
-
but where do i put it in my program???
That's my problem!!!
-
Somewhere between reading the line in, and printing it out
> strcpy(t[k], line);
It really doesn't matter whether its before or after this (for example) - either will work and both are equally valid solutions
-
I put it after strcpy....
but it still print in lower case!!!!
-
Here are my latest codes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main (void)
{
int k,i,cnt;
char line[81];
char *t[5];
for (k=0; k<5; k++)
{
gets(line);
if feof(stdin) break;
t[k]=(char*) malloc(1+strlen(line));
if (t[k]==NULL)
{
printf("out of storage");
break;
}
strcpy(t[k], line);
for(i=0;i<strlen(line);i++) //<<<============
line[i]=toupper(line[i]);
}
cnt=k;
printf("count=%d\n",cnt);
for (k=0; k<cnt; k++)
{
printf("%d %s\n", k,t[k]);
free(t[k]);
}
return 0;
}
-
-
I think maybe the problem is that the second for loop is nested inside the first...so you are toupper()ing each char and then you are replacing it with another line read from gets()...or something like that.
fyodor
-
here...
PHP Code:
void capitalize (char input[20])
{
int i;
for (i=0; input[i]; i++)
{
if (input[i]>='a' && input[i]<='z')
{
input[i]=input[i]-32;
}
}
}
-
The problem with unreg's latest code is simply that the string is copied from the line variable to the newly malloc'ed memory, but then the toupper() function makes the line variable upper case. The copy of the in the new memory is left untouched. Hence Salem's comment:
>mmm - try before then
meaning, convert the string to uppercase BEFORE copying it to the new memory.
Doing this:
>input[i]=input[i]-32;
to make something upper case is not guaranteed to work. Just because ASCII has it's upper and lower case characters 32 apart, doesn't mean that other character sets do too. EBCDIC for example, is completely different.
This is why you should stick to the standard library functions for doing this type of thing.
-
std
by standard, do you mean using "toupper" ?
are people still using EBCDIC ? :)
how do we know which functions are standard and which are not ? 'cause i want to write code that runs on most compilers (if not all of them)
i can't compile some programs in unix (gcc). it compiles
well with borland/turbo c++, etc..
-
>by standard, do you mean using "toupper" ?
Yes.
>are people still using EBCDIC ?
Yes. It's not "old fashioned", it's just format that isn't used in the "home environment" or the PC world, so a lot of people don't come in contact with it. An IBM mainframe will use it for example (it was after all, design by IBM).
>how do we know which functions are standard and which are not ?
Find a good website or reference book that details each functions compatability. A standard function will conform to one of the ANSI standards.
>i can't compile some programs in unix (gcc). it compiles well with borland/turbo c++, etc
Then the chances are you're using something non-standard.
-
Code:
if (input[i]>='a' && input[i]<='z')
{
input[i]=input[i]-32;
}
Depending on your character set, this may be invalid. If you are going to write your own, you have to assume you know the way your character set works. If you don't, then your code may or may not work.
Assuming that the alphabet is displayed from a to z, numericly incrementing, you can do:
Code:
int alpha( int a )
{
if( a >= 'a' && a <= 'z' )
{
return a+('a'<'A'?('A'-'a'):-('a'-'A'));
}
return a;
}
That should do the trick.
Quzah.