The last word in line not read - c++

I am currently working on a program that read each line from a file and extract the word from the line using specific delimiter.
So basically my code looks like this
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argv, char **argc)
{
ifstream fin(argc[1]);
char delimiter[] = "|,.\n ";
string sentence;
while (getline(fin,sentence)) {
int pos;
pos = sentence.find_first_of(delimiter);
while (pos != string::npos) {
if (pos > 0) {
cout << sentence.substr(0,pos) << endl;
}
sentence =sentence.substr(pos+1);
pos = sentence.find_first_of(delimiter);
}
}
}
However my code didnot read the last word in the line. For example, my file looks like this.
hello world
the output from the program is only the word "hello" but not "world" . I have use '\n' as the delimiter but why didnot it works?.
Any hint would be appreciated.

getline does not save the new line character in the string. For example, if your file has the line
"Hello World\n"
getline will read this string
"Hello World\0"
So your code misses the "World".
Igonoring that sentence is not defined, you could alter your code to work like this:
#include<iostream>
#include<fstream>
using namespace std;
int main(int argv, char *argc)
{
ifstream fin(argc[1]);
char delimiter[]="|,.\n ";
while (getline(fin,sentence)) {
sentence += "\n";
int pos;
pos = find_first_of(sentence,delimiter);
while (pos != string:: npos) {
if (pos > 0) {
cout << sentence.substr(0,pos) << "\n";
}
sentence =sentence.substr(pos+1);
pos = find_first_of(sentence,delimiter);
}
}
}
Note, I borrowed Bill the Lizards more elegant solution of appending the last delimiter. My previous version had a loop exit condition.

Paraphrasing this reference document:
Characters are extracted until the delimiting character (\n) is found, discarded and the remaining characters returned.
Your string doesn't end with an \n, it is ^`hello world`$, so no delimiter or new pos is found.

As others have mentioned, getline doesn't return the newline character at the end. The simplest way to fix your code is to append one to the end of the sentence after the getline call.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argv, char **argc)
{
ifstream fin(argc[1]);
char delimiter[] = "|,.\n ";
string sentence;
while (getline(fin,sentence)) {
sentence += "\n";
int pos;
pos = sentence.find_first_of(delimiter);
while (pos != string::npos) {
if (pos > 0) {
cout << sentence.substr(0,pos) << endl;
}
sentence =sentence.substr(pos+1);
pos = sentence.find_first_of(delimiter);
}
}
}

Related

Extract all directory names from a text file

I have a text file in which files names, along with their subdirectory names, could appear at any random places. E.g.
input_file.txt
This is a text file. This line has a file name and location Folder1/file1.dat appearing here.
This is another line: Folder2/file2.txt
Here is yet another line with ../Folder3/Folder4/file3.doc filename and location.
This will be on a Linux system; hence the forward-slashes.
I need a C++ code that can extract all the directory names/locations from this type of file. In the above example, the strings to be extracted would be:
Folder1
Folder2
../Folder3/Folder4
Given the above format of the input file, I suppose the algorithm ought to be something like this:
Go through each line in the file and see if the line has a forward-slash (/) anywhere in it.
If a forward-slash is found in a line, extract the substring between the last occurance of the forward-slash (/) in that line and the last space character that appeared before it.
I have tried several different ways, such as below, but just cannot get it to work, I am afraid.
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
using std::cout; using std::endl;
unsigned first, last;
if(argc < 2)
{
cout << "\nPlease give valid file name!"<<endl;
exit(1);
}
std::string para_file_name = argv[1]; //The name of the input file.
std::ifstream configfile(para_file_name);
while (getline(configfile, line)) {
if (line.find(" ")) {
if (line.find(" ")!=std::string::npos) first = line.find(" ");
if (line.find("/")!=std::string::npos) last = line.find("/");
std::string DirName = line.substr (first,last-first);
cout << " DirName = " << DirName << endl;
}
}
The code has to be compatible with versions older than C++11 and cannot use fancy external libraries such as Boost. Just native C++ please.
Not the most concise, but more performant than <regex> and works with C++98.
#include <cstdlib> // exit
#include <fstream> // fstream
#include <iostream> // cout
#include <sstream> // istringstream
#include <string> // getline
int main(int argc, char **argv)
{
if (argc < 2)
{
std::cout << "\nPlease give valid file name!\n";
exit(1);
}
// Load the file in
std::string line;
std::fstream file(argv[1]);
// For each line of file...
while (std::getline(file, line))
{
std::istringstream iss(line);
std::string word;
char delim = ' ';
// For each word of line...
while (std::getline(iss, word, delim))
{
size_t pos = word.find_last_of('/');
// Word includes '/'
if (pos != std::string::npos)
{
std::string dir_name = word.substr(0, pos);
std::cout << dir_name << "\n";
}
}
}
}
Output
Folder1
Folder2
../Folder3/Folder4
Maybe overkill, but you could use regex.
#include <iostream>
#include <regex>
#include <string>
int main() {
std::cmatch m;
std::regex_match("This is another line: Folder2/file2.txt", m,
std::regex(".*?([^/ ]+/)+.*"));
std::cout << m.str(1) << std::endl;
return 0;
}
Output
Folder2/

How to iterate over the words of a sentence in C++?

My input is "Hello World" and my targeted output is "olleH dlroW".
So my idea is to get the sentence into a variable and then loop over the words in the sentence, reverse each of them and finally concatenate them into a new variable.
My question is: how to iterate over the words of the sentence?
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
string reverseword(string word)
{
string rword;
int size = word.length();
while (size >= 0)
{
rword+= word[size];
size = size -1;
}
return rword;
}
int main()
{
string sentence;
cout<<"Enter the word/sentence to be reversed: ";
cin >> sentence;
string rsentence;
// for every word in the sentence do
{
rword = reverseword(word);
rsentence = rsentence + " " + rword;
}
cout<<rword;
return 0;
}
Before you can iterate over words in a sentence, you need to read a sentence from input. This line
cin >> sentence;
reads the first word of a sentence, not the whole sentence. Use getline instead:
std::getline(std::cin, sentence);
With sentence in memory, you can iterate it word-by-word using istream_iterator as follows:
stringstream ss(sentence);
for (auto w = istream_iterator<string>(ss) ; w != istream_iterator<string>() ; w++) {
string &word = *w;
...
}
Demo.
Here is a solution that uses find and reverse to achieve the output:
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string sentence;
std::getline(std::cin, sentence);
std::cout << sentence << std::endl;
size_t cpos = 0;
size_t npos = 0;
while((npos = sentence.find(' ', cpos)) != std::string::npos)
{
std::reverse(sentence.begin() + cpos, sentence.begin() + npos);
cpos = npos + 1;
}
std::reverse(sentence.begin() + cpos, sentence.end());
std::cout << sentence << std::endl;
return 0;
}
Input:
this is a nice day
Output:
this is a nice day
siht si a ecin yad
for(short i=0;i<sentence.length();i++){
if(sentence[i] == ' '){
counter++;
i++;
}
words[counter] += sentence[i];
}
Note the above loop to split the sentence with space and store it to a string array, words[]
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
string reverseword(string word) // function to reverse a word
{
string rword;
int size = word.length();
while (size >= 0)
{
rword+= word[size];
size = size -1;
}
return rword;
}
int main()
{
string sentence;
cout << "Enter the word/sentence to be reversed: ";
std::getline(std::cin, sentence);
string rsentence;
string words[100];
string rword;
short counter = 0;
for(short i=0; i<sentence.length(); i++){ // looping till ' ' and adding each word to string array words
if(sentence[i] == ' '){
counter++;
i++;
}
words[counter] += sentence[i];
}
for(int i = 0; i <= counter; i++) // calling reverse function for each words
{
rword = reverseword(words[i]);
rsentence = rsentence + " " + rword; // concatenating reversed words
}
cout << rsentence; // show reversed word
return 0;
}
I have corrected the code. Hope this helps...!!
NB : You were using cin to read space seperated string that is not possible. You must use std::getline(std::cin, sentence) to read space separated strings.
You can also use std::reverse() to reverse a string
Please refer to Most elegant way to split a string?
to split your sentence into tokens(words)
then, iterate over the new list of words to perform any operation
An answers above gives a way to convert your input to words, i.e., cin >> sentence returns a "word" (so, just call it repeatedly).
However, this brings up the question of what is a "word". You would like to translate a computer construct - string of characters - into a more complex form - words. So, you must define what you mean when you want words. It can be as simple as "space" separated substrings or your string - then use the split function, or read your string a word at a time (cin >> word)
Or you may have more stringent requirements, like they can't include punctuation (like a period at the end of a sentence) or numbers. Then think about using Regex and word patterns (like, "\w+").
Or you may want "real" words like you would find in a dictionary. Then you need to take into account your locale, parse your input into chunks (using split, Regex, or something), and look up each chunk in a human language dictionary.
In other words, "word" parsing is only as simple or complex as your requirements are.
With Boost you could use the boost::split function:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <boost/algorithm/string.hpp>
int main()
{
std::string sentence = "Hello world";
std::vector<std::string> words;
boost::split(words, sentence, boost::is_any_of(" "));
std::string rsentence;
for (std::string word : words) // Iterate by value to keep the original data.
{
std::reverse(word.begin(), word.end());
rsentence += word + " "; // Add the separator again.
}
boost::trim(rsentence); // Remove the last space.
std::cout << rsentence << std::endl;
return 0;
}
This answer is my humble contribution to the fight against global warming.
#include <string>
#include <iostream>
#include <algorithm>
#include <cctype>
int main()
{
std::string sentence;
while (std::getline(std::cin, sentence))
{
auto ws = sentence.begin();
while (ws != sentence.end())
{
while (std::isspace(*ws)) ++ws;
auto we = ws;
while (we != sentence.end() && !std::isspace(*we)) ++we;
std::reverse(ws, we);
ws = we;
}
std::cout << sentence << "\n";
}
}
This assumes "word" is defined as "a sequence of non-whitespace characters". It is easy to substitute a different character class instead of "non-whitespace", e.g. for alphanumeric characters use std::isalnum. A definition that reflects the real-world notion of word as e.g. used in natural language sciences is far far beyond the scope of this answer.

How can identify a blank line in C++?

I'm reading in information from a file. I need a counter that counts how many text filled lines there are. I need that counter to stop if there is any blank line (even if there are text filled lines after that blank line).
How would I do this? Because I'm not exactly sure how to identify a blank line to stop the counter there.
If you are using std::getline then you can just detect an empty line by checking if the std::string you have just read is empty.
std::ifstream stream;
stream.open("file.txt");
std::string text;
while(std::getline(stream,text))
if(!text.size())
std::cout << "empty" << std::endl;
I'd suggest using std::getline for it:
#include <string>
#include <iostream>
int main()
{
unsigned counter = 0;
std::string line;
while (std::getline(std::cin, line) && line != "")
++counter;
std::cout << counter << std::endl;
return 0;
}
Since #Edward made a comment about handling whitespace and it might be important. When lines with only whitespaces are considered as "empty lines" too I'd suggest changing it to:
#include <string>
#include <iostream>
#include <algorithm>
#include <cctype>
int main()
{
unsigned counter = 0;
std::string line;
while (std::getline(std::cin, line) &&
std::find_if_not( line.begin(), line.end(), std::isspace != line.end()) {
++counter;
}
std::cout << counter << std::endl;
return 0;
}
It's quite verbose, but the advantage is that it uses std::isspace to handle all different kind of spaces (e.g. ' ', '\t', '\v', etc...) and you don't have to worry if you handle them correctly.
In C++ 11 you can use,
std::isblank
In a loop, read all lines, one-by-one, into a single string variable. You can use a std::getline function for that.
Each time after reading a line into that variable, check its length. If it's zero, then the line is empty, and in that case break the loop.
However, checking for empty lines like is not always really right thing. If you are sure that the lines will be empty, then it's OK. But if your "empty" lines can contain whitespaces,
123 2 312 3213
12 3123 123
// <--- Here are SPACEs. Is it "empty"?
123 123 213
123 21312 3
then you might need to not check for "zero-length", but rather whether "all characters are whitespaces".
No error checking, no protection, just a simple example... It is not tested, but you get the gist.
#incldue <iostream>
#include <string>
using namespace std;
int main()
{
string str = "";
int blank = 0, text = 0;
ifstream myfile;
myfile.open("pathToFile");
while(getline(myfile,str))
{
if(str == "")
{
++blank;
}
else
{
++text;
}
}
cout << "Blank: " << blank << "\t" << "With text: " << text;
return 0;
}
Simply check the string length and use a line counter. When the string length is zero (i.e., the string is blank) print the line counter. Sample code is provided for your reference:
// Reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
int i = 0;
if (myfile.is_open())
{
while (getline (myfile, line))
{
i++;
// cout << line << '\n';
if (line.length() == 0)
break;
}
cout << "blank line found at line no. " << i << "\n";
myfile.close();
}
else
cout << "Unable to open file";
return 0;
}

How to convert a sentence from upper case to lower case that contains space and numbers

I'm trying to convert a sentence from upper case to lowercase. I also write a code but I stopper when a space is appear. How can I fix this problem and convert the whole sentence? Here is my code
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[100];
cin>>str;
for(int i=0;i<strlen(str);i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
str[i]=str[i]+32;
}
}
cout<<str<<endl;
return 0;
}
It's because of theinput operator >>, it breaks on space. If you want to read a whole line then use std::getline to read into a std::string instead.
Then read about the C++ standard algorithms, like for example std::transform. Also, std::tolower doesn't modify anything that's not an upper-case letter, so it's a good function to use.
The error is because operator>> delimites on spaces. The alternative is to use getline. See the following example:
#include <cstring>
#include <iostream>
#include <string>
int main() {
std::string s;
std::getline(std::cin, s);
std::cout << "Original string: " << s << std::endl;
if (!std::cin.fail()) {
const int len = strlen(s.c_str());
for (size_t i = 0; len > i; ++i) {
if ((s[i] >= 'A') && (s[i] <= 'Z'))
s[i] = s[i] - 'A' + 'a';
}
}
std::cout << "New string: " << s << std::endl;
return 0;
}
The reason input stops at whitespace is because formatted input is delimited by whitespace characters (among others). You will need unformatted I/O in order to extract the entire string into str. One way to do this is to use std::istream::getline:
std::cin.getline(str, 100, '\n');
It's also useful to check if the input succeeded by using gcount:
if (std::cin.getline(str, 100, '\n') && std::cin.gcount())
{
...
}
But in practice it's recommended that you use the standard string object std::string which holds a dynamic buffer. To extract the entire input you use std::getline:
std::string str;
if (std::getline(std::cin, str)
{
...
}
Here is one of the examples of doing it using transform function.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string str;
if (getline(cin, str))
{
transform(str.begin(), str.end(), str.begin(), ptr_fun<int, int>(toupper));
}
cout << str << endl;
return 0;
}

Trying to read from a file and skip punctuation in C++, tips?

I'm trying to read from a file, and make a vector of all the words from the file. What I tried to do below is have the user input the filename, and then have the code open the file, and skip characters if they aren't alphanumeric, then input that to a file.
Right now it just closes immediately when I input the filename. Any idea what I could be doing wrong?
#include <vector>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
string line; //for storing words
vector<string> words; //unspecified size vector
string whichbook;
cout << "Welcome to the book analysis program. Please input the filename of the book you would like to analyze: ";
cin >> whichbook;
cout << endl;
ifstream bookread;
//could be issue
//ofstream bookoutput("results.txt");
bookread.open(whichbook.c_str());
//assert(!bookread.fail());
if(bookread.is_open()){
while(bookread.good()){
getline(bookread, line);
cout << line;
while(isalnum(bookread)){
words.push_back(bookread);
}
}
}
cout << words[];
}
I think I'd do the job a bit differently. Since you want to ignore all but alphanumeric characters, I'd start by defining a locale that treats all other characters as white space:
struct digits_only: std::ctype<char> {
digits_only(): std::ctype<char>(get_table()) {}
static std::ctype_base::mask const* get_table() {
static std::vector<std::ctype_base::mask>
rc(std::ctype<char>::table_size,std::ctype_base::space);
std::fill(&rc['0'], &rc['9']+1, std::ctype_base::digit);
std::fill(&rc['a'], &rc['z']+1, std::ctype_base::lower);
std::fill(&rc['A'], &rc['Z']+1, std::ctype_base::upper);
return &rc[0];
}
};
That makes reading words/numbers from the stream quite trivial. For example:
int main() {
char const test[] = "This is a bunch=of-words and 2#numbers#4(with)stuff to\tseparate,them, I think.";
std::istringstream infile(test);
infile.imbue(std::locale(std::locale(), new digits_only));
std::copy(std::istream_iterator<std::string>(infile),
std::istream_iterator<std::string>(),
std::ostream_iterator<std::string>(std::cout, "\n"));
return 0;
}
For the moment, I've copied the words/numbers to standard output, but copying to a vector just means giving a different iterator to std::copy. For real use, we'd undoubtedly want to get the data from an std::ifstream as well, but (again) it's just a matter of supplying the correct iterator. Just open the file, imbue it with the locale, and read your words/numbers. All the punctuation, etc., will be ignored automatically.
The following would read every line, skip non-alpha numeric characters and add each line as an item to the output vector. You can adapt it so it outputs words instead of lines. I did not want to provide the entire solution, as this looks a bit like a homework problem.
#include <vector>
#include <sstream>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string line; //for storing words
vector<string> words; //unspecified size vector
string whichbook;
cout << "Welcome to the book analysis program. Please input the filename of the book you would like to analyze: ";
cin >> whichbook;
cout << endl;
ifstream bookread;
//could be issue
//ofstream bookoutput("results.txt");
bookread.open(whichbook.c_str());
//assert(!bookread.fail());
if(bookread.is_open()){
while(!(bookread.eof())){
line = "";
getline(bookread, line);
string lineToAdd = "";
for(int i = 0 ; i < line.size(); ++i)
{
if(isalnum(line[i]) || line[i] == ' ')
{
if(line[i] == ' ')
lineToAdd.append(" ");
else
{ // just add the newly read character to the string 'lineToAdd'
stringstream ss;
string s;
ss << line[i];
ss >> s;
lineToAdd.append(s);
}
}
}
words.push_back(lineToAdd);
}
}
for(int i = 0 ; i < words.size(); ++i)
cout << words[i] + " ";
return 0;
}