Replacing vowels with symbols

Hey!

I am a total noob in c++ programing. And i wanna learn it very bad:D.
So for anew task i have to write a program that reads data from text and than replace all vowels with some symbol. I know how to erad text from a file, but than i dont know how to tell the program to change all vowels with *. I dont know how to, when i read text from file, store this text in some string or what. Than i think i must do, that the programme compares vowels with every letter in the text, and when the vowel matches a letter it changes it to *. But i dont know how to put this in code:D

So if someone could help me i would be very happy:P

Ty all
you mean "hello" would become "h*ll*"?

for each char, in string str if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') str[i] = '*';

if you are dealing with c-strings, the loop would be for(int i = 0; str[i] != 0; i++) because the last char of c-string is '\0'.

if you use std::string, for(int i = 0; i < str.length(); i++)
Aha ok. And how do i get the text to str[i]. I would you function for this than call it in main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void vowels()
{
   ifstream read;
   read.open("vowels.txt");
   while (!read.eof())
   {
       char str=read.get();
        for(int i = 0; i < str.length(); i++)
        {
       
        if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') 
        {
        str[i] = '*';
          }
      }
}      


Something like this? I think this code is wrong but i dunno how to write it otherwise
1
2
3
4
5
6
7
8
9
10
ifstream file("vowels.txt";
std::string stdstr; /*or*/ char cstr[80];
//then choose one
file >> stdstr;//reads one word
//or
getline(file, stdstr);//reads the whole line
//or
file >> cstr;
//or
file.getline(cstr, 80);//whole line (or at least 80 symbols) 
Damm i dont get it... Dont understand what i need to do :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main ()
{
    ifstream file("vowels.txt");
    char cstr[80];
    
    while (!file.eof())
    {

    file.getline(cstr,80);
    
    
    }




system("pause");
return 0;
}


you mean something like this?
But i dont know where to put for sentence now. And i dont know what is the middle parameter for for sentence.

for(int i=0; .........; i++)
Last edited on
it's good so far.
now, you know what code you need. you just have to decide where you need it.
before line 4? there cstr is undeclared so you can't operate on it.
on line 5? there cstr exists, but there is nothing in it yet (there is rubbish)
after on line 18? the program will never reach that.
line 14? on every cycle of the while loop a line is read and stored in cstr. new lines overwrite the old ones therefore you would only be able to change the last line after the loop has ended.
line 8? on the first cycle, line 9 will read a string. then on the second cycle line 8 will be able to change it. yay. though I wonder what will line 8 be changing on the first cycle? and will the last line be changed?

so, what line remains? you could just paste the code where you want and try it..
isnt cstr declared with char?

And i still cant get it to work... Dunno what code i need to put in between :D
cstr is a char array.

you know the code. I already wrote it. look at my first post. You combined it correctly in your 2nd post, just you have to use the other for loop here (though in many cases your life would be simpler if you used std::string, so you may want to get used to those at some point)

an I forgot about your other question the last time. the second part of the for( ; ; ) is a condition. when this condition is not met, the loop ends.
Last edited on
Hmm i will try something to do with this code tonight hope i will get it right...
Just 1 thing....
This code : file.getline(cstr,80); does it gets only 1 line from txt file? What if txt has more than 1 line in it ? Will it get the whole txt?
good luck.

getline will read one line. if you call it again, it will read another line. that is way it is in a while(!file.eof()) loop
Last edited on
Ok my last try for tonight, can you pls tell me if i am any closer to the solution :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<cstdio>
#include<fstream>
#include<string>

int main ()
{
    ifstream file("vowels.txt");

    char cstr[80];
    
    while (!file.eof())
    {

    file.getline(cstr,80);
    
    
    
    for(int i = 0; i < cstr.length(); i++)
    {
            if(cstr[i] == 'a' || cstr[i] == 'e' || cstr[i] == 'i' || cstr[i] == 'o' || cstr[i] == 'u') 
            {
            cstr[i] = '*';
            }
    
    }


}

system("pause");
return 0;
}
length() is a method of std::string.
as I said, for c-strings use for(int i = 0; cstr[i] != 0; i++)
you may also use for(int i = 0; i < strlen(cstr); i++) if you find that easier to understand.

other than that it should work. just remember to add some output :)
ok i change the for sentence with this1for(int i = 0; i < strlen(cstr); i++)

When i compile the code there are some errors, but i donnu why :D

`ifstream' undeclared (first use this function)
`;' before "file"
`file' undeclared (first use this function)

I think all of this is declared or am i missing something. And i also want to ask if i need to close this document somewhere? I open it with ifstream("wofels.txt"); Do i need to close it at the end ?

Tomorow i will look at output data so i save this in some txt files, but first i would like to run this code without errors:D
closed account (3hM2Nwbp)
ifstream is in the namespace 'std' std::ifstream
Ok tnx, i got the code working now:D
Now i have only 1 more problem, whats the right way to do output data. Something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
    ifstream file("vowels.txt");
    ofstream exit("text.txt");

    char cstr[80];
    
    while (!file.eof())
    {

    file.getline(cstr,80);
    
    
    
    for(int i = 0; i < strlen(cstr); i++)
    {
            if(cstr[i] == 'a' || cstr[i] == 'e' || cstr[i] == 'i' || cstr[i] == 'o' || cstr[i] == 'u') 
            {
            cstr[i] = '*';
            }
    
    }
    cout<<cstr;
   
    }
    file.close();
    exit.close();


}
Ok i got it. But i have 1 more problem, if input text is :
Today is monday.
Tomorrow will rain.

the output text looks like : Today is monday.Tomorrow will rain.

How would i get that the output text would be: the same as input... Not all text in one line...

Is it possible to read the text with char cstr= file.get() so it reads whole text?


for output method i used exit.write(cstr, strlen(cstr));
Last edited on
indeed. I didn't thing about that.
you could either write output_file << cstr << '\n';//or std::endl;
or change your program entirely to
1
2
3
4
5
while(!input_file.eof()){
   char c = input_file.get();
   //if c is a vowel, change it to '*'
   output_file.put(c);
}


note: do not use write() for text files. it is intended for binary data. operator << is much more simple to use.
Topic archived. No new replies allowed.