Sunday, September 6, 2015

AS-Level Related Computer Science Programming .( Visual Basic )

Programming Questions with solutions:



1.       Gina is developing her programming skills in string handling.

                 She is going to input one string. The string contains letters and zero or more " * " characters in any positions. Gina wants to remove all the " * "s and output the letters in their original order. For example:
                       ●    Input  "com*put**er*" , the output is "computer"
                       ●   Input "hardware", the output is "hardware"
(a)        Using a high-level programming language, write the code to perform this task. 
    ( Ensure that you use meaningful variable names and lay the code out clearly )


Module module1

    Sub main()
        Dim counter As Integer
        Dim inputString, temp, newString As String
        newString = ""

        Console.WriteLine("Enter any word:")
        inputString = Console.ReadLine()

        For counter = 1 To Len(inputString)
            temp = Mid(inputString, counter, 1)
            If temp <> "*" Then newString = newString + temp
        Next

        Console.WriteLine(newString)
        Console.ReadLine()
    End Sub

End Module





2.       John is developing his programming skills in string handling.

                 He is going to input two string.
           Each string is made up of three parts:
                       ●    Letters following by
                       ●    a single " * " character, followed by
                       ●     Letters

The groups of letters after the " * " characters are joined together to form a new string which is then output.

 For example, with " DFG*COM" and "B*PUTER" as inputs, the new string output will be
"COMPUTER".
(a) Using a high-level programming language, write the code to perform this task. ( Ensure that you use meaningful variable names and lay the code out clearly.)




Module module1
    Sub main()
        Dim counter As Integer
        Dim inputString1, inputString2, temp, newString, finalWord As String
        Dim newString1, newString2 As String
        Console.WriteLine("Enter any word:")
        inputString1 = Console.ReadLine()

        Console.WriteLine("Again enter any word:")
        inputString2 = Console.ReadLine()

        For counter = 1 To Len(inputString1)
            temp = Mid(inputString1, counter, 1)
            If temp = "*" Then newString1 = Mid(inputString1, counter + 1, Len(inputString1) - counter)
        Next

        For counter = 1 To Len(inputString2)
            temp = Mid(inputString2, counter, 1)
            If temp = "*" Then newString2 = Mid(inputString2, counter + 1, Len(inputString2) - counter)
        Next

        newString = newString1 + newString2
        For counter = 1 To Len(newString)
            temp = Mid(newString, counter, 1)
            If temp <> "*" Then finalWord = finalWord + temp
        Next
        Console.WriteLine(finalWord)
        Console.ReadLine()
    End Sub


End Module


Instagram

No comments:

Post a Comment

Comments here....