hi there
are there functions like toupper(); and tolower();
which i know from php that i can use in C ? :confused:
Printable View
hi there
are there functions like toupper(); and tolower();
which i know from php that i can use in C ? :confused:
ctype.h
Good luck :)Code:#include <ctype.h>
uh ;) thanks!
Should you ever be caught w/o ctype.hCode:#include <stdio.h>
int main(void)
{
char aString[] = "THiS # Is && A && CRazY # strING\n";
int i, letter;
printf("%s", aString);
for(i=0; aString[i] != '\0'; i++)
{
letter = aString[i];
/* to upper */
if(letter >= 97 && letter <= 122)
aString[i] = letter - 32;
}
printf("toupper: %s", aString);
for(i=0; aString[i] != '\0'; i++)
{
letter = aString[i];
/* to lower */
if(letter >= 65 && letter <= 90)
aString[i] = letter + 32;
}
printf("tolower: %s", aString);
return 0;
}
nice ;) thank you
Non portable. But then, why would you be without ctype.h in the first place?Quote:
Originally posted by ronin
Should you ever be caught w/o ctype.h
Code:if(letter >= 97 && letter <= 122)
aString[i] = letter - 32;
if(letter >= 65 && letter <= 90)
aString[i] = letter + 32;
}
Quzah.
I enjoy thinking of ways to replicate the functions supported by the compiler, and I deal primarily with Dos instead of other platforms such as Unix, Linux, Win32 etc, so those methods have worked.
It may be a lot of extra work, but it helps me to understand what's going on, and isn't that the purpose of learning in general?.... I've never taken a C course, but I've had one semester of intro C++ :)
You care to elaborate on the non portability? Is it the numbering systems?
What about replacing int with a char and using 'a' && 'z' ?
>You care to elaborate on the non portability?
C does not assume ASCII; in EBCDIC, letters of the alphabet are not contiguous.
>What about replacing int with a char and using 'a' && 'z' ?
Better, but no -- still not portable.
>>You care to elaborate on the non portability?
You need to look at the different between the ASCII and EBCDIC charts.
A simple observation is that in the ASCII chart, the uppercase A-Z are 65 to 90, and the lower case a-z are 97 to 122.
Whereas, the EBCDIC chart has these in different places completely. Take a look at this table and you'll see that a-z are not even in one contiguous block, so addition/subtraction on them won't work the same as ASCII.
[edit]Doh!
Hmm... I never thought about that.
A new topic to hunt down and conquer. :D
Thanks Dave... nice source, and as always thanks Hammer.