String

Dec 5, 2021 at 7:25am
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;
}
Dec 5, 2021 at 7:32am

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 Dec 5, 2021 at 7:34am
Dec 5, 2021 at 10:44am
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';
}

Dec 5, 2021 at 9:47pm
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.