when I run the main function, the output of tolowercase didn\'t come out. Here i
ID: 3839361 • Letter: W
Question
when I run the main function, the output of tolowercase didn't come out.
Here is my whole code.
I am very confused now, please help me.
Here is my whole code.
#include <iostream>
using namespace std;
int smallCol(int x[][3], int r, int c) {
int ans;
for (int col = 0; col < c; col++) {
int sum = 0;
for (int row = 0; row < r; row++)
sum += x[row][col];
if (col == 0 || sum < ans)
ans = sum;
}
return ans;
}
void toUpperCase(string x, string y){
int lengthofx = x.length();
int lengthofy = y.length();
for (int i=0; i < lengthofx; i++)
if((x[i]>= 'a') && (x[i]<='z'))
cout << (char)(x[i]-32);
cout << " ";
for (int j=0; j < lengthofy; j++)
if ((y[j]>= 'a') && (y[j]<='z'))
cout << (char)(y[j]-32);
}
void toLowerCase(string x, string y){
int lengthofx = x.length();
int lengthofy = y.length();
for (int i=0; i < lengthofx; i++)
if((x[i]<= 'a') && (x[i]>='z'))
cout << (char)(x[i]+32);
cout << " ";
for (int j=0; j < lengthofy; j++)
if ((y[j]<= 'a') && (y[j]>='z'))
cout << (char)(y[j]+32);
}
int main() {
int x[2][3] = {{3, 1, 4}, {1, 5, 9}};
cout << "Smallest column sum = " << smallCol (x, 2, 3) << endl;
string a,b;
cout << "Please enter two words: ";
cin >> a >> b;
cout << "Words in uppercase: ";
toUpperCase(a,b);
cout << endl;
cout << "Words in lowercase: ";
toLowerCase(a,b);
cout << endl;
return 0;
}
outcome:
Smallest column sum = 4
Please enter two words: hello hello
Words in uppercase: HELLO HELLO
Words in lowercase:
Is it because the user input is taken by the toUpperCase function so the toLowerCase function gets no input?
If yes, please fix for me, thank you so much!
Explanation / Answer
There looks to be problem in the toLowerCase function.
void toLowerCase(string x, string y){
int lengthofx = x.length();
int lengthofy = y.length();
for (int i=0; i < lengthofx; i++)
if((x[i]<= 'a') && (x[i]>='z'))
cout << (char)(x[i]+32);
cout << " ";
for (int j=0; j < lengthofy; j++)
if ((y[j]<= 'a') && (y[j]>='z'))
cout << (char)(y[j]+32);
}
In the above piece of code if condition( if((x[i]<= 'a') && (x[i]>='z')) )will never become true as if the char has ASCII value less than or equal to that of a ,it can never have ASCII value greater than or equal to that of z and hence cout statement following if condition will never be executed.
Try the following piece of code and check the output.
void toLowerCase(string x, string y){
int lengthofx = x.length();
int lengthofy = y.length();
for (int i=0; i < lengthofx; i++)
if((x[i]>= 'A') && (x[i]<='Z')) // Change if condition like this
cout << (char)(x[i]+32); else cout<<(char)x[i]; //Added the else condition in case char is already lowercase ,print as it is. cout << " ";
for (int j=0; j < lengthofy; j++)
if ((y[j]>= 'A') &&(y[j] <='Z')) cout << (char)(y[j]+32); else cout<< (char)y[j];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.