Hi, I need some help with starting a program. Kind of lost on how to begin it, any guide to start it would be helpful.

Here is the link of the program: http://home.earthlink.net/~craie/122/labs/strfind.html

I need to do all the different levels for this program.

Thanks

Here some code to get you started.

int main(){
    //your code here
    return 0;
}

Post again when you have something in main.
Then people can help with any problems or specific questions about your code.

This (C++) lab will help you practice with C-strings and libraries. Function overloading (in C++) might come in handy, as well.

Two useful functions that are not provided in the standard library (but are with standard C++ string) are find a character in a string and find a substring in a string.

NOTE! These are also provided in C for C strings:

char* p = strchr( str, ch ); // find char ch in str
char* q = strstr( str, findStr ); // findStr in str

So it seems you are to write a 'wrapper' for your class xString that uses a dynamic C string inside the class to hold C strings.

So ... your code can just use the two above C provided functions ... (or you can 'Roll Your Own' - Which IS generally NOT advised!)

If you look at the class String example that I provided for you on an earlier post, you will see there, these examples:

int get_index( char charToFind ); // returns index or -1;
int get_index( const String& strToFind ); // returns index or -1 ... case specific
int get_index_ignore_case( const String& strToFind );

Also ... you may like to see this (revised) ...

class String:

thanks, aside from using the above char functions would I need to create a String class? And say for example if I have the string "I like to program" and if I wanted to find out the position of the t(how would I get a value for that position (7)).

Thanks

Take a look at this simple example ...

/* findCharInString.cpp */


/*

    ... say for example if I have the string:

    "I like to program"

    and if I wanted to find out the *INDEX* of the char 't'
    how would I get a value for that index =  7 ?

*/

#include <iostream>
#include <string>  /* or use your own class String */
#include <cctype> /* re. tolower */

using namespace std;

/* BUT ... better to use C++ library than to 'roll your own' */
int indexCharFoundIn( const string& s,  char c )
{
    int i;
    for( i = s.size() - 1; i >= 0; --i )
    {
        if( s[i] == c ) break;
    }
    return i; /* returned value is >= 0 if found, else -1 */
}

/* recall C/C++ char's are stored as integer values ... */
int takeInChar( const string& msg )
{
    cout << msg << flush;
    string reply;
    getline( cin, reply );
    if( reply.size() )
        return reply[0];
    // else ...
    return 0;
}
bool more()
{
    if( tolower( takeInChar( "\nMore (y/n) ? " )) == 'n' )
        return false;
    // else ...
    return true;
}

int main()
{
    do
    {
        cout << "Enter a line of text: " << flush;
        string testLine;
        getline( cin, testLine );

        cout << "Enter the char to find: " << flush;
        char c = cin.get();
        while( cin.get() != '\n' ) ; /* 'flush' cin stream ... */

        int i = indexCharFoundIn( testLine,  c );
        if( i != -1 )
            cout << "Found at index " << i << endl;
        else
            cout << c << " was NOT found in " << testLine
                 << endl;
    }
    while( more() ) ;
}

Take a look at this simple example ...

/* findCharInString.cpp */


/*

    ... say for example if I have the string:

    "I like to program"

    and if I wanted to find out the *INDEX* of the char 't'
    how would I get a value for that index =  7 ?

*/

#include <iostream>
#include <string>  /* or use your own class String */
#include <cctype> /* re. tolower */

using namespace std;

/* BUT ... better to use C++ library than to 'roll your own' */
int lastIndexCharFoundIn( const string& s,  char c )
{
    int i;
    for( i = s.size() - 1; i >= 0; --i )
    {
        if( s[i] == c ) break;
    }
    return i; /* returned value is >= 0 if found, else -1 */
}

/* recall C/C++ char's are stored as integer values ... */
int takeInChar( const string& msg )
{
    cout << msg << flush;
    string reply;
    getline( cin, reply );
    if( reply.size() )
        return reply[0];
    // else ...
    return 0;
}
bool more()
{
    if( tolower( takeInChar( "\nMore (y/n) ? " )) == 'n' )
        return false;
    // else ...
    return true;
}

int main()
{
    do
    {
        cout << "Enter a line of text: " << flush;
        string testLine;
        getline( cin, testLine );

        cout << "Enter the char to find: " << flush;
        char c = cin.get();
        while( cin.get() != '\n' ) ; /* 'flush' cin stream ... */

        int i = lastIndexCharFoundIn( testLine,  c );
        if( i != -1 )
            cout << "Last found was at index " << i << endl;
        else
            cout << c << " was NOT found in " << testLine
                 << endl;
    }
    while( more() ) ;
}

You may also like to see using strchr ... the C string version of finding a char in a C type string?

Note the use of 'pointer arithmetic', when using strchr, to find the index (offset) value of the found char ... if found.

/* findCharInString3.cpp */

/* comparing ...
   1. C++ library char find... function
   2. C++ 'roll your own char find... function
   3. C style char find... function ... (strchr)
*/


#include <iostream>
#include <string>  /* C++ string or use your own class String */
#include <cctype> /* re. tolower */

/* Today's C++ uses <cstring> ... and not <string.h> */
#include <cstring>  /* re. strchr */


using namespace std;

/* Note: it is considered good style to use C++ library
   and not to 'roll your own' functions ... */
int lastIndexCharFoundIn( const string& s,  char c )
{
    int i;
    for( i = s.size() - 1; i >= 0; --i )
    {
        if( s[i] == c ) break;
    }
    return i; /* returned value is >= 0 if found, else -1 */
}

/* recall C/C++ char's are stored as integer values ... */
int takeInChar( const string& msg )
{
    cout << msg << flush;
    string reply;
    getline( cin, reply );
    if( reply.size() )
        return reply[0];
    // else ...
    return 0;
}
bool more()
{
    if( tolower( takeInChar( "\nMore (y/n) ? " )) == 'n' )
        return false;
    // else ...
    return true;
}

int main()
{
    do
    {
        cout << "Enter a line of text: " << flush;
        string s;
        getline( cin, s );

        char c = takeInChar( "Enter the char to find: " );

        // using 'roll your own' find... //

        int i = lastIndexCharFoundIn( s,  c ); 

        if( i != -1 )
            cout << "Last " << c
                 << " found was at index " << i << endl;
        else
            cout << c << " was NOT found in " << s << endl;


        // using C++ library find... //

        size_t pos = s.find_first_of( c );

        if( pos != string::npos )
            cout << c << " first occured at index "
                 << pos << endl;

        // using C string library find... //

        // firstly, convert C++ string to a (tmp) C string
        const char* tmp = s.c_str();

        char* p = strchr( tmp, c ); // find char c in tmp C string
        if( p ) // if pointer value NOT equal NULL
            cout << c << " first occured at index "
                 << (p-tmp) << endl; // see pointer arithmetic

    }
    while( more() ) ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.