how do i change a string to all uppercase~?
Printable View
how do i change a string to all uppercase~?
If you're still using the LVP deal:
Code:#include <ctype.h>
#include <iostream.h>
#include "string.h"
int main()
{
String s = "tESt";
for (int i = 0; i < s.length(); i++)
{
s[i] = tolower(s[i]);
}
cout << s;
return 0;
}
I have not tested it but try this and there might be some errors:
PHP Code:char mystr[50], mystr2[50];
cin>>mystr;
for(int i=1;i<=strlen(mystr);i++)
{
mystr2 = mystr2 + (char)((int)mystr[i] + 32);
}
cout<<mystr2;
abdul: your program will probably work to make a string composed ONLY of lowercase alpha characters (a-z, nothing else). It won't work in any other case - that can be corrected by replacing the line in the for loop by toupper(...).
for Filburt's code: toupper, not tolower...
I'm not using string.h. I'm using string
no matter, use filburts method slightly modified:
Code:#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "tESt";
for (int i = 0; i < s.length(); i++)
{
s[i] = tolower(s[i]);
}
cout << s;
return 0;
}
thanks all of ye~! i"m making a silly hangman game to get used to C++, and well, i thought it would be appropriate to make the letters you enter non-case sensative =] I have one more question, though. How come when I link to conio.h, clrscr() and gotoxy() don't work, but when i link to conio.c, they do?
Shouldn't you guys be using the _toupper function?
well, i am, but for my purposes - converting all the letters to one case to compare them without having to worry about case sensitivity - it could be either one. now someone tell me whats up with conio.h :[
Is this not that Vb you are coding with?
May you post this in the Forum for Visual Basic, you will have more sucess with the answere.
In C++ just write in a App with MFC:
varname.MakeUpper();
Cheers,
Myvar
Ahem...
Myvar: he is NOT coding VB, this is just his signature...
And he should NOT use MFC unless he knows both C++ and the API well.
Moose: what compiler do you use? Basically, you include header files (.h, .hh, .hpp, no extension) and link to modules (.c, .cpp), object files (.o, .obj) and libraries (.lib, .a). The extensions depend on what you do and on the compiler. clrscr() and gotoxy() are only borland...
I use bloodshed. The only problem I'm haivng with C++ is it's hard to know how to use the header files ;p In school, I learned to use conio.h and clrscr(), gotoxy(), and also lvp\string.h and the functions that go with that. I thought every header file would be just like those, but I guess not. I'd like to know how to clear the screen with the compiler i'm using - bloodshed, but I think more importantly, I'd like to know how Im supposed to figure out how to use all the included header files.
Why can't C++ be nice and simple with everything built in like VB =D No, before 50 people jump all over me and think i'm serious about that, no, i'm sure it makes c++ more powerful in the end :P Anywayz, the syntax in C++ is really simple, it's just understanding how to use it with header files and the like that confuses me :[ So, what i want to know now is a website maybe? something that explains all this to me.
The cross-browser method to clear the screen is system("cls"); (for DOS, for LINUX it would be "clear"). You need to include stdlib.h.
For the headers:
header files provide declarations of the functions in libraries and modules. They are included with the #include directive: use <filename> for headers that come with the compiler and "filename" for headers you wrote yourself.
I heard bout the lvp/ thing twice now, and it seems to be something very evil that has to do with a dumb compiler that some schools use.
There are two types of headers that come with compilers:
a) the standard headers. The functions inside are the same for all compilers all over the world. They are standardisized by ANSI. They include common headers such as stdlib.h, stdio.h, string.h and math.h. They also include the c++ standard headers, like iostream, fstream and string.
b) Compiler extensions: conio.h is the most common. They can contain different things and difer from compiler to compiler. DOS compilers (like DJGPP or older Borland and Microsoft compilers) will have completely different things in those than Windows compilers or Unix compilers. For example, conio.h for borland contains clrscr(), gotoxy() and such, while vc++6.0 conio.h contains outp() and inp().
Then there are libraries that have their own headers, like MFC or DirectX. You usually place them in their own directory and set the compilers standard search path to include those, so you can include them using <>.
Headers almost never contain real code, only declarations and constant definitions.
Sorry CornedBee I just wanted to help.
But may you tought that he MUST reinvent all Algorithms new. I understand thats important to understand whats behind MFC. But with C# the API is not used anymore, isnt it?
So why he had to know it?
But I see, you are the Teacher so keep teaching Coach.
Best Regards,
Myvar
Well, who uses C#? It's an evil M$ thing, and I will avoid it as long as possible. And nobody says you don't use API in C# (or does anybody? MS maybe?). It will be accessible, so people will use it sometimes. At least unless MS comes up with something far better than MFC. It is not bad, but it can make you sick sometimes.
And even if MS comes up with something better, he still should know how to use API IMO. Debugging MFC apps is a mess at best and hell at worst, so why should it be better with other libraries?