Need help writing C++ program that counts non-whitespace characters ?

In summary: END_MARK = '&'; int i, numspaces; char nextChar; string msg; numspaces=1; cout << "Enter some text - on 1 or more lines - followed by &.\n"; getline(cin, msg); // checks each character in the string for (i=0; i<int(msg.length()); i++) { nextChar = msg.at(i); // gets a character if (
  • #1
nukeman
655
0
Need help writing C++ program that counts non-whitespace characters...?

Homework Statement



Here are instructions...

Code:
Write a program - stored in the file charcounter.cpp - that will read and count non-whitespace characters from the standard input stream - until some special character is entered. Let's use & for now. (This value is declared as a named constant so that it can easily be changed in only one place in the program - SO ... the & should (must??) not appear anywhere else in the program!)

Your program will also count the number of digits in the input stream, as shown below. Note that the cctype library contains a boolean function isdigit(achar) ...

(Note that your program should handle the unusual case in which the & is the first non-whitespace character.)

Your program must produce behaviour very similar to the following ...

$ ./a.out

Enter some text - on 1 or more lines - followed by &.
1   25
cat
0g 6&
There were 9 (non-whitespace) characters before the &
5 of them were digits.
$

Homework Equations


The Attempt at a Solution



This is what I have so far. Really having trouble with this one

#include <iostream>
#include <string>
using namespace std;

int main()
{
const char END_MARK = '&';
int i, numspaces;
char nextChar;
string msg;

numspaces=1;

cout << "Enter some text - on 1 or more lines - followed by &.\n";
getline(cin, msg);

// checks each character in the string
for (i=0; i<int(msg.length()); i++)
{
nextChar = msg.at(i); // gets a character
if (isspace(msg))
numspaces++;
}
cout << "\nThere were " << numspaces << " (non-whitespace) characters before the &.";
cin.ignore();
return 0;
}
 
Last edited:
Physics news on Phys.org
  • #2


nukeman said:
nextChar = msg.at(i); // gets a character

This line is unnecessary; the variable nextChar does not appear anywhere else in the code.

The count of spaces should start with zero, not one (it should be obvious why).

It looks like the code you posted would result in a count of the number of spaces in a line of text (until '\n'), and not the number of spaces until '&'. There is an easy way to modify your program to fix this issue. You can use an overload of the function getline to change the character that is used to denote the end of a line. This may be done by providing a third parameter to the getline function, which is a character to be used as the delimiter.
 
  • #3


How do I change it so it counts just characters, not words??

THanks so much for your help!

And yes, I want it to end with &, insted of \n - Cant I just swap them?
 
  • #4


I haven't looked through program, but if you want to count chars you would need to use the library <cctype>. Hint hint: I would use isalpha(); and test if character is alphabetical or isdigit for numbers.
 
  • #5


"library <cctype>. Hint hint: I would use isalpha(); and test if character is alphabetical or isdigit for numbers.``

I am not too sure how to use that...
 
  • #6


nukeman said:

Homework Statement



Here are instructions...

Code:
Write a program - stored in the file charcounter.cpp - that will read and count non-whitespace characters from the standard input stream - until some special character is entered. Let's use & for now. (This value is declared as a named constant so that it can easily be changed in only one place in the program - SO ... the & should (must??) not appear anywhere else in the program!)

Your program will also count the number of digits in the input stream, as shown below. Note that the cctype library contains a boolean function isdigit(achar) ...

(Note that your program should handle the unusual case in which the & is the first non-whitespace character.)

Your program must produce behaviour very similar to the following ...

$ ./a.out

Enter some text - on 1 or more lines - followed by &.
1   25
cat
0g 6&
There were 9 (non-whitespace) characters before the &
5 of them were digits.
$


This is what I have so far. Really having trouble with this one

#include <iostream>
#include <string>
using namespace std;

int main()
{
const char END_MARK = '&';
int i, numspaces;
char nextChar;
string msg;

numspaces=1;

cout << "Enter some text - on 1 or more lines - followed by &.\n";
getline(cin, msg);

// checks each character in the string
for (i=0; i<int(msg.length()); i++)
{
nextChar = msg.at(i); // gets a character
if (isspace(msg))
numspaces++;
}
cout << "\nThere were " << numspaces << " (non-whitespace) characters before the &.";
cin.ignore();
return 0;
}

Ok, So I kind of see what you are trying to do. What I notice is that isspace is not a member of cstring library. It is a member of cctype library. To get the line that the user entered and receive the characters, I would us cin.get() to receive the line by characters. Then I would put the characters into an array. Then I would use a for-loop to see if there are characters or spaces and increment an int by 1.
 
  • #7


We have not even go to arrays yet, so I can't put it into that.

Can you help me with some sample code that would just count characters and digits...?? I am so lost on this one :(
 
  • #8


nukeman said:
"library <cctype>. Hint hint: I would use isalpha(); and test if character is alphabetical or isdigit for numbers.``

I am not too sure how to use that...

Code:
[B]#include <cctype>[/B]
#include <iostream>

using namespace std;
int main()
{
    char c;
    cin >> c;
    if([B]isalpha(c)[/B])
        cout << "You entered an alphabetic character." <<endl;

}
nukeman said:
We have not even go to arrays yet, so I can't put it into that.

Can you help me with some sample code that would just count characters and digits...?? I am so lost on this one :(

Ignore his comment about using an array. Strings have all the array functionality you might need.

Counting characters and digits is similar to counting spaces, you'd just use different functions to determine if each character is a digit or a space. You may use the functions isalpha and isdigit.
 
Last edited:
  • #9


MisterX, yes isalph and isdigit is what I must use, but i don't know how to wwrite it (I am VERY new to C++ )

Any help, or tutorials on how I can write this?
 

FAQ: Need help writing C++ program that counts non-whitespace characters ?

What is a non-whitespace character in C++?

A non-whitespace character in C++ is any character that is not a space, tab, or newline. This includes letters, numbers, punctuation marks, and special characters.

How do I count non-whitespace characters in a C++ program?

To count non-whitespace characters in a C++ program, you can use a loop to iterate through each character in a string or file and check if it is a non-whitespace character. If it is, you can increment a counter variable. Alternatively, you can use the built-in functions such as count_if or find_if from the algorithm library.

Can you provide an example of a C++ program that counts non-whitespace characters?

Yes, here is a simple C++ program that counts non-whitespace characters in a string:

#include <iostream>#include <string>using namespace std;int main() {    string str = "Hello World! 123";    int count = 0;    for (char c : str) {        if (!isspace(c)) {            count++;        }    }    cout << "Number of non-whitespace characters: " << count << endl;    return 0;}

How can I make my C++ program count non-whitespace characters from a file instead of a string?

To count non-whitespace characters from a file in a C++ program, you can use the ifstream class from the fstream library to read the file contents. Then, you can use the same logic as in the previous example to count the non-whitespace characters.

Is there a way to count non-whitespace characters without using a loop in C++?

Yes, you can use the count_if function from the algorithm library to count the number of non-whitespace characters in a string or file. It takes in a range of characters and a predicate function as parameters, which can be used to check for non-whitespace characters. This method can be more efficient compared to using a loop, especially for larger strings or files.

Similar threads

Replies
3
Views
1K
Replies
15
Views
2K
Replies
6
Views
3K
Replies
3
Views
2K
Replies
4
Views
2K
Replies
15
Views
4K
Replies
5
Views
2K
Back
Top