removing multiple spaces in c++ from string - c++

I have the following code to open a file and read the data from it, then take the relavent part and print it to screen.
char* search = "model name";
int Offset;
string Cpu;
ifstream CpuInfo;
CpuInfo.open ("/proc/cpuinfo");
if(CpuInfo.is_open())
{
while(!CpuInfo.eof())
{
getline(CpuInfo,Cpu);
if ((Offset = Cpu.find(search, 0)) != string::npos)
{
//cout << "found '" << search << " " << line << endl;
break;
}
}
CpuInfo.close();
}
Cpu.replace (0,13,"");
cout << Cpu
This usually outputs the type of CPU your using, but one problem is that some people have various spaces inbetween the words that it prints out.
My question is how to remove all the spaces from inbetween the words. They can of random ammount and aren't always present.
Thank you in advance.

Since your question states: "how to remove all the spaces from inbetween the words":
You can use std::remove_if from the standard <algorithm> library in addition to std::isspace:
std::string mystring = "Text with some spaces";
std::remove_if(mystring.begin(), mystring.end(), std::isspace);
This now becomes:
Textwithsomespaces
REFERENCES:
http://en.cppreference.com/w/cpp/algorithm/remove

Related

Reading variable data from a string

I am reading through a large log file of bans, and from those bans I want to specify a name within that line (see John below). I then want to print out only the IP of that line. Here are a few lines from an example log file:
[13:42:51] james preston (IP: 11.111.11.11) was banned by john
[13:42:51] gerald farmer (IP: 222.22.222.22) was offline banned by James
[13:42:51] luke parker (IP: 33.33.333.333) was banned by john
So far I can get the lines of the bans containing "john" however I would like to then extract the IP address from those lines.
int main() {
ifstream BanLogs;
BanLogs.open("ban-2019.log");
// Checking to see if the file is open
if (BanLogs.fail()) {
cerr << "ERROR OPENING FILE" << endl;
exit(1);
}
string item;
string name = "john";
int count = 0;
//read a file till the end
while (getline(BanLogs, item)) {
// If the line (item) contains a certain string (name) proceed.
if (item.find(name) != string::npos) {
cout << item << endl;
count++;
}
}
cout << "Number of lines " << count << endl;
return 0;
}
Since you are new to programming, here is the most vanilla way:
size_t startIdx = item.find("(IP: ");
if (startIdx == std::string::npos) continue;
startIdx += 5; // skip the "(IP: " part
size_t endIdx = item.find(')', startIdx + 1);
if (endIdx == std::string::npos) continue;
cout << item.substr(startIdx, endIdx - startIdx) << endl;
This kind of jobs are much easier to do with scripting languages, i.e. Python.
As mentioned in the comments, regular expressions are one option.
Another would be to use std::string::find which you are already using to select the relevant lines. You cloud search for "(IP:" to get the starting position of the address (The actual starting position is the result of std::string::find plus 4 for the length of the search string). Then you can search for ")" to get the end position of the IP address in the string. Using these two positions you can extract a substring containing the IP address by using std::string::substr.

Code reading extra line in my loop?

My goal is to read from an input file and count the number of lines that have at least 1 lowercase letter and 1 digit. I have already solved the rest of my code which counted all of the lowercase, uppercase, digits, characters and words no problem. I have also read from the input file and reversed the lines word by word. I cannot seem to figure out why the code is counting 8 lines when there are only 7 with 1 lowercase and 1 digit. While using getline() for all of the other loops, I've had no issues. I'm not looking specifically for someone to write the code for me. I'd just like an explanation of why this is happening if possible?
My input file contains:
This is a test file for hw3
How many Uppercase letters are in this f1le?
How many Lowercase letters are in this F1le?
H0W mAnY dIg1ts ar3 1N in this FILe?
Turn the 1npU7 N4m3 int0 its reverse
reverse the Lines to their opp05173 coutnerpart
find tOTal NumbEr of characTer5 in F1le
THIS IS A TEST LINE
My code for this section is:
inFile.clear();
inFile.seekg(0, inFile.beg);
while(getline(inFile, line)){
wordInput.str(line);
wordInput.clear();
wordInput.seekg(0);
while(wordInput.get(c)){
if(islower(c)){
lowerCase++;
}
else if(isdigit(c)){
digit++;
}
}
if(lowerCase >= 1 && digit >= 1){
lineCount++;
}
}
cout << lineCount << endl;
return 0;
}
I have initialized all of my int variables to 0 and the top and I have declared my sstream variables as well. My libraries include <sstream> <fstream> <string> <iostream> and <algorithm> (which was used for earlier parts.
The output I am getting is
8
when it should be 7. The final line should not be counted as it has no lowercase letters and no digits. I am thinking that the first line is being read a second time and then stopping. I am in an intro to C++ class and have yet to learn how to use the debugger. Thank you in advance.
You made it clear that that you initialise all int variables to 0, which is great; however, let's have a look at your code (formatted so the indentation makes more sense):
// read line from file
while(getline(inFile, line))
{
wordInput.str(line);
wordInput.clear();
wordInput.seekg(0);
// read character
while(wordInput.get(c))
{
if(islower(c))
{
lowerCase++;
}
else if(isdigit(c))
{
digit++;
}
}
if(lowerCase >= 1 && digit >= 1){
lineCount++;
}
}
Here you read a line, and go through all characters on that line, and if you find a lowercase character, or a digit, you increment a variable. What happens when you read the next line? You haven't reset those variables back to 0, so upon reading the next line they would both be above 1 already.
You need the following:
while(getline(inFile, line))
{
wordInput.str(line);
wordInput.clear();
wordInput.seekg(0);
// We're about to start reading this line, so obviously we haven't found any yet
digit = 0;
lowerCase = 0;
Better yet, you can probably just declare those variables within the read line while loop:
while(getline(inFile, line))
{
int digit = 0;
int lowerCase = 0;
Although you haven't been taught to use a debugger, a great way of debugging is with cout statements. Put some print statements in to determine what all your variables are at any given time:
while(getline(inFile, line))
{
std::cout << "read line " << line << std::endl;
while(wordInput.get(c))
{
std::cout << "lowercase found so far: " << lowerCase << std::endl;
std::cout << "digits found so far: " << digit << std::endl;
if(islower(c))
{
std::cout << "lowercase character found: " << c << std::endl;
lowerCase++;
}
else if(isdigit(c))
{
std::cout << "digit found: " << c << std::endl;
digit++;
}
}
if(lowerCase >= 1 && digit >= 1)
{
std::cout << "found a lowercase character (" << lowerCase << ") or a digit (" << digit << ")" << std::endl;
lineCount++;
}
}
It's been awhile since I have coded in C++. There are other ways to debug than a debugger program.
I would add cout << line << endl; to your first while loop. That way you can output how the lines are being read and if any are being repeated. Also check your islower(char) and isdigit(char) functions to make sure they are reading appropriate ascii ranges.

How to count words and numbers from a simple sentence in C++

I'm a beginner at c++(took a couple classes, then no c++ for a while, then starting back up several months later), and I'm trying to count the number of words in a simple sentence and then count the number of numbers in that same sentence. To count the words, I use:
int countWord(char *word)
{
int counter = 0;
char *words = strtok(word, " 0123456789-");
while (words != NULL)
{
counter++;
words = strtok(NULL, " 0123456789-");
}
return counter;
}
The number counter is basically the same, just instead of using integers I use the alphabet.
char *num = strtok(number, " abcdefghijklmnopqrstuvwxyz");
My main is:
int main()
{
char str[] = "what time is 88 it 99today";
cout << "words = " << countWord(str) << " " << "numbers = " <<
countNum(str) << endl;
system("pause");
return 0;
}
When I run this it outputs: words = 3 numbers = 2.
When i rearrange main to:
char str[] = "what time is 88 it 99today";
cout << "words = " << countWord(str) << " ";
cout << "numbers = " << countNum(str) << endl;
output is: words = 5 numbers = 0
Can anyone explain why this is incorrect? Also, if anyone can refer me to a text that covers this, I'd appreciate that. The text I learned from is: "C++ Programming: Program Design Including Data Structures by D.S. Malik. I didn't see any techniques in this book to count "words". Thank you.
The issue is that strtok marks the end of tokens in the original string by a null character. Citing from cppreference:
If such character was found, it is replaced by the null character '\0' and the pointer to the following character is stored in a static location for subsequent invocations.
Notes: This function is destructive: it writes the '\0' characters in the elements of the string str. In particular, a string literal cannot be used as the first argument of strtok.
In your case the line
cout << "words = " << countWord(str) << " " << "numbers = " <<
countNum(str) << endl;
is a composition of operator<<, like
...operator<<(operator<<(cout, "words"), countWord(str))...
so the line countNum(str) is evaluated first. Then countWord(str) is evaluated secondly. This is in contrast to
cout << "words = " << countWord(str) << " ";
cout << "numbers = " << countNum(str) << endl;
where the other way around happens.
One solution is to use a copy of the original string when using strtok, e.g. use strtok(strdup(str)) every time. Better yet, use standard C++ library features, like std::string, std::count_if etc. I'm sure there are plenty of word counting solutions around using pure C++.
Vlad has submitted a nice answer for your C-style code. My answer is demonstrating use of more C++ libraries to help move things along:
#include <iostream>
#include <string>
#include <vector>
#include <regex>
int main() {
// The main string.
std::string str = "what time is 88 it 99today";
// Space is your delimiter
std::string delimiter = " ";
// Create a regex string for matching numbers, including floating point.
std::regex number_chars(std::string("[0123456789.]+"));
// A lambda function to help tokenize strings.
// Returns a vector of substring tokens.
// The internal code is taken from an answer on Stack Overflow.
auto tokenizer = [](std::string s, std::string delimiter) {
size_t pos = 0;
std::string token;
std::vector<std::string> tokens;
while (pos = (s.find(delimiter))) {
token = s.substr(0, pos);
tokens.push_back(token);
s.erase(0, pos + delimiter.length());
if (pos == std::string::npos)
break;
}
return tokens;
};
// Apply the lambda.
auto tokens = tokenizer(str, delimiter);
// Output your tokens.
for (auto it : tokens) {
std::cout << it << "\n";
} std::cout << "\n";
// Output tokens that are numbers.
for (auto it : tokens) {
if (std::regex_match(it, number_chars)) {
std::cout << "String: " << it << " is a number.\n";
}
}
return 0;
}
Since C++ has a regular expression library in C++11, it would be good to leverage it.
Coliru: http://coliru.stacked-crooked.com/a/43cd6711e1243f4a

count specific things within a code in c++

can anyone help me make this more generalised and more pro?
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
// open text file for input:
string file_name;
cout << "please enter file name: ";
cin >> file_name;
// associate the input file stream with a text file
ifstream infile(file_name.c_str());
// error checking for a valid filename
if ( !infile )
{
cerr << "Unable to open file "
<< file_name << " -- quitting!\n";
return( -1 );
}
else cout << "\n";
// some data structures to perform the function
vector<string> lines_of_text;
string textline;
// read in text file, line by
while (getline( infile, textline, '\n' ))
{
// add the new element to the vector
lines_of_text.push_back( textline );
// print the 'back' vector element - see the STL documentation
cout << lines_of_text.back() << "\n";
}
cout<<"OUTPUT BEGINS HERE: "<<endl<<endl;
cout<<"the total capacity of vector: lines_of_text is: "<<lines_of_text.capacity()<<endl;
int PLOC = (lines_of_text.size()+1);
int numbComments =0;
int numbClasses =0;
cout<<"\nThe total number of physical lines of code is: "<<PLOC<<endl;
for (int i=0; i<(PLOC-1); i++)
//reads through each part of the vector string line-by-line and triggers if the
//it registers the "//" which will output a number lower than 100 (since no line is 100 char long and if the function does not
//register that character within the string, it outputs a public status constant that is found in the class string and has a huge value
//alot more than 100.
{
string temp(lines_of_text [i]);
if (temp.find("//")<100)
numbComments +=1;
}
cout<<"The total number of comment lines is: "<<numbComments<<endl;
for (int j=0; j<(PLOC-1); j++)
{
string temp(lines_of_text [j]);
if (temp.find("};")<100)
numbClasses +=1;
}
cout<<"The total number of classes is: "<<numbClasses<<endl;
Format the code properly, use consistent style and nomenclature and throw out the utterly redundant comments and empty lines. The resulting code should be fine. Or “pro”.
Here, I’ve taken the efford (along with some stylistic things that are purely subjective):
Notice that the output is actually wrong (just run it on the program code itself to see that …).
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string file_name;
cout << "please enter file name: ";
cin >> file_name;
ifstream infile(file_name.c_str());
if (not infile) {
cerr << "Unable to open file " << file_name << " -- quitting!" << endl;
return -1;
}
else cout << endl;
vector<string> lines_of_text;
string textline;
while (getline(infile, textline)) {
lines_of_text.push_back(textline);
cout << lines_of_text.back() << endl;
}
cout << "OUTPUT BEGINS HERE: " << endl << endl;
cout << "the total capacity of vector: lines_of_text is: "
<< lines_of_text.capacity() << endl << endl;
int ploc = lines_of_text.size() + 1;
cout << "The total number of physical lines of code is: " << ploc << endl;
// Look for comments `//` and count them.
int num_comments = 0;
for (vector<string>::iterator i = lines_of_text.begin();
i != lines_of_text.end();
++i) {
if (i->find("//") != string::npos)
++num_comments;
}
cout << "The total number of comment lines is: " << num_comments << endl;
// Same for number of classes ...
}
I'm not really sure what you're asking, but I can point out some things that can be improved in this code. I'll focus on the actual statements and leave stylistic comments to others.
cin >> file_name;
To handle file names with spaces, better write
getline(cin, file_name);
int PLOC = (lines_of_text.size()+1);
Why do you claim that there's one more line than there actually is?
if (temp.find("//")<100)
with some complicated comment explaining this. Better write
if (temp.find("//")<temp.npos)
to work correctly on all line lengths.
cout<<"The total number of comment lines is: "<<numbComments<<endl;
Actually, you counted the number of end-of-line comments. I wouldn't call a comment at the end of a statement a "comment line".
You don't count /* */ style comments.
Counting the number of classes as };? Really? How about structs, enums, and plain superfluous semicolons? Simply count the number of occurences of the class keyword. It should have no alphanumeric character or underscore on either side.
Use proper indentation, your code is very difficult to read in its current form. Here is a list of styles.
Prefer ++variable instead of variable += 1 when possible; the ++ operator exists for a reason.
Be consistent in your coding style. If you're going to leave spaces between things like cout and <<, function arguments and the function parantheses do it, otherwise don't, but be consistent. Pick one naming convention for your variables and stick to it. There is a lot about styles you can find on google, for example here and here.
Don't use the entire std namespace, only what you need. User either using std::cout; or prefix all of your cout statements with std::
Avoid needless comments. Everyone knows what ifstream infile(file_name.c_str()); does for example, what I don't know is what your program does as a whole, because I don't really care to understand what it does due to the indentation. It's a short program, so rather than explaning every statement on its own, why not explain what the program's purpose is, and how to use it?
These are all stylistic points. Your program doesn't work in its current form, assuming your goal is to count comments and classes. Doing that is a lot more difficult than you are considering. What if I have a "};" as part of a string for example? What if I have comments in strings?
Don't import the whole std namespace, only things you need from it:
using std::string;
Use a consistent naming convention: decide whether you prefer name_for_a_variable or nameforavariable or nameForAVariable. And use meaningful names: numbComments makes me associate to very different things than would numberOfComments, numComments or commentCount.
If your original code looks like this, I strongly recommend to select a single consistent indentation style: either
if ( ... )
{
...
}
or
if ( ... )
{
...
}
bot not both in the same source file.
Also remove the useless comments like
// add the new element to the vector
This is "only" about the readability of your code, not even touching its functionality... (which, as others have already pointed out, is incorrect). Note that any piece of code is likely to be read many more times than edited. I am fairly sure that you will have trouble reading (and understanding) your own code in this shape, if you need to read it even a couple of months after.
"More professional" would be not doing it at all. Use an existing SLOC counter, so you don't reinvent the wheel.
This discussion lists a few:
http://discuss.techinterview.org/default.asp?joel.3.207012.14
Another tip: Don't use "temp.find("};}) < 100)", use "temp.find("};") != temp.npos;"
Edit: s/end()/npos. Ugh.

string program for ice cream shop (Edited again) [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
With the assistance of others, I have redone the code from scratch due to them pointing out numerous errors and things that wouldn't work. Thus I have changed the code massively.
I have the program working other than two formatting settings that I can't figure out how to get to work.
I need to only print "DAILY SCOOP REPORT" once at the top of the output, but I've moved it around but due to the way the arrays are set up I don't know where to put it.
Here is my code:
#include <iostream>
include
include
include
include
include
using namespace std;
int main()
{
string flavor_input, Capitalize;
string flavors[] = { "Chocolate", "Vanilla", "Strawberry", "Mint", "Rocky Road", "Mocha" };
int scoop_count [6] = { 0, 0, 0, 0, 0, 0 }, scoops = 0, j, k;
bool valid_option;
cout << "Welcome to Frozen Tongue Ice Cream Shop\n"<<endl;
cout << "Flavors avaliable: "<<endl;
cout << "Chocolate "<<endl;
cout << "Valnilla "<<endl;
cout << "Strawberry "<<endl;
cout << "Mint "<<endl;
cout << "Rocky Road "<<endl;
cout << "Mocha \n"<<endl;
while(true) {
cout << "Please enter the flavor of icecream sold or 'STOP' to exit.\n"<<endl;
getline (cin, flavor_input); // getline causes rocky road to be accepted with a space between the words.
string::iterator it( flavor_input.begin()); //converting the first letter of input to uppercase if not already.
if (it != flavor_input.end())
flavor_input[0] = toupper((unsigned char)flavor_input[0]);
while(++it != flavor_input.end())
{
*it = tolower((unsigned char)*it);
}
if (flavor_input == "STOP" || flavor_input == "Stop")
break;
valid_option = false;
for(int i=0;i<6;i++) //Checks to see if input matches those of possible inputs.
if(!flavor_input.compare(flavors[i]))
{
valid_option = true;
break;
}
if(!valid_option)
{
cout << "Invalid Flavor. Try again.\n\n";
flavor_input.clear();
continue;
}
for(int i=0;i<6;i++)
{
if(!flavor_input.compare(flavors[i]))
{
cout << "Enter how many scoops were sold: ";
cin >> scoops;
cin.ignore();
scoop_count[i] += scoops;
scoops = 0;
cout << '\n';
break;
}
}
}
for(int i=0;i<6;i++)
{
if(!scoop_count[i])
continue;
else
{
cout << "\nDAILY SCOOP REPORT: "<<endl;
cout << setiosflags( ios::left )
<< setw( 11 ) << flavors[i]
<< resetiosflags( ios::left )
<< setw( 4 ) << scoop_count[i] << endl;
}
}
cin.get();
return 0;
}
Thanks again for all of the assistance. It is greatly appreciated.
Thanks to all the assistance and pointing me in the direction of what to study, I have the program completed other than one last part.
I figured out that why it wasn't working when I moved the "DAILY SCOOP REPORT" line around. I had renamed the file and when I compiled it, it was outputing the "last working configuration" kinda deal if that makes sense. So I created a new project (the .cpp file has to have a certain name for submission) and put the code in it. Now the line is printed only once.
In the code block below, I have it where it lowers casing for all other letters other than the first or so it seems to be doing. The reason I have the case coding the way I do is that the instructions want the flavor report to print out with first letter of each word cap and lower after that. I am going to look into how to cap the 2nd "R" in Rocky Road, but other than the ignore white-space I don't really know how. Do I need to parse the line?
Anyone to point me in the right direction would be appreciated.
I tried but it gives error that in the first if statement "syntax error : identifier 'flavor_input'".
//converting the first letter of input to uppercase if not already.
string::iterator it( flavor_input.begin());
if flavor_input = "rocky road"
(it != flavor_input.end())
flavor_input[6] = toupper((unsigned char)flavor_input[6]);
if (it != flavor_input.end())
flavor_input[0] = toupper((unsigned char)flavor_input[0]);
while(++it != flavor_input.end())
{
*it = tolower((unsigned char)*it);
}
switch doesn't work with strings.
You need to use the operator == to select the right choice like so:
string count = // ... something
if (count == "choc") {
}
else if (count == "van") {
}
else if (count == "str") {
} // .. etc
A few other things: make sure you spell string with a consistent case, all lower case and no upper case. String is something different than string.
Make sure you surround strings with double quotes "" and not single quotes ''. single quotes are for single characters like 'a' or 'b'. double quotes are for multiple characters strings like "abc" and "hello"
Having the word count as both the function name and an argument name is probably a bad idea and will not compile because the same name means two different things.
You can't return multiple values from a function. writing something like return a,b,c; doesn't mean what you probably want it to mean. the comma (,) operator allows several expressions to be evaluated in the same statement and the result is the value of the last expression so writing return 1,2,3; is exactly the same as writing return 3;
C++ cannot switch on a string. Replace your switch(count) {...} with if/else if statements. Additionally the proper format for a string is "string", not 'string' (single quotes designate a single character, like: 'a'). Also, ensure that you always use the correct casing for string objects (string as opposed to String, like you have as your return values)
Other than that, it would be helpful to see the compiler errors you are getting.
Another thing I noticed in your source: you will have to omit the semicolons (-cola?) at the end of the following lines:
cout << "Please enter the flavor of icecream sold or 'STOP' to exit.\n"<<endl
<< "Flavors avaliable:\n"<<endl
<< "chocolate\n"<<endl
<< "valnilla\n"<<endl
<< "strawberry\n"<<endl
<< "mint\n"<<endl
<< "rocky road\n"<<endl
<< "mocha\n"<<endl
<< "Enter flavor : ";
This is just a single huge expression. The same applies to
cout << "\nDAILY SCOOP REPORT: \n"<<endl
<< "chocolate :"<<chocolate<<"\n"<<endl
<< "vanilla :"<<vanilla<<"\n"<<endl
<< "strawberry :"<<strawberry<<"\n"<<endl
<< "mint :"<<mint<<"\n"<<endl
<< "rocky road :"<<rocky_road<<"\n"<<endl
<< "mocha :"<<mocha<<"\n"<<endl;
Also: the endl and the "\n" are redundant. You will see the choices being separated by empty lines.
I haven't looked at the whole thing, but this isn't going to do what you want:
if (flavor = 'STOP' || 'stop')
I think you need:
if (flavor.compare("STOP") == 0 || flavor.compare("stop") == 0)
Let's go down the problems I see.
String count (string& flavor, string& count, string& chocolate, string& vanilla, string& strawberry, string& mint, string& rocky_road, string& mocha);
You're using String here, I'm sure you meant std::string or just string.
Inside the count function (SO is truncating the code when pasted), you're ending the line with a semicolon after endl yet trying to continue the stream output. I think you meant
Next:
if (flavor = 'STOP' || 'stop')
I think you meant to use the operator== instead of operator=, which is assignment not comparison. Also, there are no junctions in c++, so you will have to write that out as:
if (flavor == 'STOP' || flavor == 'stop')
Next:
switch (count) { case 'choc' :
Two problems here. First, you can only use plain-old-data (pod) in switch statements. Using std::string in a switch will not automatically call operator==; you will have to use if/else statements. Also, string literals are double quoted whereas character literals are single quoted.
Next:
chocCount + count;
This isn't really a statement. I'm sure you meant chocCount += count;
Next:
if (flavor = chocolate) chocCount + count;
Again, you want to use == and chocCount += count;.
Most of these problems are repeated. You should fix each of these problems everywhere they exist. There may be other problems, but I was basically compiling that in my head.
I didn't read through all of it to find semantic problems, but your count function is clearly not returning a count (at least what I currently see posted). You are returning a String, which I assume you meant string.
That's all this human compiler is going to solve for 1 homework assignment. I could recommend you go read a good C++ tutorial.