Finding all substrings

Nov 15, 2013 at 6:32pm
Hello, i must find every substring in string and output position of it. But i have no idea how i can find another substring in string when i already find one.

Its should looks like this Input & Output
Enter a string, a substring, and a character:
writingstringfunctionsforfarstrings str s
String 'str' found, position(s): 7, 28
Character 's' found 4 times.


but my code doing only this:
Enter a string, a substring, and a character:
writingstringfunctionsforfarstrings str s
String 'str' found, position(s): 7
Character 's' found 4 times.


So what i must change ? :)

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  #include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
    cout << "Enter a string, a substring, and a character:" << endl;
    char buffer[100], strc1[100], strc2[100], strc3[100];
    char strclow[100];
    if (gets(buffer) == NULL)
        {
        cout << "Nespravny vstup." << '\n';
        }

    if(sscanf(buffer,"%s%s%s", strc1, strc2, strc3)!= 3)
        {
          cout << "Nespravny vstup." << '\n';
          return 0;
        }



        int i=0;
        char a;
        while (strc1[i])
        {
        a=strc1[i];
        a = (tolower(a));
        strclow[i]=a;
        i++;
        }

    string s1 = strc1;
    string s2 = strc2;
    string s3 = strc3;
    string sx = strclow;

     if (s3.size() > 1)
        {
        cout << "Nespravny vstup." << '\n';
        return 0;
        }

int index = 0;
if(index = sx.find(s2))
if(index < 1)cout << "String '"<< s2 <<"' not found." << '\n';
else
cout << "String '"<< s2 <<"' found, position(s): "<< index << '\n';


int pocitadlo = 0;
for (unsigned int i = 0; i < s1.size(); i++)
{
 if(s3[0] == s1[i])
    pocitadlo++;


}
cout << "Character '"<< s3 <<"' found "<< pocitadlo << " times." << '\n';

    return 0;
}
Last edited on Nov 15, 2013 at 6:33pm
Nov 15, 2013 at 7:05pm
What you could do is have the find operation in a while loop, and each there's a successful match, start the next find operation from where the substring was found. The loop would terminate when find() finds nothing.

I'm attaching an article on the find member function because it can take other parameters, some of which you may find useful?
http://www.cplusplus.com/reference/string/string/find/

Line 48: Why is there an if here?

I hope I wasn't confusing there.

-Albatross
Nov 15, 2013 at 9:03pm
yes i know what i want do but i dont know how :( if i do something like this its stuck
        int index = 0;
for(unsigned int i = 0; i < s1.size();i++)
    {

if(index = sx.find(s2))
    {
   cout << "String '"<< s2 <<"' found, position(s): "<< index << '\n';
i = index;
break;
}

}
Nov 16, 2013 at 12:27am
Use the overload of find() which also takes a position as a parameter. start with position 0, then keep finding as long as the return value of find isn't string::npos.

Use an array to keep track of the positions at which the substrings were found, and use another counter to keep track of how many times it was found, then print the values at the end.
Nov 16, 2013 at 10:34am
how i overload find ? i dont know how i can do it
Nov 16, 2013 at 11:38am
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 <string>

int main()
{
    std::cout << "Enter a string, a substring, and a character: " ;

    std::string str, substr ;
    char c ;

    // eg. enter: writingstringfunctionsforfarstrings str s
    std::cin >> str >> substr >> c ;

    bool substring_was_found = false ;
    int cnt_char = 0 ;

    for( std::size_t pos = 0 ; pos < str.size() ; ++pos )
    {
        if( str.find( substr, pos ) == pos )
        {
            if( !substring_was_found )
            {
                std::cout << "String '" << substr << "' found, position(s): " ;
                substring_was_found = true ;
            }
            std::cout << pos << ' ' ;
        }

        if( str[pos] == c ) ++cnt_char ;
    }

    if( !substring_was_found ) std::cout << "String '" << substr << "' not found" ;
    std::cout << "\ncharacter '" << c << "' found " << cnt_char << " times.\n" ;
}
Nov 16, 2013 at 12:54pm
thank you :)
Topic archived. No new replies allowed.