Thread: printing a portion of elements within an array

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    4

    printing a portion of elements within an array

    I have a small uni task where I have to switch lower case characters to uppercase characters from a user input within an array, up to a max of 20 characters. if the user inputs a number of characters that is less than the maximum specified, i was wondering how you would go about printing these without printing the garbage data from the array. the only way I can think of doing this is by having the user specify how many they are going to enter, and looping over that number of indexes within the array. im fairly new to c programming and would appreciate any and all help

  2. #2
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    81
    Is the input separated by spaces or combined into an array of characters, such as amessagethatlookslikethis?

    If the former, you could split the input into tokens, separated by spaces, then loop and capitalize.

    If the latter, you can loop through the input until you hit the null terminator '\0', capitalizing on the way.

    This is off the top of my head. An example of expected output could narrow down a possible solution.
    Last edited by catacombs; 05-20-2019 at 12:58 AM.

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    4
    Quote Originally Posted by catacombs View Post
    Is the input separated by spaces or combined into an array of characters, such as amessagethatlookslikethis?

    If the former, you could split the input into tokens, separated by spaces, then loop and capitalize.

    If the latter, you can loop through the input until you hit the null terminator '\0', capitalizing on the way.

    This is off the top of my head. An example of expected output could narrow down a possible solution.
    its the latter, would the user have to input the null terminator?

  4. #4
    Registered User
    Join Date
    May 2019
    Posts
    4
    an example would be:
    input = abcdef
    output = ABCDEF

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    4
    I figured it out, thanks for all the help

  6. #6
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    81
    Quote Originally Posted by LiquidAlchemy View Post
    its the latter, would the user have to input the null terminator?
    No. Users don't input the null terminator. And, apologies, when I said last night you could search for the null terminator, I meant to say they could look for '\n', which is the new line characters. That's what would appear on user input.

    Quote Originally Posted by LiquidAlchemy View Post
    I figured it out, thanks for all the help
    Cool! What was your solution?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should be aware of the notion of a string as a representation of text, in which a string is a contiguous sequence of characters terminated by a null character. A string must be stored in an array of characters that is at least large enough to hold all the characters including the null character, but could be larger.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing unique array elements
    By sean_cantab in forum C Programming
    Replies: 22
    Last Post: 04-12-2016, 06:12 PM
  2. Help printing elements of array
    By Joshua Green in forum C Programming
    Replies: 8
    Last Post: 10-23-2015, 01:29 AM
  3. Help with printing elements of an array
    By shaddock in forum C Programming
    Replies: 12
    Last Post: 06-15-2015, 06:38 PM
  4. Please help with Printing duplicate array elements
    By TheSprawl in forum C Programming
    Replies: 9
    Last Post: 11-23-2011, 11:22 PM
  5. Printing out Array Elements
    By BB89 in forum C++ Programming
    Replies: 6
    Last Post: 03-24-2010, 02:00 PM

Tags for this Thread