Locate substring question

Nov 20, 2014 at 6:07pm
Hi guys! I'm trying to write a little program that would do these tasks in order:
Ex:
******************************************
Enter a word : alabama
Enter a letter you want to search: a

Result:

Letter a found in position 1,3,5 and 7.
*******************************************

I've been trying to code it with strstr but I can't make it work with the cin fonction.

Thanks a lot for your help!
Nov 20, 2014 at 6:24pm
If you just want to find a single character, you don't need to use substring. But either way, you just need to start your search after the previously found result.

http://en.cppreference.com/w/cpp/string/basic_string#Search
Last edited on Nov 20, 2014 at 6:25pm
Nov 20, 2014 at 7:14pm
Thanks and great documentation. I'm still having a hard time finding all the positions of a certain letter, it seems that only the first position is showed.
Any tips here?

Thanks
Nov 20, 2014 at 7:44pm
So far here's my code:


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>
#include <conio.h>
#include <string>
#include <iomanip>
using namespace std;
main() {

string word; 
string letter;
const int maxchar = 10; 
const int maxletter = 1;     
cout << "Enter a word(Max 10 char): " <<endl;       
cin >> word;
if (word.length() < maxchar) {
cout <<"Invalid word!"<<endl;               
getch ();
return 0;
}    
cout << "Enter the letter you want to find " <<endl;       
cin >> letter;
if (letter.length() > maxletter) {
cout <<"Only 1 letter please!"<<endl;               
getch ();
return 0;

}    
cout << "Your word: " << word <<endl;
cout << "Your letter: " << lettre <<endl;
cout << "The letter " << letter << " is found at position: " <<endl;
getch();
return 0;
}



I just can't get find to work on this code, thanks for your input
Nov 20, 2014 at 9:28pm
Anybody have an idea on how I could implement the find function in the code above..

Thanks a lot!
Nov 20, 2014 at 10:29pm
The find function takes the position you want to start searching from as one of its parameters. Just start searching after the last found location.
Nov 27, 2014 at 5:48pm
Thanks for you that. I just added the find function and it seems to work but the problem is that it only gives me the first occurence of the letter I'm searching. It looks like I have to add something to word.find(letter) but I can't get it. I need all position occurences, how can I do that?

Here's my code 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
32
33
34
#include <iostream>
#include <conio.h>
#include <string>
#include <iomanip>
using namespace std;
main() {

string word; 
string letter;
string::size_type position;
const int maxchar = 10; 
const int maxletter = 1;     
cout << "Enter a word(Max 10 char): " <<endl;       
cin >> word;
if (word.length() < maxchar) {
cout <<"Invalid word!"<<endl;               
getch ();
return 0;
}    
cout << "Enter the letter you want to find " <<endl;       
cin >> letter;
if (letter.length() > maxletter) {
cout <<"Only 1 letter please!"<<endl;               
getch ();
return 0;

}    
cout << "Your word: " << word <<endl;
cout << "Your letter: " << letter <<endl;
cout << "The letter " << letter << " is found at position: " <<endl;
cout << word.find(letter) <<endl;
getch();
return 0;
}
Nov 27, 2014 at 6:19pm
You can only get one occurrence at a time. Once you find an occurrence you need to search again but this time start after the occurrence you just found. Repeat this process until you can no longer find another occurrence.
Last edited on Nov 27, 2014 at 6:20pm
Nov 27, 2014 at 6:29pm
That's what I'm try to do but I can't figure it out. I'm trying to put it in a loop but I can't get it to work. Thanks anyway! :)
Last edited on Nov 27, 2014 at 6:31pm
Nov 27, 2014 at 6:34pm
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
#include <iostream>
#include <string>

int main()
{
    const int maxchar = 10;
    std::string word ;
    std::cout << "Enter a word(Max " << maxchar << " char): " ;
    std::cin >> word ;
    // if( word.size() > maxchar ) etc.

    /*string*/ char letter; // use the type char to hold one character
    // const int maxletter = 1;
    std::cout << "Enter the letter you want to find: " ;
    std::cin >> letter ;

    std::cout << "Your word: " << word << '\n'
              << "Your letter: " << letter << '\n'
              << "The letter '" << letter << "' is found at positions: " ;

    for( std::size_t pos = 0 ; pos < word.size() ; ++pos )
    {
       if( word[pos] == letter ) std::cout << pos << ' ' ;
    }
    std::cout << '\n' ;
}
Nov 27, 2014 at 6:49pm
Thanks a lot! I'll study this code! Have a good day!
Topic archived. No new replies allowed.