how do i use do while loop to find the length of two strings?

hey guys, im having problems with a piece of code im trying to create using do while loop, here are the conditions:

Your program receives from user two strings of symbols: first long one and then short one. Then your program returns all positions in long string where short string occurs as a substring.
For example: long string is "aabcccbabc" and short one is "abc". Short string can be seen twice in the long string "aabcccbabc" in positions 1 and 7 (enumeration of positions in string starts as always from 0). Position of the substring is the position of its first symbol in the whole string.
Requirements:
1) Your program invites user to enter long string.
2) Program gets the long string from user.
3) Your program invites user to enter short string.
4) Program gets the short string from user.
5) If the length of the short string is larger than the length of the long string then repeat steps 1-5. In other words, force user to reenter both strings again and again until long string is finally longer or as long as the short string.
This step will require you to compute the lengths of both string. Your should do it yourself. You are NOT allowed to use build-in tools for finding string length (like strlen() )
6) Output the line "The occurances were found in the following positions: " and then from the new string you list all the positions separated by space. If nothing was found then instead of list of positions message "Required substring was not found".
7) If at least one occurance of short string was found in the long string output the total number of occurances in the following format on the separate line: "Total number of occurances is #number_of_occurances"

string istr;
int pos, len, I;
cin >> istr;
cin >> pos;
cin >> len;
for (I = 0; I < len; I++)
cout << ist [pos + I]

I know this piece of code gets the substring from the main string and computes the length of the substring, but im not sure how to expand this it so that it can satisfy the requirements, any suggestions?
One way to calculate the string length is like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int getSize(string input)
{
	int i = 0;
	int size = 0;
	char c = input[0];

	while (c != '\0')
	{
		size++;
		i++;
		c = input[i];
	}

	return size;
}
#include <iostream>
using namespace std;

int main ()
{
string shield, argus;
int count = 0, size = 0, i, j;

cout << "please enter first string: ";
cin >> shield;
for (i = 0; shield [i] != '\0'; i++)
count++;
cout << "first string shield is this long: " << count << endl;

cout << endl;

cout << "please enter second string: ";
cin >> argus;
for (j = 0; argus [j] != '\0'; j++);
size++;
cout << "second string argus is this long: " << size << endl;

cout << endl;

return 0;
}

thanks for the reply, I originally had this, but I never understood why the computer always computed the second string with a length of 1
for (j = 0; argus [j] != '\0'; j++);

You shouldn't have a ';' after for.
ok I see the mistake, thanks
#include <iostream>
using namespace std;

int main ()
{
string thanos, ultron;
int rogers = 0, stark = 0, i;
i = 0;

do
{
cout << "please enter first string: ";
cin >> thanos;
for (i = 0; thanos [i] != '\0'; i++)
rogers++;
cout << "first string is this long: " << rogers << endl;
cout << "please enter second string: ";
cin >> ultron;
for (i = 0; ultron [i] != '\0'; i++)
stark++;
cout << "second string is this long: " << stark << endl;
; }
while (thanos [i] >= ultron [i]);
return 0;
}

here is my new code, I was able to get the computer to compute the length of both strings and force the user to reenter both strings if the second string was longer than the first string...however, the loop creates an unwanted increase to the computed length of both strings and continues to increase for every loop
for example: user is forced to reenter strings and user enters first string "popcorn" and instead of length is 7, the length is 14 and the same goes for the second string...could it be because of an error in the while condition im using? also I noticed the loop never stops even if the first string was longer than second string
Last edited on
You have a ';' in a wrong place.
 
; }

variables rogers and stark keep increasing in every loop because you never reset them to zero.And finally you need to compare the size of the string so you need to use rogers instead of thanos[i] and stark insted of ultron[i].

The code should look like this :

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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string thanos, ultron;
	int rogers, stark, i;
	i = 0;

	do
	{
		rogers = 0;
		stark = 0;
		cout << "please enter first string: ";
		cin >> thanos;
		for (i = 0; thanos[i] != '\0'; i++)
			rogers++;
		cout << "first string is this long: " << rogers << endl;
		cout << "please enter second string: ";
		cin >> ultron;
		for (i = 0; ultron[i] != '\0'; i++)
			stark++;
		cout << "second string is this long: " << stark << endl;
	} while (stark >= rogers);
	return 0;
}
//my first programming project
#include <iostream>
using namespace std;

int main ()
{
string thanos, ultron;
int rogers, stark, logan, i;
logan = 0;
i = 0;

do
{
rogers = 0;
stark = 0;
cout << "please enter the first string: ";
cin >> thanos;
for (i = 0; thanos [i] != '\0'; i++)
rogers++;
cout << "the length of the first string is: " << rogers << endl;
cout << "please enter the second string: ";
cin >> ultron;
for (i = 0; ultron [i] != '\0'; i++)
stark++;
cout << "the length of the second string is: " << stark << endl;
; }
while (stark > rogers);
if (thanos [i] == ultron [i])
if (logan == 0) cout << "the position of the substring is: ";
cout << i << " ";
if (logan != 0);
else
cout << "the substring was not found";
logan++;
i++;
return 0;
}
thanks again, this is my new code with which I tried adding the functions for determining the position of the substring if the second string matches any part of the first string and if not the computer would instead output nothing was found. this piece of code has some errors I cannot figure out...for one the computer would always output the length of the second string a second time and then output nothing was found...any suggestions?
Topic archived. No new replies allowed.