Drop links or images here to add them to the editor.

Split a message into multiple parts.

★★☆

Messaging and social networking apps often require a message to be split into a number of smaller messages. For example, Twitter was once 140 characters per tweet, now called X, it is 280. SMS messages were typically 160 characters.

Tweet

Make

Write a program that asks the user to enter a message and the maximum number of characters allowed in a single message. The program then outputs a list of each part of the message up to the maximum number of characters for each part, formatted exactly like a Python list literal (single quotes by default, double quotes when the element contains a single quote).

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called tweets that:

  1. Takes two parameters, message, the message to be processed and num_chars, the maximum number of characters per single message.
  2. Returns a List<string> of messages from the original message with each new message containing a maximum of num_chars characters.

Complete the main program so that:

  1. The user can input the message.
  2. The user can input the maximum number of characters per message.
  3. The list of messages is output as a single Python-style literal line, e.g. ['Hello, how', ' are you t', 'oday?'].

Typical inputs and outputs from the program would be:

Enter the message: 
Hello
How many characters per message? :
10
['Hello']
Enter the message: 
Hello, how are you today?
How many characters per message? :
10
['Hello, how', ' are you t', 'oday?']
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// Tweet program

using System;
using System.Collections.Generic;

class Submission
{
---
    // -------------------------
    // Subprograms
    // -------------------------
    // Return message as a list of elements, each being num_chars long
---
    static List tweets(string message, int num_chars)
    {
---
        List tweet_list = new List();
        string tweet = "";
---
        // Add each character to a list
        for (int index = 0; index < message.Length; index++)
        {
---
            tweet += message[index];
---
            // Start a new tweet if num_chars reached
            if ((index + 1) % num_chars == 0)
            {
---
                tweet_list.Add(tweet);
                tweet = "";
---
            }
---
        }
---
        // Add the last characters to the list if not exactly divisible by num_chars
        if (tweet != "")
        {
---
            tweet_list.Add(tweet);
---
        }
---
        return tweet_list;
---
    }


---
    // -------------------------
    // Main program
    // -------------------------
    public static void Main(string[] args)
    {
---
        Console.WriteLine("Enter the message: ");
        string message = Console.ReadLine();
---
        Console.WriteLine("How many characters per message? :");
        int max_chars = Convert.ToInt32(Console.ReadLine());
---
        List tweet_elements = tweets(message, max_chars);
---
        foreach (string tweet in tweet_elements)
        {
---
            Console.WriteLine(tweet);
---
        }
---
    }
---
}
</pre>
</dw-parsons-puzzle>

</details>