converting string to upper case

i m supposed to write a program which asks user to input a string and then convert it to uppercase if lowercase alphabets are present. here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main()
{
	cout<<"enter string :";
	char s[256];
	cin.getline(s,256);
	for(int i=0;s[i]='\0';i++)
	{
		s[i]=toupper(s[i]);
	}
	cout<<s;
}

problem is when i run this program it does not display anything
Check the comparison in the for loop.
s[i]='\0' should be s[i]=='\0'
thanks for reply.... program is running but not converting the string.
champ it should be
s[i]!='\0';

enjoy programming :)
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
cant stop laughing over myself..... thanks prashant!!
@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
#include <algorithm>
Also, remove that include duplicate, you included string twice, it's a bad programming practice
Last edited on
Topic archived. No new replies allowed.