-2

I have seen many good code to do this problem. I am new to coding. My question is where my logic went wrong. I think that problem is with second string str1. I din't initialize it. even when I am printing element by element withing if , it is working. but it is not working, when I am trying to print whole string str1.

#include<iostream>
#include<string>


using namespace std;


int main()
{
 string str = "Hello, have a good day", str1;





 for (int i = 0, j =0; i < str.length(); ++i)
 {


    if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
        if (str[i] == 'I' || str[i] == 'i' || str[i] == 'U' || str[i] == 'u' || str[i] == 'O' || str[i] == 'o' || 
            str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' )
        {
            str1[j] = str[i];
            //std::cout << str1[j] ;
            j++;
        }
    else 
    {
        str1[j] = str[i];
        j++;

    }   

 }

 cout << str1 <<'\n';

}

output is just blank.

  • 5
    `str1` is an empty string (size == 0) so you can't do `str1[j] = str[i];` you can `str1.push_back(str[i]);` – drescherjm Dec 23 '18 at 15:22
  • What do you think happens when `str1[j]` is accessed for the first time? – CinCout Dec 23 '18 at 15:23
  • But It is string variable, not char array, then why can't I do it. I just want to know explanation. @drescherjm – NAZMUL HUSSAIN Dec 23 '18 at 15:24
  • I gave you a solution. Use `str1.push_back(str[i]);` instead of `str1[j] = str[i];` – drescherjm Dec 23 '18 at 15:25
  • @CinCout that would contain some garbage value. – NAZMUL HUSSAIN Dec 23 '18 at 15:26
  • 1
    I think you should learn about [`std::isalpha`](https://en.cppreference.com/w/cpp/string/byte/isalpha) and [`std::toupper`](https://en.cppreference.com/w/cpp/string/byte/toupper). – Some programmer dude Dec 23 '18 at 15:28
  • Note that this code makes assumptions about the encoding of characters that are not required by the language definition. They're valid when the system uses ASCII, but in EBCDIC, for example, there are a handful of non-letters for which `str[i] >= 'a' && str[i] <= 'z'` is true. That's why the standard library has things like `std::isalpha`. – Pete Becker Dec 23 '18 at 16:42
  • @PeteBecker I just read about std: :isalpha and std::toupper. how do i use that in my code. could you suggest me, please. – NAZMUL HUSSAIN Dec 23 '18 at 17:20
  • _"that would contain some garbage value"_ That's one possible manifestation of the undefined behaviour that your program has. In truth, you simply _shall not attempt to use elements that don't exist_. If your string is empty, it has no characters, and `str1[j]` is _broken_, simple as. Which C++ book are you using? – Lightness Races in Orbit Dec 23 '18 at 17:20
  • I am not using any book. I am just going through codesdope.com tutorial and doing all practice set. @LightnessRacesinOrbit – NAZMUL HUSSAIN Dec 23 '18 at 17:25
  • @NAZMULHUSSAIN Okay, you should have [a book](https://stackoverflow.com/q/388242/560648). It'll explain things like this. – Lightness Races in Orbit Dec 24 '18 at 13:15
  • @LightnessRacesinOrbit whenever I search for book, i get confused which book , I should start with. can you suggest me. in your link, there is lots of book. can you suggest me precisely, which book should I start with? – NAZMUL HUSSAIN Dec 24 '18 at 14:06
  • @NAZMULHUSSAIN Not really - it depends on you, which is why each book listed is accompanied by a description of it and who it's aimed at – Lightness Races in Orbit Dec 24 '18 at 14:10

2 Answers2

2

The first thing to do is to write a function that determines whether a character is a consonant:

bool is_not_consonant(char ch) {
    static char consonants[] = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
    return std::find(std::begin(consonants), std::end(consonants), ch) == std::end(consonants);
}

Then use that function as a predicate to std::copy_if:

std::string result;
std::string input = whatever;
std::copy_if(std::begin(input), std::end(input),
    std::back_inserter(result),
    is_not_consonant);
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
1

Explanation

The problem is that you don't need the else condition. All you need to do is check for a vowel, and print if found which is rightly covered in your if condition.

Code

Try this:

#include<string>
using namespace std;
int main()
{
    string str = "Hello, have a good day", str1;
    for (int i = 0; i < str.length(); ++i)
    {
        if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
            if (str[i] == 'I' || str[i] == 'i' || str[i] == 'U' || str[i] == 'u' || str[i] == 'O' || str[i] == 'o' || str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' )
            {
                str1 += str[i];
            }

    }
    cout << str1 <<'\n';
}
  • Since it is a string, you can simply use the += operator to add the character. Instead of str1[j] = str[i], you can use str1 +=str[i]. – javan.rajpopat Dec 23 '18 at 15:53
  • @javan.rajput but In this code , spaces and other special character won't be printed. – NAZMUL HUSSAIN Dec 23 '18 at 17:01
  • To remove ONLY the vowels, remove "if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))" from the above code. This will only select the upper and lower case vowels and not add them to str1 – javan.rajpopat Dec 23 '18 at 19:51
  • I do not need to delete vowels. I need to delete consonants. but in your above code , you are also deleting space and other special characters. that is why I used else block. – NAZMUL HUSSAIN Dec 23 '18 at 20:13
  • #include using namespace std; int main() { string str = "Hello, have a good day", str1; for (int i = 0; i < str.length(); ++i) { if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z')){ if (str[i] == 'I' || str[i] == 'i' || str[i] == 'U' || str[i] == 'u' || str[i] == 'O' || str[i] == 'o' || str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' ) { str1 += str[i]; }}else{str1 += str[i];} } cout << str1 <<'\n'; } – javan.rajpopat Dec 24 '18 at 19:55