Thread: Simple Function program, with Strings?

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think the benefit of the exercise is come up with an algorithm that solves the problem given your available skillset. If the OP understands algorithms and was able to find equal and use it, then I'd say great. But otherwise, identifying a series of steps to accomplish the task is what programming is all about.

    The string is just the vehicle. It's pretty irrelevant to the task in my eyes.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The begins() in the code above still returns void. Not good.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    Quote Originally Posted by brewbuck View Post
    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
    Code:
    #include <iostream>
    #include <string>
    
    
    std::string string1;    //First string
    std::string string2;    //Second string
    
    int main()
    {
    
    
    std::cout << "Please enter the first string: ";
    std::getline(std::cin, string1);                //Gets first string
    
    std::cout << "Please enter the second string: ";
    std::getline(std::cin, string2);                //Gets second string
    
    if(string1.size() <= string2.size() && std::equal(string1.begin(), string1.end(), string2.begin()))
            {
            std::cout << "It matches";
            }
    else
            {
            std::cout << "It does not match.";
            }
    
    
    return (0);
    }
    Throws an error saying:

    ex9-2.cpp: In function `int main()':
    ex9-2.cpp:19: `::equal' undeclared (first use here)


    It says ::equal is undeclared. That to me means that the compiler is interpreting equal as a variable...? Isn't it a string function method? I can't a type, is that is the problem..

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you want to use equal, you have to #include the proper header that includes it. You should be able to find the proper header in your reference book/website.

  5. #20
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    A way I thought of (though, again, not the best way) is
    Code:
    std::string s1("hello"), s2("hell");
    if(s1.find(s2) == 0)   std::cout << "Yup." << std::endl;
    else   std::cout << "Nope." << std::endl;
    Last edited by CodeMonkey; 06-22-2007 at 10:30 PM. Reason: CODE TAGS
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #21
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    CodeMonkey that is correct to a certain extent, but it does not correctly answer the entire question. The question as if string one starts with string two. Although that does give a relatively decent answer. I dont know why not just get a substring of string1 as the length of string2, and compare string1.substr(0, string2.length()) == string2. Then again that may be incorrect.
    [EDIT]
    it does work. here ya go. simple as hell really.
    Code:
    int main()
    {
        string one, two;
        cout << "Enter string1: ";
        getline(cin, one);
        cout << "Enter string2: ";
        getline(cin, two);
        cout << '\n' << endl;
        if ( two.length() < one.length() && one.substr(0, two.length()).compare(two) == 0 )
        {
            cout << "String 1 starts with string 2." << endl;
        }
        else
        {
            cout << "String 1 does not start with string 2." << endl;
        }
        if ( one.length() < two.length() && two.substr(0, one.length()).compare(one) == 0 )
        {
            cout << "String 2 starts with string 1." << endl;
        }
        else
        {
            cout << "String 2 does not start with string 1." << endl;
        }
    }
    [/EDIT]
    Last edited by Raigne; 06-22-2007 at 11:59 PM.

  7. #22
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Quote Originally Posted by Raigne View Post
    CodeMonkey that is correct to a certain extent, but it does not correctly answer the entire question. The question as if string one starts with string two. Although that does give a relatively decent answer.
    Since the question has been answered many times over, the point is moot. However, I do not see how my method does not entirely answer the question.

    s1.find(std::string& str) returns the index of the first occurrence of str within the invoking string (s1). If s1 does not contain str, then npos is returned. Thus, if the function returns the value 0, then s1 contains str, and it is at the beginning of s1.

    Correct me if I misunderstand the function. I'm using Schildt as a quick reference... I know, I know....
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The method is correct. It's just inefficient for long strings, because it keeps searching even after it's clear that s2 doesn't start s1.
    All methods involving substrings are also inefficient, because I know of no implementation of std::string that doesn't allocate a new string for substrings. (Although with small buffer optimization, this doesn't mean much as long as the substring is short.)

    The most efficient way is indeed using std::equal.
    Code:
    #include <algorithm>
    #include <string>
    
    bool begins(const std::string s1, const std::string s2)
    {
      return s1.length() < s2.length() &&
        std::equals(s1.begin(), s1.end(), s2.begin(), s2.begin()+s1.length());
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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