function: is string a substring - Help Please

Question:: function to check whether string s1 is a substring s2. The function returns the first index in s2 if there is a match. Otherwise returns -1.

Uses int indexOf(const string& s1, const string& s2)

My errors say:
5 q2\assign 5 q2.cpp(53): error C2664: 'int indexOf(const std::string &,const std::string &)': cannot convert argument 1 from 'const std::string [80]' to 'const std::string &'
1> c:\users\kim digney\desktop\cs assignments\aasignment 5\assignment 5 q2\assign 5 q2.cpp(53): note: Reason: cannot convert from 'const std::string [80]' to 'const std::string'
1> c:\users\kim digney\desktop\cs assignments\aasignment 5\assignment 5 q2\assign 5 q2.cpp(53): note: No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Help is appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  

#include <iostream>
#include <cstring>
using namespace std;

// Checking if a string is a substring
int indexOf(const string& s1, const string& s2)
{
	int lengthLeft = stringLength(s2);
	int startingIndex = 0;


toWhile: //I found this in the textbook: Note toWhile is a label. You can use break with a label attached.
	while (stringLength(s1) <= lengthLeft)
	{
		
		bool same = true;
		for (int i = 0; i < stringLength(s1); i++)
		{
			if (s1[i] != s2[startingIndex + i])
			{
				startingIndex++;
				lengthLeft--;
				same = false;
			}
		}

		if (same) return startingIndex;
	}

	return -1;
}

int main()
{
	// Prompt the user to enter a string
	cout << "Enter the first string: ";
	const std::string s1[80];
	cin.getline(s1, 80);

	// Prompt the user to enter a string
	cout << "Enter the second string: ";
	const std::string s2[80];
	cin.getline(s2, 80);

	cout << "indexOf(\"" << s1 << "\", \"" <<
		s2 << "\") is " << indexOf(s1, s2) << endl;

	return 0;
}



Last edited on
 
int lengthLeft = stringLength(s2);

There is no standard function named stringLength in C++. To get the length of a string you can use the length() or size() member functions. http://www.cplusplus.com/reference/string/string/length/


 
const std::string s1[80];

This defines an array of 80 empty strings that you can't modify. I think you only want the middle part. std::string s1;


 
cin.getline(s1, 80);

The getline member function only works with char arrays (C strings). When using std::string you need to use std::getline instead. http://www.cplusplus.com/reference/string/string/getline/
Last edited on
I thought I fixed line 8...

int lengthLeft = strlen(s2);
...But it still underlines line 8 (s2) line 13 (s1) and line 17 (s1)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstring>
using namespace std;

// Checking if a string is a substring
int indexOf(const string& s1, const string& s2)
{
	int lengthLeft = strlen(s2);
	int startingIndex = 0;


toWhile: //I found this in the textbook: toWhile is a label. You can use break with a label attached.
	while (strlen(s1) <= lengthLeft)
	{
		
		bool same = true;
		for (int i = 0; i < strlen(s1); i++)
		{
			if (s1[i] != s2[startingIndex + i])
			{
				startingIndex++;
				lengthLeft--;
				same = false;
			}
		}

		if (same) return startingIndex;
	}

	return -1;
}

int main()
{
	// Prompt the user to enter a string
	cout << "Enter the first string: ";
	std::string s1;
	cin.getline(s1, 80);

	// Prompt the user to enter a string
	cout << "Enter the second string: ";
	const std::string s2;
	std::getline(std::cin,s2);

	cout << "indexOf(\"" << s1 << "\", \"" <<
		s2 << "\") is " << indexOf(s1, s2) << endl;

	return 0;
}


I am still unsure of line 37-38
1
2
const std::string s2;
	std::getline(std::cin,s2);


after reading about the std::getline....etc

My errors now say:
error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const std::string' to 'const char *'

: note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

: cannot convert argument 1 from 'const std::string' to 'const char *'

note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

: error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const std::string' to 'const char *'

: note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

:error C2664: 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::getline(_Elem *,std::streamsize,_Elem)': cannot convert argument 1 from 'std::string' to 'char *'

: error C2039: 'getline': is not a member of 'std'

: error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

And here I thought I was getting close to solving this...


Last edited on
Please do yourself a huge favor and include <string>, not <cstring>. Work with C++ strings.

strlen is a C function. Don't use strlen with std::strings.
As Peter87 was saying, use the "length" or "size" member functions to get the number of characters in a string:

1
2
std::string my_string = "hello world";
int size = my_string.size();//size is 11 
Topic archived. No new replies allowed.