Reputation: 2432
Following code is supposed to return the upper case string of the source. It works but does not convert the string. Could not figure out what was wrong.
char *StrUpper (char *s) {
int i = 0;
char *t = &s [i];
while (*t) {
if ((*t > 0x5a) && (*t < 0x7b)) t = (t - 32);
t = &s [i++];
}
return (s);
}
int main () {
printf ("%s\n", StrUpper ("lower case string"));
return (0);
}
Upvotes: 0
Views: 169
Reputation: 173
you might also want to change
t = &s[i++];
to
t = &s[++i];
because , i++ first assigns current i value to the index then performs the increment, you end up processing the same index over again the first time.
Upvotes: 0
Reputation: 311068
First of all these two statements
if ((*t > 0x5a) && (*t < 0x7b)) t = (t - 32);
t = &s [i++];
has no sense.
This expression statement
t = (t - 32);
changes the pointer itself. And this statement
t = &s [i++];
does not change the pointer at once.
Moreover it is not clear what these magic numbers, 0x5a, 0x7b, 32 mean.
The function could be written like
char * StrUpper ( char *s )
{
for ( char *p = s; *p; ++p )
{
if ( *p >= 'a' && *p <= 'z' ) *p -= 'a' - 'A';
}
return s;
}
Here is an example of using the function
#include <iostream>
char * StrUpper ( char *s )
{
for ( char *p = s; *p; ++p )
{
if ( *p >= 'a' && *p <= 'z' ) *p -= 'a' - 'A';
}
return s;
}
int main()
{
char s[] = "hello";
std::cout << s << std::endl;
std::cout << StrUpper( s ) << std::endl;
return 0;
}
The output is
hello
HELLO
Take into account that some coding systems do not guarantee that all lower case letters follow each other without intermediate codes. So in general this approach is not correct.
Upvotes: 0
Reputation: 409364
A string literal is a constant pointer to an array of constant characters. In other words, string literals are read only.
Trying to modify constant data leads to undefined behavior. And if your program have undefined behavior, nothing about its behavior can be trusted at all.
Upvotes: 4
Reputation: 137382
You have a few problems in your code:
You forgot to dereference the string in the assignment:
t = (t - 32);
should be
*t = (*t - 32);
You don't check the correct range:
if ((*t > 0x5a) && (*t < 0x7b))
should be
if ((*t > 0x6a) && (*t < 0x7b))
or, even better
if ((*t >= 'a') && (*t <= 'z'))
You go over the first character twice:
t = &s [i++];
should be
t = &s [++i];
or simply
t++;
Upvotes: 2