Sting Function

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
33
34
#include <iostream>

int main(){

 string str1, str2, str3;

 while (cin >> str1){
  while (cin >> str2){
   while (cin >> str3){

  cout << "The New Set:" << endl;
  cout << "First String is " <<  str1 << " and contains " << str1.length()
       << " characters." << endl;
  cout << "Second String is " << str2 << " and contains " << str2.length()
       << " characters." << endl;
  cout << "Third String is " << str1 + str2 << " and contains " <<   str1.length() + str2.length() << " characters." << endl;

  if(str3.find(str1) || str3.find(str2))
  cout << "The word " << str3 << " is found in the first string at position " 
       << str1.location[str3] << endl;
  cout << "The word " << str3 << " is found in the second string at position "
       << str2.location[str3] << endl;
  cout << "The word " << str3 << " is found in the third string at position " 
       << 
     else
  
    }
   }
  }

 

return 0;
}  

The output is supposed to be:
A new set:
First string is “This.is.” and contains 8 characters.
Second string is “a.nonsense.sentence.” and contains 20 characters.
Third string is “This.is.a.nonsense.sentence.” and contains 28 characters.
The word “ten” is not found in the first string.
The word “ten” is found in the second string at position 14.
The word “ten” is found in the third string at position 22.

A new set:
First string is “WhyamIso” and contains 8 characters.
Second string is “silly?” and contains 6 characters.
Third string is “WhyamIsosilly?” and contains 14 characters.
The word “sos” is not found in the first string.
The word “sos” is not found in the second string.
The word “sos” is found in the third string at position 6.

The longest string contained 28 characters.
Do I have the code correct so far?
you need std::string and std::cout and to #include <string>
otherwise you will get errors on all strings and cout statements
well, the issue is (sadly) that I don't know how to correctly use the find function
std::string::find will return std::string::npos if it doesn't find the string it's looking for. If you compare the returned value with that npos value, you should be good to go!

-Albatross
This is what I have right now

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 <string>

int main(){

 string str1, str2, str3, line;
  
 size_t found;
  
 while (getline(cin,line)){

  cout << "The New Set:" << endl;
  cout << "First String is " <<  str1 << " and contains " << str1.length() << " characters." << endl;
  cout << "Second String is " << str2 << " and contains " << str2.length() << " characters." << endl;
  cout << "Third String is " << str1 + str2 << " and contains " <<   str1.length() + str2.length() << " characters." << endl;
  found=str1.find(str3);
  if (found!=string::npos) 
     cout << "The word '" << str3 << "'cannot be found in the first string" << endl; 
  found=str2.find(str3);
  if (found!=string::npos) 
     cout << "The word '" << str3 << "'cannot be found in the second string" << endl;
  found=cat.find(str3); //I don't know how to structure a find in a concatenation 
  if (found!=string::npos) 
     cout << "The word '" << str3 << "'cannot be found in the third string" << endl;
    }
  

 

return 0;
}  
I'm getting

This.is.
a.nonsense.statement
ten
The New Set:
First String is This.is. and contains 8 characters.
Second String is a.nonsense.statement and contains 20 characters.
Third String is This.is.a.nonsense.statement and contains 28 characters.
The word 'ten' is not found in the first string
The word 'ten' is not found in the second string //should be found
The word 'ten' is not found in the third string //should be found
WhyAmISo
The New Set:
First String is This.is. and contains 8 characters.
Second String is a.nonsense.statement and contains 20 characters.
Third String is This.is.a.nonsense.statement and contains 28 characters.
The word 'WhyAmISo' is not found in the first string
The word 'WhyAmISo' is not found in the second string
The word 'WhyAmISo' is not found in the third string
silly?
The New Set:
First String is This.is. and contains 8 characters.
Second String is a.nonsense.statement and contains 20 characters.
Third String is This.is.a.nonsense.statement and contains 28 characters.
The word 'silly?' is not found in the first string
The word 'silly?' is not found in the second string
The word 'silly?' is not found in the third string
sos
The New Set:
First String is This.is. and contains 8 characters.
Second String is a.nonsense.statement and contains 20 characters.
Third String is This.is.a.nonsense.statement and contains 28 characters.
The word 'sos' is not found in the first string
The word 'sos' is not found in the second string
The word 'sos' is not found in the third string
I... don't see a variable named "cat" in your program, though you use it on line 22. :/

Also, find() returns std::string::npos when there is no match, otherwise it tells you where the first character of the match is. I think I should have emphasized that. :)

-Albatross
Last edited on
yea...I don't know why I did that
I just declared str 4 as str 1 + str 2

but its thinking it can't find stuff when I know it should find something, and it should match the output that I previously posted a few replies back. Why is it reading another line whilst using the same information as the first set of lines?
Last edited on
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

int main(){

 string str1, str2, str3, str4;
 size_t found;
 
 
  
    while (cin >> str1){
    
     while (cin >> str2){
    str4=str1+str2;
      while (cin >> str3){





       cout << "The New Set:" << endl;
       cout << "First String is " <<  str1 << " and contains " << str1.length() << " characters." << endl;
       cout << "Second String is " << str2 << " and contains " << str2.length() << " characters." << endl;
       cout << "Third String is " << str1+str2 << " and contains " <<   str1.length() + str2.length() << " characters." << endl;
       found=str1.find(str3);
         if (found!= string::npos) {  
            cout << "The word '" << str3 << "' is found at position " << int(found) << "." << endl;
                                   }
         else                      
            cout << "The word '" << str3 << "' is not found in the first string." << endl; 
                                   
       found=str2.find(str3);       
         if (found != string::npos) {
            cout << "The word '" << str3 << "' is found at position " << int(found) << "." << endl;
                                    }
         else          
            cout << "The word '" << str3 << "' is not found in the second string." << endl; 
                                     
      found=str4.find(str3);
         if (found != string::npos)   {
            cout << "The word '" << str3 << "' is found at position " << int(found) << "." << endl;
                                      }
         else                         
            cout << "The word '" << str3 << "' is not found in the third string." << endl;
         cout << "\n";
                       }
                        }
                         }
                        
 

   return 0;
}  


CURRENT OUTPUT:

This.is.
a.nonsense.sentence.
ten
The New Set:
First String is This.is. and contains 8 characters.
Second String is a.nonsense.sentence and contains 20 characters.
Third String is This.is.a.nonsense.sentence and contains 28 characters.
The word 'ten' is not found in the first string.
The word 'ten' is found at position 14.
The word 'ten' is found at position 22.

WhyAmISo
The New Set:
First String is This.is. and contains 8 characters.
Second String is a.nonsense.sentence and contains 20 characters.
Third String is This.is.a.nonsense.sentence and contains 28 characters.
The word 'WhyAmISo' is not found in the first string.
The word 'WhyAmISo' is not found in the second string.
The word 'WhyAmISo' is not found in the third string.

silly?
The New Set:
First String is This.is. and contains 8 characters.
Second String is a.nonsense.sentence and contains 20 characters.
Third String is This.is.a.nonsense.sentence and contains 28 characters.
The word 'silly?' is not found in the first string.
The word 'silly?' is not found in the second string.
The word 'silly?' is not found in the third string.

sos
The New Set:
First String is This.is. and contains 8 characters.
Second String is a.nonsense.sentence and contains 20 characters.
Third String is This.is.a.nonsense.sentence and contains 28 characters.
The word 'sos' is not found in the first string.
The word 'sos' is not found in the second string.
The word 'sos' is not found in the third string.

EXPECTED OUTPUT:
A new set:
First string is “This.is.” and contains 8 characters.
Second string is “a.nonsense.sentence.” and contains 20 characters.
Third string is “This.is.a.nonsense.sentence.” and contains 28 characters.
The word “ten” is not found in the first string.
The word “ten” is found in the second string at position 14.
The word “ten” is found in the third string at position 22.

A new set:
First string is “WhyamIso” and contains 8 characters.
Second string is “silly?” and contains 6 characters.
Third string is “WhyamIsosilly?” and contains 14 characters.
The word “sos” is not found in the first string.
The word “sos” is not found in the second string.
The word “sos” is found in the third string at position 6.

The longest string contained 28 characters.

How can I edit my code to produce the expected output?
Last edited on
could someone please help me get the expected output...I've been staring at my code for so long
Topic archived. No new replies allowed.