Question

Lab Assignment # 2 - COMP 212 [Visual Studio - C#]

Lab Assignment # 2 - Programming 3 Exercise 02 (5 marks] Implement an extension method for built-in class String Builder to c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CountWords
{
class Program
{
static void Main(string[] args)
{
string str;
Console.Write("\nEnter a string: ");
str = Console.ReadLine();

StringBuilder sb = new StringBuilder(str);
IEnumerable<string> words = sb.ToString().Split(' ');
int count = 0;
foreach (var c in words)
{
count++;
}
Console.WriteLine("\nThe number of words contained in sb is "+count);
Console.ReadKey();

}
}
}

Output:

Enter a string: This is to test whether the extension method count can return a right answer or not The number of words conta

Add a comment
Know the answer?
Add Answer to:
Lab Assignment # 2 - COMP 212 [Visual Studio - C#] Lab Assignment # 2 -...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need this in Visual Studio C++ Write a function that count the number of vowels,...

    I need this in Visual Studio C++ Write a function that count the number of vowels, the number of consonants and the average number of letters in each word. The function accept a C-String (char array) or a string object as an argument and return the number of vowels, number of consonants and the average number of letters in each word. Problem: Requirements: . Use pointers as part of the solution Read a string from a text file. . Write...

  • This program should be run on Visual Studio. Please use printf and scanf as input and...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.11 Lab Exercise Ch.6a: Functions: String analyzer Create and debug this program in Visual Studio. Upload your Source.cpp file for testing (1) Prompt the user to enter a string of their choosing. Output the string. (1 pt) Ex: ics Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to...

  • C++, Visual Studio (Optional-Extra Credit) Write an AddressBook class that manages a collection of Person objects....

    C++, Visual Studio (Optional-Extra Credit) Write an AddressBook class that manages a collection of Person objects. Use the Person class developed in question Lab 4 Question 2. An AddressBook will allow a person to add, delete, or search for a Person object in the address book 1. The add method should add a person object to the address book .The delete method should remove the specified person object from the address book .The search method that searches the address book...

  • C++ / Visual Studio Implement the member function below that returns true if the given value...

    C++ / Visual Studio Implement the member function below that returns true if the given value is in the container, and false if not. Your code should use iterators so it works with any type of container and data. template< typename Value, class Container > bool member( const Value& val, const Container& cont ); just implement this function in the code below #include <iostream> #include <array> #include <deque> #include <list> #include <set> #include <string> #include <vector> using namespace std; #define...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Program 2:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Program 2: Design (pseudocode) and implement (source code) a class called Counter. It should have one private instance variable representing the value of the counter. It should have two instance methods: increment() which adds on to the counter value and getValue() which returns the current value. After creating the Counter class, create a program that simulates tossing a coin 100 times using two Counter objects (Head...

  • The Murach's Visual Basic 2015 Book And Visual Studio Exercise 7-2 Enhance the Invoice Total application...

    The Murach's Visual Basic 2015 Book And Visual Studio Exercise 7-2 Enhance the Invoice Total application If you did exercise 6-1 in the last chapter, this exercise asks you to add data validation and exception handling to it. 1. Copy the Invoice Total application from your C:\VB 2015\Chapter 06 directory to your Chapter 07 directory. This should be your solution to exercise 6-1 of the last chapter. 2. If your application has both a Sub procedure and a Function procedure...

  • C #sharp Visual Studio Programming, Implement the array declaration and initialization for -For this quiz I...

    C #sharp Visual Studio Programming, Implement the array declaration and initialization for -For this quiz I attached (EXAMPLE of 10 questions).... Using arrays to format this quiz? using System; namespace Quiz {     class MainClass     {         public static void Main(string[] args)         {             Console.WriteLine("Welcome to the Quiz!");             Console.WriteLine("--------------------------------");             Console.WriteLine(" ");             //Questions                       var Question1 = "1. How many trees in a forest?";             var Question2 = "2. Sample Question";             var Question3 =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT