How to parse a string and ignore whitespace? - c++

int line = 0;
string teststring = " ";
string stringarray[100];
while (codeFile.good())
{
getline(codeFile, teststring, ' ');
if(teststring!="" && teststring[0]!='\n' && teststring[0] != 9 && teststring[0] != 10 && teststring[0] != 32 && teststring[0]!=' '
&& teststring!=" " && teststring!=" ")
{
stringarray[line]=teststring; // still stores whitespace :(
cout << stringarray[line] << endl;
line++;
}
}
Hello, I am going through a text file and trying to store each string inside an element of an array but, am having problems with elements storing completely white space.

I have just solve a similar problem, how about this code:
while (codeFile.good())
{
getline(codeFile, teststring);
for(size_t idx = 0; idx < teststring.size(); i++) {
size_t start = idx;
while (teststring[i] != ' ') {
idx++;
}
stringarray[line] = teststring.substr(start, idx - start);
cout << stringarray[line] << endl;
line++;
}
}

Ignoring all the white spaces is exactly what operator>> does.
Your snippet can be rewritten as:
// ...
std::string word;
std::vector<std::string> words;
while ( codeFile >> word )
{
if ( word.empty() ) continue;
std::cout << word << '\n';
words.push_back(std::move(word));
}

Related

C++ Question: string.at function returns a "std::out_of_range" error in terminal

I've been working on this code for a while now and I keep getting the same terminal error. I have narrowed the issue down to two at functions. I've looked it up but the only answer I can seem to find is if the coder used the wrong variable in a for loop or that the variable in the at function isn't indexed properly.
Can't seem to figure out why the str.at() functions specifically are throwing errors when the variable str should be initialized. The at functions in question are the second and fourth if statements inside the do-while loop.
Here's the code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// start main function
int main()
{
ifstream infile;
string line;
string str;
int words = 0;
int charsNotIncludingSpaces = 0;
int charsIncludingSpaces = 0;
int wordsEndingWithE = 0;
int sixLetterWords = 0;
int wordsBeginningWithVowel = 0;
int wordsContainingATE = 0;
int allEs = 0;
int wordsWithAtleastTwoEs = 0;
infile.open("USDictionary.txt");
// read the first line from the file
getline(infile, line);
while(!infile.eof())
{
unsigned int startIndex = 0;
unsigned int endIndex = 0;
// get each word from the line and find the required results
do
{
endIndex = line.find(' ', startIndex);
if(endIndex > 0 && endIndex < line.size())
{
str = line.substr(startIndex, endIndex - startIndex);
}
else
{
str = line.substr(startIndex);
}
startIndex = endIndex + 1;
words++;
charsNotIncludingSpaces += str.size();
if(str.at(str.size() - 1) == 'e')
{
wordsEndingWithE++;
}
if(str.size() == 6)
{
sixLetterWords++;
}
if(str.at(0) == 'a' || str.at(0) == 'A'
|| str.at(0) == 'e' || str.at(0) == 'E'
|| str.at(0) == 'i' || str.at(0) == 'I'
|| str.at(0) == 'o' || str.at(0) == 'O'
|| str.at(0) == 'u' || str.at(0) == 'U')
{
wordsBeginningWithVowel++;
}
unsigned int ateIndex = str.find("ate");
if (ateIndex >= 0 && ateIndex < str.size())
{
wordsContainingATE++;
}
for (unsigned int k = 0; k < str.size(); k++)
{
if(str.at(k) == 'e')
allEs++;
}
if (str.find_first_of('e') != str.find_last_of('e'))
{
wordsWithAtleastTwoEs++;
}
} while (endIndex > 0 && endIndex < line.size());
charsIncludingSpaces += line.size();
// read the next line from the file
getline(infile, line);
}
infile.close();
/*// print the results
cout << "Total number of words in the dictionary: "
<< words << endl;
cout << "Total number of characters in the dictionary (not including white spaces): "
<< charsNotIncludingSpaces << endl;
cout << "Total number of characters in the dictionary (including white spaces): "
<< charsIncludingSpaces << endl;
cout << "Total number of words ending in the letter e: "
<< wordsEndingWithE << endl;
cout << "Total number of 6 letter words: "
<< sixLetterWords << endl;
cout << "Total number of words beginning with a vowel: "
<< wordsBeginningWithVowel << endl;
cout << "Total number of words containing the substring \"ate\": "
<< wordsContainingATE << endl;
cout << "Total number of occurances of the letter e: "
<< allEs << endl;
cout << "Total number of words containing at least two occurances of the letter e: "
<< wordsWithAtleastTwoEs << endl;*/
return 0;
}

How to loop through multiple condition IF statement in C++

I am a newbie in programming and I wrote a piece of code that works, but I am interested in making it scalable.
The following code checks if a single word from a vector list matches with any of the words in another vector list.
if (words[i] != dislike[0] && words[i] != dislike[1] && words[i] != dislike[2] && words[i] != dislike[3]) //check for disliked words
cout << words[i] << '\n';
As visibile, the code does the job by iterating word by word, so if I were to change the nubmer of words in my second vector list, I will need to add it to the IF statement.
Is there a more optimal solution? I have been trying to figure this out for hours, however I had no luck.
Thanks.
P.S. Below is my full code.
int main()
{
// words we dislike
vector<string>dislike = { "broccoli", "coke", "potatoes", "water" };
//take input from user
vector<string> words;
for (string temp; cin >> temp; ) // read whitespace-separated words
words.push_back(temp); // put into vector
//give output
cout << "Number of words: " << words.size() << '\n';
//sort(words); // DONT sort the words
for (int i = 0; i<words.size(); ++i) //go through your words vector
{
if (i == 0 || words[i - 1] != words[i])//remove repeating words
{
if (words[i] != dislike[0] && words[i] != dislike[1] && words[i] != dislike[2] && words[i] != dislike[3]) //check for dislike
cout << words[i] << '\n';
else
cout << "BlEEP!\n"; //print if word is disliked*/
}
}
return 0;
}
What about this?
Adding another loop for that iterates throught all the dislike vector.
int main()
{
// words we dislike
vector<string>dislike = { "broccoli", "coke", "potatoes", "water" };
//take input from user
vector<string> words;
for (string temp; cin >> temp; ) // read whitespace-separated words
words.push_back(temp); // put into vector
//give output
cout << "Number of words: " << words.size() << '\n';
//sort(words); // DONT sort the words
for (int i = 0; i<words.size(); ++i) //go through your words vector
{
if (i == 0 || words[i - 1] != words[i])//remove repeating words
{
for(int j=0;j<dislike.size();j++)
{
int count = 0;
if (words[i] != dislike[j])count++;
if(count == dislike.size())cout << words[i] << '\n'; //check for dislike
else
cout << "BlEEP!\n"; //print if word is disliked*/
}
}
}
return 0;
}

How to find the shortest word in the string C++

I need a help.
I have a function which print Longest Word in sentence.
But how to display shortest word?
string text = "My name is Bob";
void LongestWord(string text)
{
string tmpWord = "";
string maxWord = "";
for(int i=0; i < text.length(); i++)
{
/// If founded space, rewrite word
if(text[i] != ' ')
tmpWord += text[i];
else
tmpWord = "";
/// All the time check word length and if tmpWord > maxWord => Rewrite.
if(tmpWord.length() > maxWord.length())
maxWord=tmpWord;
}
cout << "Longest Word: " << maxWord << endl;
cout << "Word Length: " << maxWord.length() << endl;
}
The suggestion given in the comment section will work, it's just a matter of rearranging your control structures to make it work. i.e
for(int i=0; i < text.length(); i++)
{
/// If founded space, rewrite word
if(text[i] != ' ')
tmpWord += text[i];
else
{
if(minWord.length()==0)//this only happens once
minWord=tmpWord;//for the first word,you need to assign minWord so you have something to compare to
if(tmpWord.length() < minWord.length() )//move this block here
minWord=tmpWord;
tmpWord = "";
}
}
I might add, you can check for a word much easily if you used istringstream with the extraction operator>>. Something like:
#include <sstream>
....
string text="my name is bob";
string tmpWord = "";
string minWord = "";
istringstream ss(text);//defines the input string stream and sets text in the input stream buffer
while(ss.peek()!=EOF)//until the end of the stream
{
ss>>tmpWord;//read a word up to a space
if(minWord.length()==0)//this only happens once
minWord=tmpWord;
if(tmpWord.length() < minWord.length() )
minWord=tmpWord;
}
void ShortestWord(std::string const& text)
{
std::stringstream ss(text);
std::vector<std::string> v(std::istream_iterator<std::string>(ss), {});
auto min = std::min_element(v.begin(), v.end(),
[] (auto& lhs, auto& rhs) { return lhs.size() < rhs.size(); });
auto p = std::make_pair(*min, min->size());
std::cout << "Shortest Word: \"" << p.first << "\"\n";
std::cout << "Word Length: " << p.second << '\n';
}
void ShortestWord(string text)
{
string tmpWord = "";
// The upper bound of answer is text
string minWord = text;
for(int i=0; i < (int)text.length(); i++)
{
/// If founded space, rewrite word
if(text[i] != ' ')
{
tmpWord += text[i];
}
else
{
// We got a new word, try to update answer
if(tmpWord.length() < minWord.length())
minWord=tmpWord;
tmpWord = "";
}
}
// Check the last word
if(tmpWord != "")
{
if(tmpWord.length() < minWord.length())
minWord=tmpWord;
}
cout << "Shortest Word: " << minWord << endl;
cout << "Word Length: " << minWord.length() << endl;
}
If we want to get both min value and max value, the initialize values should be opposites to each of them.
Actually, that should be the max-limit string of 'text'.
In the development of the business application, this is common sense, but some programers may hate the way of this.
string minWord = text; // MAX_SIZE
string maxWord = "";
for(int i = 0; i < text.length(); i++)
{
/// If founded space, rewrite word
if(text[i] != ' ')
tmpWord += text[i];
if(text[i] == ' ' || i == text.length()) {
/// All the time check word length and if tmpWord > maxWord => Rewrite.
if(tmpWord.length() > maxWord.length())
maxWord = tmpWord;
if(tmpWord.length() < minWord.length())
minWord = tmpWord;
tmpWord = "";
}
}

Error with running BruteForce algorithmn?

I'm taking a users input like so:
algo_type "pattern" filename
ex.
bf "inging" input_file.txt
As of now I separate the users input into three different variables, one for the algo_type, one for the pattern I'm looking for, and one for the filename. Once I get the pattern and filename I'm trying to take the pattern into the Bruteforce algo and search each line and print the position that pattern occurs in the line of the .txt file. Right now though every time I enter the input into the algo it returns -1 meaning the BruteForce isn't running? What exactly am I doing wrong here?
int BruteForce(const string& line, const string& pattern){
int n , m;
n = line.length();
m = pattern.length();
for(int i = 0 ; i < n - m ; i++){
int j = 0;
while( j < m && line[i + j] == pattern[j]){
j = j+1;
if( j == m){
return i;
}
}
}
return -1;
}
int main(){
string text, algo_type , pattern , fname, line;
getline(cin ,text);
istringstream iss(text);
if(iss >> algo_type >> pattern >> fname){
cout << algo_type << pattern << fname << "'\n'";
}
int i = 0;
ifstream ifs;
ifs.open(fname.c_str());
while(getline(ifs, line) && fname != ""){
if( algo_type == "bf"){
cout << "Line " << i++ << ":" << BruteForce(line,pattern) << endl;
}
}
return 0;
}
I suppose you wanted return -1 at the end of BruteForce, rather then at the end of the first iteration.
Also, the first loop condition needs to have <= instead of <, or matches ending in the very position won't be found.
Here's a complete, fixed version: EDIT as per the edit, list multiple matches within lines:
#include <string>
using namespace std;
int BruteForce(const string& line, size_t start, const string& pattern) {
const size_t n = line.length();
const size_t m = pattern.length();
if (n<m) return -1;
for(size_t i = start; i <= (n - m); i++) {
for(size_t j=0; j < m && (line[i + j] == pattern[j]); ++j) {
if(j == m-1) {
return i;
}
}
}
return -1;
}
#include <iostream>
#include <fstream>
#include <sstream>
int main() {
string text, algo_type, pattern, fname, line;
getline(cin ,text);
istringstream iss(text);
if(iss >> algo_type >> pattern >> fname) {
cout << " " << algo_type << " " << pattern << " " <<fname << "\n";
}
int i = 1;
ifstream ifs;
ifs.open(fname.c_str());
while(getline(ifs, line) && fname != "") {
if(algo_type == "bf") {
int pos = -1;
while (-1 != (pos = BruteForce(line, pos+1, pattern)))
cout << "Line " << i << ":" << pos << " " << line << endl;
}
i++;
}
return 0;
}
See it Live on Coliru: http://coliru.stacked-crooked.com/a/f1a7693d7d3bd7c5
I've tested it with
./test <<< "bf iss /etc/dictionaries-common/words" | grep Miss
Which printed
Line 10241:1 Miss
Line 10242:1 Mississauga
Line 10242:4 Mississauga
Line 10243:1 Mississippi
Line 10243:4 Mississippi
Line 10244:1 Mississippi's
Line 10244:4 Mississippi's
Line 10245:1 Mississippian
Line 10245:4 Mississippian
Line 10246:1 Mississippian's
Line 10246:4 Mississippian's
Line 10247:1 Mississippians
Line 10247:4 Mississippians
Line 10248:1 Missouri
Line 10249:1 Missouri's
Line 10250:1 Missourian
Line 10251:1 Missourian's
Line 10252:1 Missourians
Line 10253:1 Missy
Line 10254:1 Missy's

Random characters popping up

When I debug my program, it outputs a random stream of characters when outputting the asterisked line.
int main ()
{
string inputPuzzle;
cout << "Enter a word or set of words: ";
getline(cin, inputPuzzle);
char* puzzle = new char[inputPuzzle.size()+1];
memcpy(puzzle, inputPuzzle.c_str(), inputPuzzle.size()+1);
puzzle[inputPuzzle.size()+1] = '';
int strikes = 0;
char userInput;
char* userSoln = new char[inputPuzzle.size()];
for (int i = 0; i < inputPuzzle.size(); i++)
{
userSoln[i] = '-';
if (puzzle[i] == ' ')
{
userSoln[i] = ' ';
}
}
bool solved = false;
int numberOfLetters;
for (;;)
{
numberOfLetters = 0;
cin >> userInput;
for (int i = 0; i < inputPuzzle.size(); i++)
{
if (userInput == puzzle[i])
{
numberOfLetters++;
userSoln[i] = puzzle[i];
}
}
if (numberOfLetters == 0)
{
cout << "There are no " << userInput << "'s\n" ;
strikes++;
}
else
{
cout << "There are " << numberOfLetters << " " << userInput << "'s\n";
}
if (userSoln == puzzle)
{
break;
}
if (strikes == 10)
{
break;
}
**cout << "PUZZLE: " << userSoln << "\n";**
cout << "NUMBER OF STRIKES: " << strikes << "\n";
}
if (strikes == 10)
{
cout << "Sorry, but you lost. The puzzle was: " << puzzle;
}
else
{
cout << "Congratulations, you've solved the puzzle!!! YOU WIN!!!!!";
}
}
I've tried clearing cin buffers, but nothing doing. I have all the necessary include files (string and iostream) too, so that isn't the issue, and i have the namespace std above the main method.
This isn't a valid character constant.
puzzle[inputPuzzle.size()+1] = '';
If you intended a terminating character, it should be
puzzle[inputPuzzle.size()+1] = '\0';
or just
puzzle[inputPuzzle.size()+1] = 0;
or you could replace both these lines
memcpy(puzzle, inputPuzzle.c_str(), inputPuzzle.size()+1);
puzzle[inputPuzzle.size()+1] = '';
with strcpy
strcpy(puzzle, inputPuzzle.c_str());
Edit:
You also need to put a terminating character at the end of userSoln before printing it.
userSoln[ inputPuzzle.size() ] = '\0';
puzzle[inputPuzzle.size()+1] = '';
should be
puzzle[inputPuzzle.size()+1] = '\0';
you were trying to add the null terminator to the end of the string to signify the end, but '' is not quite it.