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...)
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:
#include <iostream>
#include <string>
#include <new>
usingnamespace 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 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.)