string length in do while

//my first programming project
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string thanos, ultron;


do
{
cout << "please enter the first string: ";
cin >> thanos;
cout << "please enter the second string: ";
cin >> ultron;
}
while (thanos > ultron);

int i=thanos.length();
int j=ultron.length();
int position=0;
int characters=0;
bool found;
for(int k=0; k<thanos.length(); k++)
{
for(int l=0; l<ultron.length(); l++)
{
if(ultron[k]==thanos[l])
{
characters=ultron.length()-k;
position=k+1;
found=true;
}
else
{
found=false;
}
} }
if(true)
{
cout<<"second string overlaps with first string";
cout<<" starting at first string position "<<position
<<" for "<<characters<<" characters of second string."<<endl;
}
else
{
cout<<"second string does not overlap with first"<<endl;
}
return 0;
}

I need to change this code so that the length of both strings are computed without using built in library functions such as stringlength()...also I don't know how to change it so that the user has to re-enter both strings if the first string is shorter than the second string...in other words the loop will end until the user enters the first string while it is greater than or equal to the length of the second string - im guessing a do while or just while loop
Alright man.
I don't know much.
But I know enough for this.
I think.
Hopefully.
Alright first things first, use the source code formatting "<>"for your code. It highlights and indents and fixes spacing so it's easier to read the code.
Like so:
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
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string thanos, ultron;


do
{
cout << "please enter the first string: ";
cin >> thanos;
cout << "please enter the second string: ";
cin >> ultron;
}
while (thanos > ultron);

int i=thanos.length();
int j=ultron.length();
int position=0;
int characters=0;
bool found;
for(int k=0; k<thanos.length(); k++)
{
for(int l=0; l<ultron.length(); l++)
{
if(ultron[k]==thanos[l])
{
characters=ultron.length()-k;
position=k+1;
found=true;
}
else
{
found=false;
}
} }
if(true)
{
cout<<"second string overlaps with first string";
cout<<" starting at first string position "<<position
<<" for "<<characters<<" characters of second string."<<endl;
}
else
{
cout<<"second string does not overlap with first"<<endl;
}
return 0;
}


Alright so the way you have your first while loop set up.
It says that while thanos is larger than ultron then cout those statements.
So it's just going to keep repeating those cout statements if thanos is larger and it's not going to do anything else.
Example:
please enter the first string: 20
please enter the second string: 10
please enter the first string: 20
please enter the second string: 10
please enter the first string: 20
please enter the second string: 10
please enter the first string: 20
please enter the second string: 10
please enter the first string: 20
please enter the second string: 10
please enter the first string:


So fix that.
Then let's talk about counting characters.
A string is a combination of char data types.
Char data types can hold any value but only 1 value per declaration.

Each character in a string can be accessed individually by its position (subscript or index) in the string

first character is ALWAYS at position 0
last character is at position (length-1)

string word = "apple";
cout << word[0]; // prints a

Subscript |0| 1| 2| 3| 4|

Content |a| p| p| L| e |

Does that make sense?
So if you have user input word and don't know how long it is, you still know 2 things. You know that there is the first position which is word[0] and you know that you want to count up from that position until blank space is reached or you are at the end of line.

For this particular program the user will hit "enter" after he types in a word.
In C++ '\0' is the null termination character. It marks the end of the string. Without it, the computer has no way to know how long that group of characters goes.

So create a loop that will count the characters.
Starting at position 0, and stopping until '\0'.

Struggle with it for a bit, if you can't figure it out, I'll post the answer.

As far as that other shit, imma go take a nap now.
thanks man...im gonna try and post a new and improved code asap
//my first programming project
#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 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);

int position=0;
int characters=0;
bool found;
for(int k=0; k<thanos[k]; k++)
{
for(int l=0; l<ultron[l]; l++)
{
if(ultron[k]==thanos[l])
{
characters=ultron[l]-k;
position=k+1;
found=true;
}
else
{
found=false;
}
} }
if(true)
{
cout<<"second string overlaps with first string";
cout<<" starting at first string position "<<position
<<" for "<<characters<<" characters of second string."<<endl;
}
else
{
cout<<"second string does not overlap with first"<<endl;
}
return 0;
}

ok this is the best I could do...I listened to you and initialized the position of both strings and added the null terminator for my length counter, but the last part (the substring part) is really tricky
You were asked once already to use code tags, to make your code more readable:

http://www.cplusplus.com/articles/z13hAqkS/

If you want people to give up their time and effort to help you, then making your code readable is going to make that more likely.

Last edited on
Syntax:
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
//my first programming project
 #include <iostream>
 #include <string>
 using namespace std;

 int main ()
 {
 string thanos, ultron;


 do
 {
 cout << "please enter the first string: ";
 cin >> thanos;
 cout << "please enter the second string: ";
 cin >> ultron;
 }
 while (thanos > ultron);

 int i=thanos.length();
 int j=ultron.length();
 int position=0;
 int characters=0;
 bool found;
 for(int k=0; k<thanos.length(); k++)
 {
 for(int l=0; l<ultron.length(); l++)
 {
 if(ultron[k]==thanos[l])
 {
 characters=ultron.length()-k;
 position=k+1;
 found=true;
 }
 else
 {
 found=false;
 }
 } }
 if(true)
 {
 cout<<"second string overlaps with first string";
 cout<<" starting at first string position "<<position
 <<" for "<<characters<<" characters of second string."<<endl;
 }
 else
 {
 cout<<"second string does not overlap with first"<<endl;
 }
 return 0;
 }


wow thanks for that link...I honestly didn't know how to post it with coloring
Last edited on
What're are you trying to substitute?
yeah that's my problem...im trying to see if the second string can be found in first string and if it happens it would output the position it was found in the first string
"can be found in"

Lets take words 'lolollo' and 'oll'. There are 5 potential positions (1 + 7 - 3).

  lolollo
0 oll
1  oll
2   oll
3    oll
4     oll

Pos 0 is obviously wrong, for o!=l. Same for 2 and 4.

Pos 1 starts promisingly: thanos[1]==ultron[0] and thanos[2]==ultron[1], but thanos[3]!=ultron[2].

Pos 3 matches all three pairs: 3-0, 4-1, 5-2.

Make a separate function bool foo( const string & tha, const string & ult, int pos );, that returns true if ult is in tha starting from character pos of tha.
thanks man...I appreciate your help, gonna do this asap
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
//my first programming project
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string s1, s2;
 int i, j, k, x, y, pos;
 i = 0;
 j = 0;
 k = 0;

 do
 {
 x = 0;
 y = 0;
 cout << "please enter the first string: ";
 cin >> s1;
 for (i = 0; s1 [i] != '\0'; i++)
 x++;
 cout << "the length of the first string is: " << x << endl;
 cout << "please enter the second string: ";
 cin >> s2;
 for (i = 0; s2 [i] != '\0'; i++)
 y++;
 cout << "the length of the second string is: " << y << endl;
 ; }
 while (y > x);

 bool z;
 for (j = 0; j < s1 [j]; j++)
 for (k = 0; k < s2 [k]; k++)
 if (s1 [j] == s2 [k])
    {
    z = true;
    }
 else
 {
     z = false;
 }

 if (true)
 cout << "substring was found at position: " << s1 [y] << endl; //not sure what the correct coding should be here
 else
    cout << "substring was not found" << endl;

    return 0;
}


I cannot figure out how to do what you asked...it is so stressful because I cannot use any built in library functions so I cant even write s1.length()...I will go back and continue working out my code...any more advice?
Last edited on
Topic archived. No new replies allowed.