+ 1

Help to solve ,please

Write a function that converts given sequence by the following rules: 1 - If the letter is in upper case make it lower case; 2 - If the letter is in lower case make it upper case; 3 - If it's not a letter left it as is Input: The characters that have to be converted. Input can contain any printeble characted(numbers,"[","]",")"),your task is to change only letters. The size of the given sequence is not more that 100. Output: The proper sequence of characters(see the sample). Input | Output aBaBba | AbBbBA #include<iostream> #include<cstring> using namespace std; /* * This function converts given sequence of characters * from the lower case to upper case */ char convert(char arr) { if ('a' <= arr && arr <= 'z') { return char(arr - 32); } else { return arr; } } int main() { char arr[1000]; cin >> arr; int length = strlen(arr); for (int i = 0; i < length; i++) { cout << convert(arr[i]); } }

11th Oct 2020, 7:53 AM
Azat Malgazhdar
Azat Malgazhdar - avatar
3 Réponses
+ 2
add an else if, to change upper to lower. and change 'a' <= arr to arr => 'a' (more readable) char convert(char arr) { if (arr >= 'a' && arr <= 'z') { return char(arr - 32); }else if(arr >= 'A' && arr <= 'Z') { return char(arr + 32); }else { return arr; }
11th Oct 2020, 8:10 AM
Bahhaⵣ
Bahhaⵣ - avatar
11th Oct 2020, 8:15 AM
Azhagesanヾ(✿)
Azhagesanヾ(✿) - avatar
+ 2
Thank you both!!!
11th Oct 2020, 8:24 AM
Azat Malgazhdar
Azat Malgazhdar - avatar