really need help with string!!!!urgent!

This programm counts words from input and then i have to print out the longest word, im i bit stuck on that, need to somehow loop trough all words and compare them, but thing is, that there can be whatever count of words and spaces, depends on input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include<iostream>
#include<cstring>
using namespace std;
int main()

{
    string teikums;
    string b;
    int vardi, garums, position, nextposition;
    cout<<"uzraksti teikumu!\n";
    getline (cin,teikums);
    
    garums=teikums.length();
    vardi=1;
    
    for (int i=0; i<garums; i++) 
       {
        if (teikums[i]==' ') vardi++; 
        }
        cout<<"vardu skaits teikumaa: "<<vardi<<endl; 
  

      position=teikums.find(" "); 
      nextposition=teikums.find(" ", position+1); 
      if ((nextposition-position)>position) 
  
     
    
    system ("pause");
    return 0;
}
The stream operator >> skips whitespace by default. Read single words from teikums via std::stringstream.

How about "teikumu!"? How long is it? Punctuation is not whitespace, but it is not part of a word either, is it?
Last edited on
"teikums" is just my string name, it can be indefinitely long, depends on input. in this task i only have to use white spaces, no punctuations, have to enter sentence only with white spaces.
What I did mean was: How long is this example word?
teikumu!?
123456789

7 or 9 characters? This time you leave it to the user to write proper input.

Pedantically, whitespace includes tabulators and newlines; not just space.


Look at http://www.cplusplus.com/reference/sstream/istringstream/istringstream/
The example has "stringvalues". You have "teikums".
The example converts words to int. You want std:string.
The example calculates and shows. You want max of string::size().
teikums=string, teikums in my language=sentence,thats just how i named my string, yes its up to user, how long the string will be, the size is not defined.you have to enter the sentence that will be stored in "teikums"
ah and there is another string b, which i was thinking to use as substr.
in english might be easier to understand what i have here :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 #include<iostream>
#include<cstring>
using namespace std;
int main()

{
    string sentence;
    string b;
    int words, len, position, nextposition;
    cout<<"Write a sentence!\n";
    getline (cin,sentence);
    
   len=sentence.length();
    words=1;
    
    for (int i=0; i<len; i++) 
       {
        if (sentence[i]==' ') words++; 
        }
        cout<<"There are this amount of words in a sentence: "<<words<<endl; 
  

      position=sentence.find(" "); 
      nextposition=sentence.find(" ", position+1); 
      if ((nextposition-position)>position) 
  
     
    
    system ("pause");
    return 0;
}
Last edited on
Did you read the example from the link?

Try::
11
12
13
14
15
16
17
getline( cin, teikums );
std::istringstream iss( teikums );
std::string vard;
while ( iss >> vard )
  {
    std::cout << "'" << vard << "' " << vard.size() << '\n';
  }

this works,thanks, I did read the link, just im very much a beginner and dont really understand this function, as far as I understand is, istringstream copies my previous string teikums into new array and words are separated now, somehow i also have to make this display just the longest word
You obviously will print nothing during the loop. If you had an array of numbers, how would you find a maximum value? Same principle works here.
Topic archived. No new replies allowed.