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.