Display all characters that lie between two given indices

This is a discussion on Display all characters that lie between two given indices within the C++ Programming forums, part of the General Programming Boards category; Hello, everyone. I consider myself new to C++ and I have tried to google for it and all it come ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Display all characters that lie between two given indices

    Hello, everyone.

    I consider myself new to C++ and I have tried to google for it and all it come up with is for string which I think my teacher want me to use c-string. Also, I have look in the textbook and I can't find any similar exercise. This is what I came up with
    Code:
    char str1[200]= {0};
        for (int n = 0 ; n < begin_display; n++)
        {
            str1[n] = str[n];
        }
        return str1[200];
    and here is example:

    enter a string: My brother Joseph is married.
    Please enter the beginning index: 5
    Please enter the ending index: 12
    other Jo



    Thank you for your help. I'm greatly appreciate it
    Last edited by witt; 09-22-2011 at 10:46 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    If you only intend to print, then you don't need to copy. You can adapt your loop to print character by character, starting from the beginning index and stopping at the ending index.

    Be careful of the possibility that the indices given might be out of range.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Quote Originally Posted by laserlight View Post
    If you only intend to print, then you don't need to copy. You can adapt your loop to print character by character, starting from the beginning index and stopping at the ending index.

    Be careful of the possibility that the indices given might be out of range.
    I have think of that ,but I couldn't make it work. The output is some random characters and it also print characters that outside my two given indices too. For example, My br%%%%%%%seph is married.
    Last edited by witt; 09-22-2011 at 11:11 PM.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by witt View Post
    I have think of that ,but I couldn't make it work. The output is some random characters and it also print characters that outside my two given indices too. For example, My br%%%%%%%seph is married.
    We are not mind readers, you are going to have to post what you tried. In the mean time read through Lesson 3: Loops and Lesson 9: C strings.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    I got the first part but i tried everything on the second part. nothing work. please help!!
    Code:
      else if (choice == 3)
        {
            cin.ignore();
            cout<<"How many characters from the beginning of the string do you want to display? ";
            cin>>begin_display;
    
            if (strlen(str) < begin_display)
                printf("Error: too many characters.\n");
            else
            {
                choicethree(str, begin_display);
                printf("%s\n",str);
            }
        }
      else if (choice == 4)
        {
            cin.ignore();
            cout<<"How many characters from the end of the string do you want to display? ";
            cin>>end_display;
    
            if (strlen(str) < end_display)
                printf("Error: too many characters.\n");
            else
            {
                choicefour(str, end_display);
                printf("%s\n",str);
            }
        }
    void choicethree(char str[], int begin_display)
    {
        
        for (int n = begin_display ; n < strlen(str); n++)
        {
            str[n] = '\0';
        }
        
    }
    void choicefour(char str[], int end_display)
    {
        int left;
        left = strlen(str) - end_display;
    
    
        for (int i =  0; i < (strlen(str) - end_display) ; i++)
        {
            str[i] = '\0';
        }
        
    }

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    No, you do not seem to be getting this. You need to:
    1. Ask for string input.
    2. store in char array, see the getline method
    3. Ask for start and stop values and bounds check them
    4. Iterate through your char array, printing each char individually, from start value to stop value

    Attempt these steps and then post the code if you still have problems.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Also, get rid of printf. There is absolutely no reason for it to be there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. XNA 3d - 32 bit indices
    By musicman in forum Game Programming
    Replies: 1
    Last Post: 09-06-2011, 10:15 PM
  2. How to display extended ASCII characters in LINUX
    By mocatz187 in forum C Programming
    Replies: 6
    Last Post: 01-22-2010, 11:34 PM
  3. Inconsistent display of tab characters in edit control
    By New++ in forum Windows Programming
    Replies: 0
    Last Post: 01-06-2006, 07:34 AM
  4. Array of indices
    By ronenk in forum C Programming
    Replies: 4
    Last Post: 06-15-2004, 05:36 PM
  5. What does 'indices' mean?
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2002, 06:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21