0% found this document useful (0 votes)
4 views2 pages

Lab 9

The document outlines a lab exercise involving C++ programming tasks. It includes determining character values from specific strings, writing a program to read and display a string, counting words in a string, and creating a function to count letters while excluding other characters. Sample code snippets are provided to illustrate the solutions for these tasks.

Uploaded by

riadxx1122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Lab 9

The document outlines a lab exercise involving C++ programming tasks. It includes determining character values from specific strings, writing a program to read and display a string, counting words in a string, and creating a function to count letters while excluding other characters. Sample code snippets are provided to illustrate the solutions for these tasks.

Uploaded by

riadxx1122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab9

1. Determine the value of text.at(0), text.at(3), and


text.at(10), assuming for each one that text is each of the following
strings:
a. Now is the time.
b. Rocky raccoon welcomes you.
c. Happy Holidays.
d. The good ship.
Solution

2. Using the at() method, write a C++ program that reads in a string by using
getline(), stores the string in a string object named message, and then
displays the string.
Hint: After the string has been entered and saved, retrieve and display
characters, display the length of string using the( message.length())
Solution:.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

int main ()
{

string str;
int i;
getline(cin,str);
int size = str.length();

for (i= 0; i <= (size - 1); i++)


{
cout << str.at(i);
}
cout<<" ";
cout<<str.length();

return 0;
}

3. Write a C++ program that counts the number of words in a string. A word is
encountered whenever a transition from a blank space to a nonblank
character is encountered. The string contains only words separated by blank
spaces.

4. Write a function named countlets() that returns the number of letters


in a string passed as an argument. Digits, spaces, punctuation, tabs, and
newline characters shouldn’t be included in the returned count. Include the
countlets() function written for Exercise 6a in an executable C++
program, and use the program to test the function.

You might also like