Script that replace each repeated letter in a string with one letter - c++

I have a script which takes a word from input file and outputs customizated word in output file.
For example, in input file I have word ccccccaaaaaannnnndlllllleeeeeand I need to edit the word it to look like candle and output in other file. So could you please give me a example of it?
So far I have:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string vards;
std::ifstream input_file("virkne3.in");
input_file >> vards;
std::ofstream output_file("virkne3.out");
output_file << vards.erase(std::unique(vards.begin(), vards.end()), vards.end());
cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}

Assuming you mean consecutive repeated letters, std::unique does exactly that.
vards.erase(std::unique(vards.begin(), vards.end()), vards.end());

Related

`getLine` function skipping the final, blank line in an input file

I want to find out the character length of each of the lines in a separate file, which is test.txt. test.txt contains two lines: the first contains hello, and the second is blank.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
ifstream inFile;
inFile.open("test.txt");
string currline;
while (getline(inFile, currline)) {
cout << currline.length() << endl;
}
}
When executing this program and inputting 'test.txt', I was expecting the output to be 5 0. However, the output was simply 5, which meant that the second line of test.txt was being ignored. How do I solve this issue without modifying the input file, or using a new modified file?
Your test.txt contains only one line - "hello" with a trailing '\n', rather than two lines.

Finding certain characters in a line of string

I want to be able to a string that contains certain characters in a file that contains one string per line.
#include <iostream>
#include <fstream>
#include <string>
int main(){
string line;
ifstream infile;
infile.open("words.txt");
while(getline(infile, line,' ')){
if(line.find('z')){
cout << line;
}
}
}
That's my attempt at finding all the string that contains the character z.
The text file contains random strings such as
fhwaofhz
cbnooeht
rhowhrj
perwqreh
dsladsap
zpuaszu
so with my implementation, it should only print out the strings with the character z in it. However, it seems to be reprinting out all the contents from the text file again.
Problem:
In your file the strings aren't separated by a space (' ') which is the end delimiter, they are separated by a end of line ('\n'), that is a different character. As a consequence, in the first getline everything goes to line. line contains all the text in the file, including z's, so all the content is printed. Finally, the code exits the while block after running once because getline reaches the end of the file and fails.
If you run this code
#include <iostream>
#include <fstream>
#include <string>
int main(){
std::string line;
std::ifstream infile;
infile.open("words.txt");
while(getline(infile, line,' ')){
std::cout << "Hi";
if(line.find('z')){
std::cout << line;
}
}
}
"Hi" will be only printed once. That is because the while block is only executed once.
Additionaly, see that line.find('z') won't return 0 if not match is found, it will return npos. See it running this code (As it says here):
#include <iostream>
#include <fstream>
#include <string>
int main(){
std::string line;
std::ifstream infile;
infile.open("words.txt");
while(getline(infile,line)){
std::cout << line.find('z');
if(line.find('z')){
std::cout << line << "\n";
}
}
}
Solution:
Use getline(infile,line) that is more suitable for this case and replace if(line.find('z')) with if(line.find('z') != line.npos).
while(getline(infile,line)){
if(line.find('z') != line.npos){
std::cout << line << "\n";
}
}
If you need to put more than one string per line you can use the operator >> of ifstream.
Additional information:
Note that the code you posted won't compile because string, cout and ifstream are in the namespace std. Probably it was a part of a longer file where you were using using namespace std;. If that is the case, consider that it is a bad practice (More info here).
Full code:
#include <iostream>
#include <fstream>
#include <string>
int main(){
std::string line;
std::ifstream infile;
infile.open("words.txt");
while(getline(infile,line)){
if(line.find('z') != line.npos){
std::cout << line << "\n";
}
}
}
getline extracts characters from the source and stores them into the variable line until the delimitation character is found. Your delimiter character is a space (" "), which isn't present in the file, so line will contain the whole file.
Try getline(infile, line, '\n') or simply getline(infile, line) instead.
The method find returns the index of the found character, where 0 is a perfectly valid index. If the character is not found, it returns npos. This is a special value whcih indicates "not found", and it's nonzero to allow 0 to refer to a valid index. So the correct check is:
if (line.find('z') != string::npos)
{
// found
}

How do I count individual number of characters in a word from a text file?

I have an assignment to read a text from a file and remove all of the words which have an odd number of characters. I don't really know where to start because I'm still learning the basics and could use some help. The biggest question I have is how do I count individual words. Like how would I seperate the counting from word to word in a text file. Thank you for the help
//Sorry for the lack of clarity, English is my second language and it can be difficult some times to specify what I need.
I have an assignment to write a c++ program where it:
(1)Reads a text file and determines which words are even and which are odd
(2)Then it takes even words and reverses the order of characters in each even word.
So for example I have a moderate size text. It picks out even words and reverses their character order.
So far I have written this code and I don't know if it is any good to continue with because I don't know how to reverse the order of characters.
#include <iostream>
#include <string>
#include <string.h>
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
string word;
ifstream f("text.txt");
if (f.is_open())
{
while (f >> word)
{
if (word.length() % 2 == 0)
cout << word << endl;
}
f.close();
}
else
cout << "file is not open" << '\n';
}

Getline only printing out last line when called multiple times

So this is a fairly simple example of a program where I'm trying to output the first two lines of an input text file. The ifstream should be a global variable, and the testGetFile() function is necessary (I have not done the actual text processing needed in this code.) I'm trying to figure out why this is cout-ing only the SECOND line of the input file. Any help will be appreciated!
Thanks in advance!
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
ifstream input;
string testGetFile(){
string result;
getline(input,result);
return result;
}
int main(){
input.open("testInput.txt");
cout<< testGetFile();
cout<< testGetFile();
return 0;
}

Reading strings and integers from .txt file and printing output as strings only

I'm new to C++, and I'm trying to write a short C++ program that reads lines of
text from a file, with each line containing one integer key and one alphanumeric string value (no embedded whitespace). The number of lines is not known in advance, (i.e., keep reading lines until end of file is reached). The program needs to use the 'std::map' data structure to store integers and strings read from input (and to associate integers with strings). The program then needs to output string values (but not integer values) to standard output, 1 per line, sorted by integer key values (smallest to largest). So, for example, suppose I have a text file called "data.txt" which contains the following three lines:
10 dog
-50 horse
0 cat
-12 zebra
14 walrus
The output should then be:
horse
zebra
cat
dog
walrus
I've pasted below the progress I've made so far on my C++ program:
#include <fstream>
#include <iostream>
#include <map>
using namespace std;
using std::map;
int main ()
{
string name;
signed int value;
ifstream myfile ("data.txt");
while (! myfile.eof() )
{
getline(myfile,name,'\n');
myfile >> value >> name;
cout << name << endl;
}
return 0;
myfile.close();
}
Unfortunately, this produces the following incorrect output:
horse
cat
zebra
walrus
If anyone has any tips, hints, suggestions, etc. on changes and revisions
I need to make to the program to get it to work as needed, can you please
let me know?
Thanks!
See it:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string name;
int value;
ifstream myfile("text.txt", ifstream::in);
while(myfile >> value >> name)
cout << name << endl;
return 0;
}
You are having problems because you attempt to read each line twice: first with getline and then with operator>>.
You haven't actually used std::map in any regard, at all. You need to insert the integer/string pair into the map, and then iterate over it as the output. And there's no need to close() the stream.
Instead of using "! myfile.eof()" use this code it will help.
ifstream is;
string srg;
is.open(filename);
while(getline(is,srg))
{//your code
}