string::find and using it to control an array

I am trying to use string::find and string manipulators to control an array. (What I want to happen in the program is for the computer to look through a string variable and find a specific character. Every time it does, it adds one to an integer. It should continue this until the end of the string. When it reaches the end of the string, I want it to make a dynamic array of that many elements. I will then ask the program to look through the string again for the same character and store the position of the located character to a spot in the array. (The array should be filled when this is all said and done.) This
is part of a much larger plan...)

I try to follow the format I see here:
http://www.cplusplus.com/reference/string/string/find/

Basically, I want it to do something like this (for right now):

str = "This is a test string"

'T' found at position 0;
't' found at position 10;
't' found at position 13;
't' found at position 16;

Searching complete.

int t[4] = {0, 10, 13, 16}


The reality check is that my program is doing nothing like this. It is, instead of returning 3 or 4, returning a rather ridiculously large integer. Here is what I have so far:

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
#include <iostream>
#include <string>
#include <new>
using namespace std;

int main ()
{
    string str ("This is a test string");
    string sub;
    string::iterator it;
    size_t found;
    int x = 0;
    int * t;
    cout << "The length of str is " << str.length() << " characters.\n";
    it = str.begin();
    while (it<str.end())
    {
        found = str.find("t");
        if (found!=string::npos)
        {
            x++;
        }   //end if
        /*found = str.find('T');
        if (found!=string::npos)
        {
            x++;
        }   //end if*/
        it++;
    }   //end while
    cout<<"x = "<<x;
    //t = new (nothrow) int []
    return 0;
}


I, more than likely, made some dumb mistake by using it++, but I was only trying something similar to what I seen here: http://www.cplusplus.com/reference/string/string/end/
Last edited on
Why array instead of vector?

To match the desired output, you could do something like

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
#include <iostream>
#include <string>
#include <vector>
int main ()
{
    std::string str("This is a test string");
    std::cout << "str = \"" << str << "\"\n"; 
    std::vector<size_t> t;
    for(size_t n = str.find_first_of("tT"); n != std::string::npos;
               n = str.find_first_of("tT", n+1))
    {
        std::cout << '\'' << str[n] << "' found at position " << n << ";\n";
        t.push_back(n);
    }

    std::cout << "Searching complete\n"
              << "int t[" << t.size() << "] = {";
    if(!t.empty())
    {
        for(size_t n = 0; n < t.size()-1; ++n)
            std::cout << t[n] << ", ";
        std::cout << *t.rbegin();
    }
    std::cout << "}\n";
}
online demo: http://ideone.com/ljGZV
I didn't know much about vectors (until now: I have never been taught them formally), but thanks so much! Vectors kick ass!! Oh, and I think I learned a little more about for-loop usage from this example. You're a savior, man!! By the way, are vectors supposed to be a more complete form of data structure? (By more complete, I mean a data structure that more completely supports things you can do with data structures.)
Last edited on
Topic archived. No new replies allowed.