so I am having a problem with getting my program to increment values properly.
My program needs to take a file in main(), and then pass that to a function-set to print that is called in main.
The key thing is that I need to use loops within the functions to get Letter Count, Space-Count, and Word Count.
I have the output configured right
cout << line_number << ": " << line << " [" << letter_count << " letters, " << space_count << " spaces, " << word_count << " words]" << endl;
Which results for example
0: Words go here. [# letters, # spaces, # words.]
But with my current functions for Letters and spaces, it doesn't work.
My non-space function for example
int count_non_space(string line) {
int non_space = 0;
for (int i = 0; i < line.length(); i ) {
if (line.c_str() != " ") {
non_space ;
}
}
return non_space;
It counts all of the characters in the line instead and the counterpart (space_count) counts nothing.
And that's not to mention that I don't know how to count the words in the line.
Any advice as to what is going on? as I am certain that count_space and count_non_space should be inverses of each other (count_space being the same function but with == instead of !=)
EDIT: Got the Letter and Space count correct. Now, how would I get the word count from that sort of method?
EDIT 2: Okay so letter count is off. It is counting puncutation-characters (commas, periods, dashes, hiphons.etc) as leters.
I have managed to redact periods, dashes.etc from the code manually with a reduction if statement in the count_non_characters function.
But I can't add ' to it as it already uses '' to catch the char comparison
Is there catch-all term for punctuation characters in C that I can use for
if (line[i] == "Puncutation") {
non_space--;
}
?
CodePudding user response:
As UnholySheep said, when you compare a c string (char *) you can't use standard logical operators. You will need to use strcmp(). However, if you use a c std::string then you can use compare() or logical operators.
As for finding words in a string. Here are a few resources.
- c counting how many words in line
- C function to count all the words in a string
- C Program to find number of Digits and White Spaces in a String
- Count words in a given string
For further help. Google: "Get word count per line c "
Reminder, these two are different data types and have different library support:
std::string myStr
myStr.c_str()
CodePudding user response:
If the goal is to count characters in a string that are not spaces, then there is a way to do this using the STL and lambdas that is much cleaner than writing a bunch of loops and worrying about updating variables.
int count_non_space(std::string line) {
return std::count_if(line.begin(), line.end(),
[](auto ch) {
return ch != ' ';
});
}
This also makes is very straightforward to accommodate for things like spaces and tabs.
int count_non_space(std::string line) {
return std::count_if(line.begin(), line.end(),
[](auto ch) {
return ch != ' ' && ch != '\t';
});
}
To count the opposite (just the spaces) we simply need to change the condition in the lambda.
int space_count(std::string line) {
return std::count_if(line.begin(), line.end(),
[](auto ch) {
return ch == ' ' || ch == '\t';
});
}
As Remy Lebeau helpfully points out, we don't even have to write the lambda. We can simply use the std::isspace function directly instead of the lambda.
int space_count(std::string line) {
return std::count_if(line.begin(), line.end(), std::isspace);
}
Documentation on std::count_if.
CodePudding user response:
Here's how I would revise the function you gave:
int count_non_space(string line) {
int non_space = 0;
for (int i = 0; i < line.length(); i ) {
if (line[i] != ' ') {
non_space ;
}
}
return non_space;
}
Notes:
- I changed
line.c_str()
toline[i]
in order to access thei
th character ofline
- I changed
" "
to' '
so that it's comparing against the spacechar
, not astring
which only contains the space. The comparison would fail if we were comparing thei
th char to astring
As for this:
And that's not to mention that I don't know how to count the words in the line.
I don't know how your requirements define a word, but if we assume a word is any contiguous clump of non-space characters, you could use this logic:
initialize
bool in_word
tofalse
initialize
int word_count
to0
for each
char
in thestring
:- if
in_word
isfalse
and the currentchar
is not a space, then setin_word
to betrue
and increaseword_count
by 1 - if
in_word
istrue
and the currentchar
is a space, then setin_word
to befalse
- if
return
word_count