Jul 20, 2012 at 6:40am UTC
Check the comparison in the for loop.
Jul 20, 2012 at 6:42am UTC
s[i]='\0'
should be s[i]=='\0'
Jul 20, 2012 at 6:58am UTC
thanks for reply.... program is running but not converting the string.
Jul 20, 2012 at 7:07am UTC
champ it should be
s[i]!='\0' ;
enjoy programming :)
Jul 20, 2012 at 7:10am UTC
you could use a std::string instead of a char and then:
std::transform(s.begin(), s.end(), s.begin(), toupper);
If it's a homework... I would suggest looping with string length instead, using strlen(), and... toupper is in ctype.h, here's a code sample:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <string>
#include <ctype.h>
#include <string.h>
using namespace std;
int main() {
cout << "string: " ;
char s[256];
cin.get(s, 256);
for (int i = 0; i < strlen(s); i++)
s[i] = toupper(s[i]);
cout <<endl << s << endl;
return 0;
}
In your code, the loop condition should be: s[i] != '\0'
EDIT: ow bit slower =)
Last edited on Jul 20, 2012 at 7:18am UTC
Jul 20, 2012 at 7:11am UTC
cant stop laughing over myself..... thanks prashant!!
Jul 20, 2012 at 7:39am UTC
@FeZendra no its not a homework at all. i did like your suggestion. here is what i did this time
1 2 3 4 5 6 7 8 9 10 11 12
#include<iostream>
#include<string>
using namespace std;
#include<string>
int main()
{
cout<<"enter a string: " ;
string s;
getline(cin,s);
transform(s.begin(), s.end(), s.begin(), toupper);
cout<<s;
}
but it is showing error that transform identifier not found.
help me out i m kinda noob here.
Last edited on Jul 20, 2012 at 7:45am UTC
Jul 20, 2012 at 11:07am UTC
#include <algorithm>
Also, remove that include duplicate, you included string twice, it's a bad programming practice
Last edited on Jul 20, 2012 at 11:10am UTC