- #1
- 4,652
- 38
I believe I am stuck. I need to traverse an array which will contain a sentence the user enters and identify the array positions of the first and last letters of each word in the sentence. I can do the first word, but I am not sure how to move it along after that. I am thinking I need to always store my last position of a previous word and use that when finding the first position of a new word but not sure how to do this. And I'm not sure how to move past the white spaces, but also stop at the end of the array. Thanks for any help.
For an example - Desired output for sentence "my dog barks." would be:
first letter array position: 0
last letter array position: 1
first letter array position: 3
last letter array position: 5
first letter array position: 7
last letter array position: 11
For an example - Desired output for sentence "my dog barks." would be:
first letter array position: 0
last letter array position: 1
first letter array position: 3
last letter array position: 5
first letter array position: 7
last letter array position: 11
Code:
#include <iostream>
using namespace std;
int main()
{
char sentence[80];
cout <<"Enter a sentence (no caps): \n";
cin.getline(sentence,80);
int i = 0;
while (sentence[i] != '\0')
{
if (sentence[i] <'a' || sentence[i] >'z')
{
int last = i-1;
int first = 0;
cout << "first letter array position: " <<first <<endl;
cout << "last letter array position: " <<last <<endl;
}
i++;
}
return 0;
}