简体   繁体   中英

removing vowels from c++ string

char arr[5000];
ifstream is("test.txt"); 
is.get(arr,5000);
int i = 0;
int j = 0;
cout << arr << endl;
char anar[5000];
while (arr[i] != '\0')
{
    if (arr[i] == 'i' || arr[i] == 'a' || arr[i] == 'e' ||
    arr[i] == 'o' || arr[i] == 'u')
        {
        ++i; 
        }
    else anar[j] = arr[i]; ++j; ++i; 
}++j; anar[j] = '\0';
cout << anar << endl; 
ofstream os("test.txt"); 
os.write(anar, sizeof(char)); 
cout << "written successfully" << endl;

should read the data from a file and delete the vowels from this string. After deleting vowels, it should assign the result to another string. But vowels seem strange characters and the writen file is only one character long.

How big do you think sizeof(char) is? So how many characters is this going to write?

os.write(anar, sizeof(char)); 

You actually have j characters in your array, so this works

os.write(anar, j); 

But since you have a null terminated character array even simpler would be

os << anar;

Some other errors, look at this loop

while (arr[i] != '\0')
{
    if (arr[i] == 'i' || arr[i] == 'a' || arr[i] == 'e' ||
    arr[i] == 'o' || arr[i] == 'u')
        {
        ++i; 
        }
    else anar[j] = arr[i]; ++j; ++i; 
}++j; anar[j] = '\0';

It looks like you are missing {} around the else part of the if statement. You also have an extra ++j after the while loop for some reason. Here's how it should look (I think)

while (arr[i] != '\0')
{
    if (arr[i] == 'i' || arr[i] == 'a' || arr[i] == 'e' ||
    arr[i] == 'o' || arr[i] == 'u')
    {
        ++i; 
    }
    else
    {
        anar[j] = arr[i];
        ++j;
        ++i;
    } 
}
anar[j] = '\0';

Notice how much easier these problems are to spot if you get into the habit of consistently indenting your code. You should do this.

BTW there are no C++ strings in your code, only character arrays.

A very good answer has been given by john already. So, the problem is solved.

I would like to recommend to you to learn a little bit about C++ and all the existing libraries. Especially the C++ - algorithms library is very powerful.

Look at the below program:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>


int main() {

    // Open files and check, if they could be opened
    if (std::ifstream is("withvowels.txt"); is)
        if (std::ofstream os("withoutvowels.txt"); os)

            // Copy file and remove vowels
            std::copy_if(std::istreambuf_iterator<char>(is), {}, std::ostreambuf_iterator<char>(os), [](const char c) { return !((0x208222 >> (c & 0x1f)) & 1); });
}

So, in essence, we have just 3 statements: 2 times if with initializer. And then one copy_if with a lambda for vowel detection.

If you want to know more about the lambda and vowel detection you can read in one of my other posts here .


EDIT

Op asked, how to read the file into a std::string . I added a new piece of code, where I first read the complete file into a std::string and then erase / remove the vowels. The result is shown on std::cout

Please see:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <string>

int main() {
    // Open file and check, if it could be opened
    if (std::ifstream is("r:\\withvowels.txt"); is) {

        // Read the complete file into string variable s
        std::string s(std::istreambuf_iterator<char>(is), {});

        // Remove all vowels from string
        s.erase(std::remove_if(s.begin(), s.end(), [](const char c) { return ((0x208222 >> (c & 0x1f)) & 1); }), s.end());

        // Show result
        std::cout << s;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM