Thread: Big Test! Need Help!

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    83

    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.

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    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.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    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.
    Last edited by walkonwater; 04-14-2009 at 09:09 PM.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    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";

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Quote Originally Posted by walkonwater View Post
    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.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    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

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Quote Originally Posted by dnguyen1022 View Post
    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;
    }

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    You want to start at the "integer position just read". Not 0.

    And it says print the substring only if it exists.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sustring test puzzle
    By WDT in forum C# Programming
    Replies: 3
    Last Post: 06-29-2009, 07:19 AM
  2. Help needed to verify a new on-line C test
    By JanHruska in forum Projects and Job Recruitment
    Replies: 15
    Last Post: 06-20-2009, 06:48 AM
  3. How Big is Too Big
    By kpreston in forum C Programming
    Replies: 4
    Last Post: 10-25-2008, 10:16 AM
  4. Big and little endian
    By Cactus_Hugger in forum C Programming
    Replies: 4
    Last Post: 10-12-2005, 07:07 PM
  5. Looking for some big C/C++ projects
    By C-Dumbie in forum C Programming
    Replies: 5
    Last Post: 09-16-2002, 12:18 AM