Challenge of the Month - November 2020

Nonki Takahashi 676 Reputation points
2020-11-09T13:33:45.147+00:00

These challenges are intended for people who are learning to program for the first time or for those returning to programming who want to start using Small Basic. Some will be easy, some will be hard - but they will all make you think, and more importantly be GREAT FUN!

Please post your solutions / partial solutions / questions / feedback etc. into this thread that will remain 'sticky' for the month. The only rule is that your solution must use standard Small Basic methods (no extensions).

It would be good if people could post their problems with these challenges so that a discussion can start so that everyone can learn from each other.

We may extend these challenges over into a second month if solutions and questions are still coming in.

Text Challenge

Write a program that will reverse each individual word in a sentence but leave the order of words unchanged.

Example

INPUT: Hasta la vista
OUTPUT: atsaH al atsiv

File and Text Challenge

  1. Write a program to read an entire text file into a Small Basic variable. You need to create a sample text file sample.txt somewhere first.
  2. Write a program to write out the data in a variable to a text file.
  3. Combine the 2 programs above into one program with 2 subroutines, one each to read and write to a file. Add some user input to ask for the input and output file names (inside the subroutines) so that a file can be read in and then written out to a different file.
  4. Print out each character and its the Unicode value to the textwindow for each character in the text file. Hint: look at Text.GetLength, Text.GetSubText and Text.GetCharacterCode. You need to check each character in a loop.
  5. Count the number of lines in the text file. Hint: the unicode (ascii) codes for a 'line feed' is 10 and a 'carriage return' is 13. These occur at the end of each line of text.
  6. Modify the program to remove the Unicode character output, but ask the user for some sub-text to change and replace to something else. Get the program to find all occurrences of the sub-text and replace it with the replacement text and write the modified file out.

Get the program to find all occurrences of the sub-text and replace it with the replacement text and write the modified file out.

Small BASIC
Small BASIC
A programming language created by Microsoft that serves a stepping stone for beginners from block-based coding languages to more complex text-based languages.
277 questions
0 comments No comments
{count} votes

Accepted answer
  1. Nonki Takahashi 676 Reputation points
    2020-12-24T15:57:57.057+00:00

    This is my solution for Text Challenge:

    delim = " "
    While "True"
        TextWindow.Write("INPUT: ")
        line = TextWindow.Read()
        Split()
        Program.Pause()
        For iWord = 1 To nWord
            txt = word[iWord]
            Reverse()
            Program.Pause()
            If iWord = 1 Then
                TextWindow.Write("OUTPUT: ")
            Else
                TextWindow.Write(delim)
            EndIf
            TextWindow.Write(txt)
        EndFor
        TextWindow.WriteLine("")
    EndWhile
    
    Sub Reverse
        ' param txt - text to reverse
        ' return txt - text reversed
        _txt = txt
        txt = ""
        len = Text.GetLength(_txt)
        For i = 1 To len
            txt = Text.Append(Text.GetSubText(_txt, i, 1), txt)
        EndFor
    EndSub
    
    Sub Split
        ' param delim - split with
        ' param line - to split
        ' return word - word array
        ' return nWord - number of words
        word = ""
        nWord = 0
        p = 1
        len = Text.GetLength(line)
        While p <= len
            d = Text.GetIndexOf(Text.GetSubTextToEnd(line, p), delim)
            nWord = nWord + 1
            If d = 0 Then
                word[nWord] = Text.GetSubTextToEnd(line, p)
                p = len + 1
            Else
                word[nWord] = Text.GetSubText(line, p, d - 1)
                p = p + d
            EndIf
        EndWhile
    EndSub
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Nonki Takahashi 676 Reputation points
    2020-11-09T13:45:29.677+00:00

    Hi Small Basic challengers, this is the first Challenge of the Month in Small Basic on Q&A. But the challenge problems are from old Challenges in Small Basic Forum.

    Text Challenge is from Small Basic Challenge of the Week 6, 7 and 8.
    File and Text Challenge is from Challenge of the Month - January 2012.

    Let's challenge again. Thanks.

    0 comments No comments

  2. Allen G 1 Reputation point
    2021-01-31T20:24:04.76+00:00

    Here's my solution to the Text Challenge:

    TextWindow.Show()
    TextWindow.Clear()
    'TextWindow.BackgroundColor = "blue"
    TextWindow.Title = "Text Challenge Nov, 2020"
    'TextWindow.ForegroundColor = "white"

    userin = ""
    revText = ""
    playAgain = "Y"

    While playAgain <> "n" And playAgain <> "no"
    game()
    TextWindow.WriteLine("Play again?")
    playAgain = TextWindow.Read()
    playAgain = Text.ConvertToLowerCase(playAgain)
    endwhile

    Sub game
    TextWindow.WriteLine( "Please enter some text!" )
    userin = TextWindow.Read()
    TextWindow.WriteLine( "You typed: " + userin )
    reverseSub()
    TextWindow.WriteLine( "The reversed text is: " + revText )
    EndSub

    Sub reverseSub
    length = Text.GetLength(userin)
    TextWindow.WriteLine( "The length of the text is: " + length )

    indLetters[length] = ""
    For i = 1 To length
    indLetters[i] = Text.GetSubText(userin, i, 1)
    EndFor

    TextWindow.WriteLine( "The individual letters are : ")
    For i = 1 To length
    TextWindow.Write( " " + indLetters[i] + " " )
    EndFor

    TextWindow.WriteLine( revText )

    For i = length To 1 Step -1
    'TextWindow.Write( " " + indLetters[i])
    revText = Text.Append(revText, indLetters[i])
    EndFor
    EndSub

    0 comments No comments

  3. Allen G 1 Reputation point
    2021-02-04T03:24:23.967+00:00

    I also tried this in c++:

    /* Nov. 2020 smallbasic forum challenge:
    * Write a program that reverses a string.
    */

    include <iostream>

    include <string>

    using namespace std;

    int main() {

    string userIn;
    string reversedOut = "";
    int len = 0;
    
    cout << "Enter some Text: \n";
    getline(cin, userIn);
    cout << "You wrote: " + userIn << "\n";
    len = userIn.length();
    cout << "You entry is " << len << " bytes long. \n";
    // try out the pop back command.
    //userIn.pop_back();
    //cout << "After pop back command: " << userIn << "\n";
    //userIn.append()
    //cout << userIn;
    for (int i = len; i >= 0; i--) {
        string x;
        x = userIn[i];
        reversedOut.append(x);
    };
    cout << reversedOut << "\n";
    
    /*string userin;
    getline(cin, userin);
    cout << userin << "\n";
    cout << userin[0] << "\n";
    */
    return 0;
    

    }

    0 comments No comments