ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In the book I am reading the suggested exercise is to write a function to count the number of words in a string. Then include what I define as a word in my documentation. Well, I haven't done alot with strings so I'm feeling a bit overwhelmed.
For this exercise a word should be:
A series of alphabetical characters
Seperated by spaces or commas
(Any other suggestions? I could get real thorough, but its just an exercise.)
Also, I am not clear how to proceed. Creating the function is not hard, but how do I parse a string??
Since this is a exercise, you should write the actual code, but it's not too hard.
Get a string from the user, then loop through the string until '\0'
When looping, look for spaces, commas, tabs, newlines (if you keep getting input until EOF) and if found, use a variable to track how many times (count++).
It has been a while since I used C++, so I won't post any real code since it will be wrong . But search for the function len(). Len counts the number of characters(length) in a string. Create two variables:
int numberWords;
String yourString;
Then make a for-next loop:
for (int i = 0; i <= yourString.len(); i++) {
...
// inside this loop, check to see if the hole string has "space" or "comma"
// if it has, then increase numberWords by one
// numberWords = numberWords +1
}
// show the results:
cout << numberWords << end;
I think you got the idea. The code above is all wrong for sure, but it's for you to get an idea
Oh yeah, you'll probably want to make some variable to tell if you're inside a word or not. If you are inside a word and a space, comma etc comes up, now you're outside. When a character comes up thats not some kind of space, you're inside the word and can upper the amounts of counted words.
/* Thanks all for the advice and help. I have made a version myself, but I am getting a compiler error. It would be great if ya'll could look at it. I think I've just been missing a small fundamental issue, hence my error. */
#include <string.h>
#include <iostream.h>
main() {
char user_string[100];
int words_in_string = 0;
cout << "This program takes a sentence input by the user and returns" << '\n';;
cout << " the amount of words." << '\n';
cout << "Please input now: ";
cin.getline(user_string, sizeof(user_string)); // gets the string to use
int word_count(char input_string); // calling the function
words_in_string = word_count(user_string[100]); // thought this would work
if(words_in_string == 1) {
cout << "There is " << words_in_string << " word in the string." << '\n';
}
else {
cout << "There are " << words_in_string << " words in the string." << '\n';
}
return (0);
}
/*****************************************************************
* word_count -- counts words in a given string. Considers a period, comma and space *
* bar as word seperators. Uses ASCII code to determine. *
* *
* Parameters *
* some string *
* *
* Returns *
* num_words -- number of words in string *
******************************************************************/
int word_count(char input_string) // funtion to find how many words
{
int counter = 0;
char variable_x;
int num_words = 0;
for (variable_x = input_string[0]; variable_x != '\0'; counter++) { // looping through string
if((variable_x == 32) || (variable_x == 46) || (variable_x == 44)) { // looking for spaces
num_words++;
}
variable_x = input_string[counter];
}
return (num_words); // answer to how many words.
}
// my compiler errors were: (c++ source file was called 9-1.cc Both errors within the function)
// the first error was the beginning of the for loop, the second was "variable_x = input_string[counter]; " Can I not do this?
//9-1.cc:38: error: invalid types `char[int]' for array subscript
//9-1.cc:42: error: invalid types `char[int]' for array subscript
forgive me, I think the word wrap screwed up some of the code. Especially the "{}" Excuse the code. Just look over the concepts if you could. Thanx guys.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i, numspaces;
char nextChar;
string msg;
numspaces=1;
cout << "Type in a string\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[i]))
numspaces++;
}
cout << "\nThere are " << numspaces << " words in this string.";
cin.ignore();
return 0;
}
Please stop keeping this thread alive it was started over five years ago and your comment did not add anything of value. In fact from looking at your post history, I do not think any of your posts have added value
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.