Thread: Simple Function program, with Strings?

  1. #1
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30

    Simple Function program, with Strings?

    I am using O'reilly's Practical C++ Programming book in its 2nd edition. I have to create a program to complete the following question:

    Write a function begins(string1, string2) that returns true if string1 begins string2.

    This was confusing to me at first, and I think poorly written. From what I understand, I need to write a program that inputs a first string, and then a second string. Then, I guess it should input a third string - containing both first and second strings in any order - and then output true or false depending on the order..

    As an example: Enter your first name, Enter your last name, Enter your Full name (either firstname first or firstname last), and then output true or false depending on the circumstances.., etc. I assume I can use a IF statment to determine the order of the strings..?

    Please let me know if my grasp on the question is correct, or if I am way off.

    I have the code started, and need some help - but I wanted to make sure I was approaching the question correctly before I flood the thread with questions


    Thanks all =)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I think you only need to input two strings.

    For example, entering
    hello
    hell

    would return true

    Whereas
    hello
    world

    would return false.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    Ok, got it. I knew I was interpreting it incorrectly.

    What control statment is reccommended to search a string for another string??

    Heres where I am at:

    Code:
    #include <iostream>
    #include <string>
    
    
    std::string input1;     //First string
    std::string input2;     //Second string
    
    int main()
    {
    
    std::string begins(std::string string1, std::string string2);  //Declares Function
    
    std::cout << "Please enter the first string: ";
    std::getline(std::cin, input1);         //Gets first string
    
    std::cout << "Please enter the second string: ";
    std::getline(std::cin, input2);         //Gets second string
    
    
    begins(input1, input2);  // Calls Function
    
    void begins(std::string string1, std::string string2)
    {
                    //Will Contain control statments to determine if input1 contains input2
    }
    
    
    return (0);
    }

    Thanks all

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I guess this one line is left for you to figure out, as this is the whole point of the exercise. If you want to see what tools are available (string methods), look up a reference.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Actually,
    Code:
    #include <iostream>
    #include <string>
    
    std::string begins(std::string string1, std::string string2);  //Declares Function
    
    int main()
    {
    
    std::string input1;     //First string
    std::string input2;     //Second string
    
    std::cout << "Please enter the first string: ";
    std::getline(std::cin, input1);         //Gets first string
    
    std::cout << "Please enter the second string: ";
    std::getline(std::cin, input2);         //Gets second string
    
    begins(input1, input2);  // Calls Function
    
    return (0);
    }
    
    void begins(std::string string1, std::string string2)
    {
                    //Will Contain control statments to determine if input1 contains input2
    }
    Now, what will begins() do? Will it modify either of the strings? Right now, when you call your function, it will copy the given strings so that the function has its own special copies. Will that be necessary? I'm willing to say no. So then why not let the function simply refer to the strings you gave it, and in a way where the strings will not be modified?
    Code:
    void begins(const std::string & string1,const std::string & string2)
    {
                    //Will Contain control statments to determine if input1 contains input2
    }
    Don't forget to update the declaration.

    Now, for what to do with the function (which is up to you)... you're going to be finding something... or, maybe considering little strings within the bigger strings...
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Big hint: Look at std::equal

  7. #7
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    First off, thanks everyone for their input thus far..

    Heres how I want to handle this..

    Two strings are inputted, I need to see if the first string begins the second string (hell, hello = true)

    Anyway.. Heres what I think I need to do..

    1. Get the length of the first string
    Code:
    length = std::strlen(input1)
    2. Copy string 2 using only the number determined by length from above.
    Code:
    std::strncpy(string2, string1, length)
    3. Use strcmp to compare the two inputted strings.
    Code:
    std::strcmp(string, string2)
    4. Use an IF statement that returns TRUE if the strings match, and FALSE if otherwise..

    Is this the correct way of tackling this program??

    thx all

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That looks like a good way to do it, but you shouldn't be using strlen, strncpy and strcmp since those are for C style strings. Your program above uses the C++ string class, so you would use length(), substr(), and ==.

    Also be careful to handle the case when the first string is longer than the second.

  9. #9
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    Quote Originally Posted by Daved View Post
    That looks like a good way to do it, but you shouldn't be using strlen, strncpy and strcmp since those are for C style strings. Your program above uses the C++ string class, so you would use length(), substr(), and ==.

    Also be careful to handle the case when the first string is longer than the second.
    Heres where I'm at...
    Code:
    #include <iostream>
    #include <string>
    
    
    std::string input1;     //First string
    std::string input2;     //Second string
    int length;
    std::string crop;       //Second string, cropped to same length as first string
    
    int main()
    {
    
    
    std::cout << "Please enter the first string: ";
    std::getline(std::cin, input1);         //Gets first string
    
    std::cout << "Please enter the second string: ";
    std::getline(std::cin, input2);         //Gets second string
    
    
    
    length = input1.length();
    input2.copy(crop, length);
    
    std::cout << "Length: " << length << '\n';
    
    
    return (0);
    }
    Code:
    input2.copy(crop, length);
    ^^ is an attempt to copy input2 to a new string, crop, but only to the length of input1, which should be defined by the length int variable.

    Heres the errors its throwing when compiled:

    ex9-2.cpp: In function `int main()':
    ex9-2.cpp:24: no matching function for call to `basic_string<char,string_char_traits<char>,__defa ult_alloc_template<false,0> >::copy (string &, int &)'
    /usr/include/g++/std/bastring.cc:232: candidates are: size_t basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> >::copy(char *, unsigned int, unsigned int = 0) const



    thanks =)
    Last edited by otchster; 06-22-2007 at 01:01 PM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'd use the substr method rather than copy.

  11. #11
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    Ok, when I try to use substr, heres what I get:

    Code:
    #include <iostream>
    #include <string>
    
    
    std::string input1;     //First string
    std::string input2;     //Second string
    int length;
    std::string crop;       //Second string, cropped to same length as first string
    
    int main()
    {
    
    
    std::cout << "Please enter the first string: ";
    std::getline(std::cin, input1);         //Gets first string
    
    std::cout << "Please enter the second string: ";
    std::getline(std::cin, input2);         //Gets second string
    
    
    
    length = input1.length();
    crop = input2.substr(length);
    
    std::cout << "Length: " << length << '\n';
    std::cout << "input 2 cropped = " << crop << '\n';
    
    return (0);
    }
    The program DOES compile correctly without errors. For the first string, I enter "hell", and for the second string I enter "hello".. It says the first string is length 4 (correct), but then it outputs "o". Its simply taking 4 chars off the beginning of the second string. I need it to crop them from the end.

    If the first strings 4 long, I need to make string 2 (no matter how long) just the first 4 chars. From here I will use a == IF statement to compare.

    How do I use substr to remove all chars except the defined beginning length?

    thank you =)

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The first parameter of substr is the the index of the first character you want to include in your substring. So when you used 4 on the string hello, you start with the letter 'o' ('h' is index 0, 'e' is 1, 'l' is 2, 'l' is 3 and 'o' is 4).

    So change that first parameter to the index of the first character you want to include.

    It is the second parameter to substr that is the number of characters you want.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    This should take a single line of code, people:

    Code:
    if(string1.size() <= string2.size() && std::equal(string1.begin(), string1.end(), string2.begin()))
        // it matches

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    This is an exercise to see if the OP can figure out how to solve the problem, not to see what the "best way" is.

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    This is an exercise to see if the OP can figure out how to solve the problem, not to see what the "best way" is.
    If the intention is to comprehend how to manipulate the individual characters of strings, then I think the exercise is quite bizarre, since the entire point of std::string is to insulate you from the representation of the string as a sequence of characters.

    In other words, making people "look inside" a std::string only teaches them that that's the right way to deal with strings, and it's not (at least in C++).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM