Hello, I am new to C++ and I trying to create a program that inverts a strings lowercase letters to upper and vice-versa.
I believe the error is in what I am returning within my function. Below is what I have written so far.
The program when compiled does not produce any errors, but after inputting the string, the output is just the same string I inputted.
Both my main.cpp and manip.cpp have included my header file which contains all of the preprocessor directives such as:
#include<iostream>
#include<string>
#include<cctype>
#include<iomanip>
and,
using namespace std; // I know this is not convention, but professor requires it this way.
> Does this mean outside of the for loop I should assigned input[i] to input?
No, you need not do anything, input is a string while input[i] refers to the ith character of that string, when you modify input[i], input string is also modified.
Now regarding your code
c = isalpha(input[i]);
This line isn't right.
isalpha() returns true if input[i] is an alphabet, false otherwise
Thus if input[i] is an alphabet, c is assigned the value 1, not the character.
This is what you need to do
1 2 3 4 5
if (isalpha(input[i])
{
c = input[i]
....convert to upper/lower
}
HINT: If you examine the ASCII table you can see that uppercase and lowercase letters have systematically different ASCII codes which you might take advantage of instead of using toupper which, even though it shows a lot of promise, might not be successful.
1 2 3 4 5
char xx = 78;
cout << xx << endl;
char yy = 110
cout << yy endl;
Yeah, I can elaborate. The reason I said it that way is that, on face value, those are the functions I also first explored to solve the problem. So I looked up their function reference and decided, well maybe toupper() and tolower() are worthwhile to keep going with (and I don't want to stop you) but even if I can get it to work, the ASCII code way is less trouble.
Yes, all of your suggestions were a big help! Thank you very much.
I see why it was outputting the same string I was inputting. Like @ a k n said earlier, the function is just returning the character with an Upper/Lower case character depending.
So in order to print the result it is necessary to assign the new value to a variable then print the variable.
So in order to print the result it is necessary to assign the new value to a variable then print the variable.
Yes in this case. If you haven't already learned it there is another method called pass by reference which enables you to pass the input but instead of making a copy within the function this way here, you can change the original string.
I fail to see how directly exploiting ASCII is better than using the more intuitive and portable ctype functions ?
From the function names itself, it is obvious what they are doing while your method assumes knowledge that in ASCII alphabets are laid out sequentially, which isn't obvious to a beginner. Nor did you answer my question on how it might not be successful.
Never mind the fact that virtually every platform we'll encounter will be ASCII compatible, it sounds like a good practice to me to handle things like this with a proper function (even though things like EBCDIC exist).