# Counting Words in A String

Page 1 of 1

## 5 Replies - 12541 Views - Last Post: 12 February 2012 - 07:45 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=266202&amp;s=f46f4e974a200c132216ccfc3d388e90&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cieru

Reputation: 0
• Posts: 17
• Joined: 16-January 12

# Counting Words in A String

Posted 10 February 2012 - 10:39 AM

Good Day! I was currently working on a C++ Program that will count the number of words in and entered string...I figured out already how to count the words...But we must repeat the process by using do while loop...That is the start of my problem...The loop is working fine...But in the secong run for counting the words, the output always result the number of words from the first trial ...

here's my code:

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

void newline();

int main()
{
char s[100];
char ans;
int numWords = 0;
int i=0;
do{
numWords=0;
cout <<"\nEnter a String: ";
cin.getline(s,100);
while(i<strlen(s))
{

if(s[i] != '.' && s[i] != '!' && s[i] != '?'
&& s[i] != ' ' && s[i] != ','){
numWords++;
while(s[i] != '.' && s[i] != '!' && s[i] != '?'
&& s[i] != ' ' && s[i] != ',' && i < strlen(s)){
i++;
}
}
else {
i++;
}

}
cout<<"\nThe String contains : ";
cout << numWords << endl;
cout << "\nTry Again? (Y or N): ";
cin >> ans;
newline();
}while(ans =='y' &&'Y');
cout <<"\nThank You! Press Enter to Exit the Program...";

return 0;
system ("pause>0");
}

void newline()
{
char s;
do{
cin.get(s);
}while(s!='\n');
}
```

any suggestions? I've checked the program for a hundred times now..>,<...sorry for being like this...I'm totally a beginner Thank you

Is This A Good Question/Topic? 0

## Replies To: Counting Words in A String

### #2 jimblumberg

Reputation: 5285
• Posts: 16,458
• Joined: 25-December 09

## Re: Counting Words in A String

Posted 10 February 2012 - 11:50 AM

In the following snippet, after your first run:
```	do{
numWords=0;
cout <<"\nEnter a String: ";
cin.getline(s,100);
while(i<strlen(s))
```

What is the purpose of your variable i in the second loop? What is it's value after your first run? What is the value of strlen(s)? What happens if strlen(s) for the second run is smaller than the previous run?

Jim

### #3 shurd

Reputation: 37
• Posts: 162
• Joined: 05-February 12

## Re: Counting Words in A String

Posted 10 February 2012 - 12:00 PM

You are just no reseting the i, do it after
```numWords=0
```

do
```i=0
```

### #4 vividexstance

• Tiocfaidh ár lá

Reputation: 791
• Posts: 2,867
• Joined: 31-December 10

## Re: Counting Words in A String

Posted 10 February 2012 - 01:38 PM

It really looks like you're doing more work than you really have to. You should check out all the member functions for strings because you can do most of what you need with just those functions. For instance, you use the strlen function which is fine but another way would be too just use either the size() or length() member function to get the length of the string. Here's a good reference site: http://cplusplus.com/reference/.

### #5 shurd

Reputation: 37
• Posts: 162
• Joined: 05-February 12

## Re: Counting Words in A String

Posted 10 February 2012 - 02:34 PM

@vividexstance - It's a C-string so it does not have those methods. Also I'd rather encourage him to use C-strings so he can learn how to manage and understand how things work. When I took my first lessons with programming in college we couldn't even use functions from "cstring" to actually learn as much as possible about char arrays and c-strings.

### #6 cieru

Reputation: 0
• Posts: 17
• Joined: 16-January 12

## Re: Counting Words in A String

Posted 12 February 2012 - 07:45 AM

Thank for the help I'll try to check the given links and new ideas to improve my program...

shurd, on 10 February 2012 - 12:00 PM, said:

You are just no reseting the i, do it after
```numWords=0
```

do
```i=0
```

OH thanks! I tried to change the positioning of the i, and numWords within the program )) It functions succesfully Thanks again