#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0

    Check to see if string is a substring of another.


    I'm new to this language and i need a little help. The question asked is to use this function (int indexOf(const string& s1, const string& s2)) to check whether string s1 is a substring of s2. If its a match return the 1st index in s2 and if not return -1. Then it says to right a program that reads the strings.

    c++ Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
     
    using namespace std;
     
    int indexOf(const string& s1, const string& s2)
    {
        if (s2.find(s1))
        {
        return s2.length();
    }
     
        else
        {
            return -1;
        }
    }
     
     
     
    int main()
    {
     
    cout << "Enter the first string: ";
    string s1[10];
    getline(cin, s1[10]);
     
    cout << "Enter the second string: ";
    string s2[10];
    getline(cin, s2[10]);
     
    int answer = indexOf(s1[10], s2[10]);
     
    cout << answer << endl;
     
     
     
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Last edited by requinix; May 8th, 2015 at 07:45 PM. Reason: fixing code tags
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,785
    Rep Power
    4300
    Egad, you have a lot of serious bugs in your code. Let me point out a few of them.
    Code:
    string s1[10];
    This does not declare a string that is 10 characters long. Instead, it declares an array of 10 string objects. What you need is to declare it as:
    Code:
    string s1;
    You made the same mistake in your declaration of s2 as well.

    Next, you have this:
    Code:
    getline(cin, s1[10]);
    It is good that you learned to use getline and to use it with a string object. The problem is that you declared string s1 as an array of 10 strings and C++ arrays are 0-based (i.e.) if you declare string s1[10], the valid indexes are s1[0], s1[1]....s1[9]. Therefore, when you do getline(cin, s1[10]);, you're asking getline() to read your input to an invalid memory location that belongs to something else. This can cause your program to crash or produce unexpected behavior. At best, you should have done getline(cin, s1[9]). Similarly, you're doing the same mistake when you getline() on s2 and when you call indexOf.

    Thirdly, the find() method of a string object already returns the index position of a substring if it finds it, or string::npos, if it can't find it.

    Fourthly, just a minor style point. Instead of arbitrarily declaring:
    Code:
    using namespace std;
    you should leave out that line and explicitly call out the objects fully (e.g. std::string, std::cout etc.), otherwise, you defeat the entire purpose of namespaces. I know that several C++ tutorials talk about putting using namespace std; at the top of the code, but that is bad practise IMHO.

    With all that said, let's rewrite your code to work.
    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    long indexOf(const std::string& s1, const std::string& s2)
    {
      std::size_t pos = s2.find(s1);
      if (pos != std::string::npos) {
        return pos;
      }
      else {
        return -1;
      }
    }
    
    int main(void)
    {
      std::cout << "Enter the first string: ";
      std::string s1;
      std::getline(std::cin, s1);
    
      std::cout << "Enter the second string: ";
      std::string s2;
      std::getline(std::cin, s2);
    
      long answer = indexOf(s1, s2);
      std::cout << answer << std::endl;
    
      std::system("PAUSE");
    
      return EXIT_SUCCESS;
    }

    Comments on this post

    • markroberts agrees
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo