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;
}
|
🔒 Locked
How to convert a string to uppercase in c++
Advertisement
Original Post
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.
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
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.
Or else just
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
|
Or else just
|
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
#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
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
whats wrong with good old
_strupr( pString );
_strlwr( pString );
_strupr( pString );
_strlwr( pString );
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.
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:
|
--
You see some pale bulbous eyes staring at you.
quote:
Original post by SabreMan
Strictly speaking, std::string isn''t part of the STL.
Why do you say so, if I may ask?
typedef string basic_string<char>;
Looks like STL all the way to me (Container, to be specific).
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
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
-pirate_dau
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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement