Skip to main content
gamedev.net
🔒 Locked

How to convert a string to uppercase in c++

Started by LordG 14 replies 41.5k views
Original Post
LordG
LordG
I'm really starting to hate c++ (For manipulating string anyways). C++ provides a function to convert a char to uppercase, but not a whole string.. I see how it is... Anyways, I tried writing my own function to convert a string to uppercase and it didn't work.
      

void upperCase(char *word)
{
	int i;
	char s[50];

	// Converting word to uppercase.

	for (i = 0; i < (int)strlen(word); i++) {    
		strcat(s, (char *)toupper(word[i]));
	}
        cout << s;
}
      
After I compile and run the code, it prints out "< g" ...wtf? Can someone help me with a better way of converting a string to uppercase, trying not to use STL's string. Because I need to write a string to a file and I don't know how to write STL's string to a file... Edited by - LordG on March 5, 2002 11:05:41 PM Edited by - LordG on March 5, 2002 11:17:53 PM
invective
invective
YOu could use just the ascii values to simplify the following code, but i used characters to make things clearer.

  
void toUpperCase (char * word)
{
for (int i = 0; i< strlen (word); ++i)
if ((word[i] >= ''a'') && (word[i] <= ''z'')
word [i] = word[i] - ''a'' + ''A'';
}
nitro123
nitro123

Easy! All you have to do is #include <ctype.h> and use the toupper function.

#include <iostream.h>
#include <ctype.h>
#include <string.h>
#include <conio.h>

main() {

char *s = "justin";
for(int i=0;i s = toupper(s[i]);
}
cout<getch();
}
Bob Jones
invective
invective
whoops i wasn't reading carefully -- you don't mind using the to upper macro, and it looks like you are not modifying the string, only printing it in upper case. By the way your strcat problem is that toUpper returns and integer, not a char* or even a char.

    
void upperCase(char *word){
int i;
char s[50];
// Converting word to uppercase.

for (i = 0; (i < (int)strlen(word))&&(i<50)); i++)
s[i] = (char)toupper(word[i]);
s[49] = '0'; //paranoia, but you never know

cout << s;
}


Or else just

    
void printUpperCase(char *word){
int i;
for (i = 0; i < strlen(word); ++i)
cout << (char)toUpper(word[i]);
}


Really though, c strings are awful. Use C++ stl strings. You can always use the c_str() method of an std::string if you need to convert to a c style string for compatability.

Edited by - invective on March 5, 2002 11:31:21 PM
LordG
LordG
Thanks for the help. I didn''t realize it was that easy... sometimes I forget a string is an array of chars.. stupid C++.
alexk7
alexk7
Functions that assume ASCII character set are not portable. You must use toupper().

Here''s my function:

void upper_case(char* s)
{
while (*s = toupper(*s)) ++s;
}
alexk7
Shadow Wolf
Shadow Wolf
#include <stdio.h>

int mytoupper(int val);
int mytolower(int val);

int mytoupper(int val)
{
int work = 0;

if (val >= ''a'' && val <= ''z'')
{
work = (val -= 32);

}//if

return(work);

}//mytoupper

int mytolower(int val)
{
int work = 0;

if (val >= ''A'' && val <= ''Z'')
{
work = (val += 32);

}//if

return(work);

}//mytolower

int main(void)
{
char test[20] = "Woohoo";

test[3] = mytoupper(test[3]);
printf("%s\n", test);

test[3] = mytolower(test[3]);
printf("%s\n", test);

return(0);

}//main
rpgatehouse
rpgatehouse
whats wrong with good old

_strupr( pString );
_strlwr( pString );
DrPizza
DrPizza
quote:
After I compile and run the code, it prints out "< g" ...wtf? Can someone help me with a better way of converting a string to uppercase, trying not to use STL''s string.
Because I need to write a string to a file and I don''t know how to write STL''s string to a file...

The same way you write a char* out to a file.

As for converting the case (why, out of interest?) look at the recent thread on how to convert to lower case, and replace any instance of tolower with toupper.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
SabreMan
SabreMan
quote:
Original post by LordG
After I compile and run the code, it prints out "< g" ...wtf?



You''re printing out the value of a pointer, not the string pointed-to.

quote:

Can someone help me with a better way of converting a string to uppercase, trying not to use STL''s string.
Because I need to write a string to a file and I don''t know how to write STL''s string to a file...


Strictly speaking, std::string isn''t part of the STL. Anyhow, it is a lot easier to do this sort of thing with std::string and fstreams than it is with char*''s.

For example:

  
#include <string>

#include <fstream>


int main()
{
std::ofstream out( "output.txt" );
std::string s = "this is a test";

out << s;
}


--
You see some pale bulbous eyes staring at you.
SabreMan
SabreMan
Yes, actually you''re quite right. For some odd reason I assumed it wasn''t part of SGI''s STL, but I''ve just checked and it is.

--
You see some pale bulbous eyes staring at you.
pirate_dau
pirate_dau
It''s been a long time since I''ve thought about this, but isn''t there a really easy bitmask for converting ASCII characters from lower -> upper? I guess it doesn''t matter that much. OH WAIT.. yea, I think I remember (heh.. cobwebs in me brain). It''s 0xdf -- any alphabetic character & 0xdf will become upper case... just another way to do things.

-pirate_dau
DrPizza
DrPizza
quote:
Original post by Oluseyi
Looks like STL all the way to me (Container, to be specific).


Random-access sequence (a subset of random-access container which is a subset of container), to be more specific.

Though basic_string is not part of the container library, that is the interface it provides.

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.