String

Where is my mistake?

#include<bits/stdc++.h>

using namespace std;

int main(){
string s; cin>>s;
int cnt1=1, cnt2=0;
for(int i=0;i<s.size();i++){
int code= (int)s[i];
if(code <=122 && code >=97 ){
code-=32;
}
cout<<char(code);
}
cout<<endl;
}

jannin (1)
Where is my mistake?

#include<bits/stdc++.h>

using namespace std;

int main(){
string s; cin>>s;
int cnt1=1, cnt2=0;
for(int i=0;i<s.size();i++){
int code= (int)s[i];
if(code <=122 && code >=97 ){
code-=32;
}
cout<<char(code);
}
cout<<endl;
}


several mistakes :)
1) you didn't use code tags.
2) bits/stdc is nonstandard, try to avoid it
3) using namespace std is frowned upon, try to avoid it
4) cout << (char) code

its more readable to say 'a' than 97 or whatever the heck you are doing. you can still treat it like a number 'a' is a number.
Last edited on
If you're not going to use std::toupper(), then possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <iostream>

int main() {
	std::string s;

	std::getline(std::cin, s);

	for (const auto& ch : s)
		std::cout << static_cast<char>(ch - ('a' - 'A') * (ch >= 'a' && ch <= 'z'));

	std::cout << '\n';
}

jannin wrote:
Where is my mistake?

(1) You haven't put your code in code tags (which is not helpful in the forum).
(2) Your include statement is non-standard.
(3) Most importantly ... you haven't actually said what you are trying to do!

At the moment your code ... correctly ... converts a single word into all upper case.

No doubt some will quibble about signedness and non-ASCII chars and the like, but, for the time being, just tell us what your intended outcome is and in what way your code doesn't meet it.
Topic archived. No new replies allowed.