# Big Test! Need Help!

• 04-14-2009
walkonwater
Big Test! Need Help!
Okay, I have a test tomorrow basically over strings. I am very new and in the process of learning. So our teacher gave us a pre-test and gave us all the answers if we asked for them. But I didn't and no one else asked about this one and I didn't read it but I just did and am confused on what it even means. I know it says that this place isn't something for homework but it isn't homework. I just would like to know what it means so I can do well on the test tomorrow. Now we have to write this all on paper tomorrow but I don't really understand what this is?

It says: Write a sequence of C statements that will read an integer value (position) and a string from a single line, decide if there is a valid substring of length 5 starting at the integer position just read, and print the substring if it exists.
• 04-14-2009
dnguyen1022
from the way I understand it, it looks like you give it a number and a string. That number will determine what letter you start at from the string. From where you start at, determine if there are 4 characters after that. 4 because it starts from the position you give it..1 for the one you give it + 4 after that = 5...

Well I am unsure of is whether it has to be exactly a length of 5 or can be greater than.

But here is an example...

Code:

```Integer: 3 String: 1234567```
So you gave it the number 3, it would read "34567" That is exactly a length of 5, so it would output it.

A failing case...
Code:

```Integer: 5 String: bigdog```
It would start at o, and see that there is only 1 letter after that, so it is not valid. Therefore it won't output it.

When writing this you may have to consider what happens if you give it an integer that is longer than the string itself though.

Then again I could be wrong...you may want to confirm this with someone else here.
• 04-14-2009
walkonwater
I would think it would be something like this but not sure.

Code:

```string str; string pos; pos = str.substr[5];```
This is the only thing I could come up with.
• 04-14-2009
walkonwater
So could I make the str = to anything I wanted it to be? For example, could it be

Code:

`string str = "This is the string";`
• 04-14-2009
dnguyen1022
Quote:

Originally Posted by walkonwater
So could I make the str = to anything I wanted it to be? For example, could it be

Code:

`string str = "This is the string";`

Yes, or you can ask the user for the string rather than within the body of your code.
• 04-14-2009
walkonwater
So would this be the answer?

Code:

```string str = "This is the string"; string j; j = str.substr(0,5); cout << j;```
and it would print out:

Code:

`This`
• 04-14-2009
walkonwater
Quote:

Originally Posted by dnguyen1022
Yes, or you can ask the user for the string rather than within the body of your code.

So would this be my final product? Does this look like it answers the question?

Code:

``` #include <iostream> int main() {   string str;   string j;     cin >> str;   j = str.substr(0,5);   cout << j;   return 0; }```
• 04-14-2009
cyberfish
You want to start at the "integer position just read". Not 0.

And it says print the substring only if it exists.
• 04-15-2009
hk_mp5kpdw
1. Prompt the user for the one-line int/string values.
2. Read the above two values into an int and a string variable.
3. Set a second string variable equal to the substr beginning at the int position read in from the user and of length 5.
4. Determine if this second string is of length 5 or not and print accordingly.

Quote:

Originally Posted by walkonwater
Write a sequence of C statements

Are you sure you mean C and not C++. The above assumes C++ and not C. The C answer will of course be different.