0

I am trying to solve a problem where I am given a string of lowercase characters and I have to convert the vowels to uppercase. (e.g. : "mother" becomes "mOthEr".)

My attempt

#include <iostream>
#include <cstring>
using namespace std;

char s[20];
int i;

int main()
{
    cin.getline(s,20);
    for(i=0;i<20;i++)
    {
        if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
            s[i]=toupper(s[i]);
    }
    cout<<s;
}

Could someone tell me what I did wrong? I got Wrong Answer on one of the tests.

Student
  • 805
  • 1
  • 8
  • 11
Alexdanut
  • 111
  • 6
  • 12
    What if the string is longer than 20 chars? (like Supercalifragilisticexpialidocious) – hellow Jul 24 '18 at 13:24
  • 3
    Can you show the test that you fail, and your output? – BoBTFish Jul 24 '18 at 13:24
  • 2
    Your code is a very bad mix of C and C++. Either use proper C or C++, but don't mix them like this. – hellow Jul 24 '18 at 13:25
  • 5
    @hellow That's not really very helpful advice, as I would guess that they are a beginner who simply doesn't yet know what is good or bad practice. Especially as many teachers try to teach C++ by first teaching C. – BoBTFish Jul 24 '18 at 13:26
  • I forgot to mention that the problem states that the string has a maximum of 20 characters.Regarding the test I fail,I can't see its input because the website where I encountered this problem doesn't let you see the tests. – Alexdanut Jul 24 '18 at 13:29
  • 2
    Maybe it is, but I'm afraid the comment section does not leave room for a good explanation. I try it: char[20] is bad. You can store up to 19 chars (the 20th is the `\0` terminator) and you can very easily read above the limit. Use `std::string` instead :) Have a look at the example of https://en.cppreference.com/w/cpp/string/basic_string/getline to use getline with a string to read any length. – hellow Jul 24 '18 at 13:29
  • is `y` a vowel, too? – Olivier Sohn Jul 24 '18 at 13:33
  • @hellow thank you,the size was the problem. – Alexdanut Jul 24 '18 at 13:33
  • 1
    Relevant once again: [Do I need to cast to unsigned char before calling toupper?](//stackoverflow.com/q/21805674) – Baum mit Augen Jul 24 '18 at 13:36
  • 1
    If your string is less than 20 characters, you still convert 20 characters. See `std::strlen`. – Thomas Matthews Jul 24 '18 at 13:36
  • Was is something like "Elephants are cool" ? (We need to know what input failed.) – William J Bagshaw Jul 24 '18 at 13:57
  • If your string is max 20 chars, you need array the size of 21 to accommodate terminating `\0`; – Killzone Kid Jul 24 '18 at 14:18

1 Answers1

2

First, as already stated, you are confining the code to a maximun of 20 characters while you can make it more fluid and allowing for variable length by using std::string and std::getline(). You can do that by using the string length function i < input.length().

variable i is declared as a global which is an overkill.

You also need to let the user know what to do; if run as stated, the console will look blank and unless you wrote the program, you have no idea what is going on. You need something like cout << "Enter Line to modify: "; to instruct the user to type something.

Spacing and proper naming convention is something that you need to start doing from day one as to become second nature. Can't tell you how many hours I have wasted trying to figure out what the programmer is using variables for that have names like a, b, cc, and so. Give all your variables meaningful names, you will thank me in the future.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
Angel D
  • 21
  • 2
  • The auto formatting on this page is killing me. I wanted to show: input.length(); – Angel D Jul 24 '18 at 14:03
  • #include #include using namespace std; int main() { string input; cout<<"Enter Line to modify:"< – Angel D Jul 24 '18 at 14:08
  • use the tick mark "`" to get inline code style. To get block code style, indent everything by 4 spaces. – Ben Jones Jul 24 '18 at 14:14
  • Use ` for inline Code. Also: Why don't you edit your answer instead writing comments to it? – Swordfish Jul 24 '18 at 14:14
  • Swordfish - I tried multiple times to edit the answer to no avail. That is why I try the comment route. Thanks to Ben for the link. Just started here so I need to learn the ropes. – Angel D Jul 24 '18 at 15:48