6 - String and C String Manipulation
6 - String and C String Manipulation
Manipulation
CS101P -2
(Logic Formulation, Algorithms and Problem
Solving 2)
Sample Program (String Class)
Program 1
// This program cycles through a character array, displaying
// each element until a null terminator is encountered.
#include <iostream.h>
void main(void)
{
char line[80];
int count = 0;
4
Program 2
// This program demonstrates the C++ string class.
#include <iostream>
#include <string> // Required for the string class
using namespace std;
void main(void)
{
string movieTitle;
string name("William Smith");
Program output:
My favorite movie is Wheels of Fury
5
Program 3
// This program uses the == operator to compare the string entered
// by the user with the valid stereo part numbers.
#include <iostream>
#include <string>
using namespace std;
void main(void)
{
const float aprice = 249.0, bprice = 299.0;
string partNum;
6
Program 3 (continued)
if (partNum == "S147-29A")
cout << "The price is $" << aprice << endl;
else if (partNum == "S147-29B")
cout << "The price is $" << bprice << endl;
else
cout << partNum << " is not a valid part number.\n";
}
Program Output
7
Program 4
// This program demonstrates the C++ string class.
#include <iostream>
#include <string>
using namespace std;
void main(void)
{
string str1, str2, str3;
str1 = "ABC";
str2 = "DEF";
str3 = str1 + str2;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
str3 += "GHI";
cout << str3 << endl;
}
8
Program Output
ABC
DEF
ABCDEF
ABCDEFGHI
9
Program 5
// This program demonstrates a function, countChars, that counts
// the number of times a specific character appears in a string.
#include <iostream.h>
int countChars(char *, char); // Function prototype
void main(void)
{
char userString[51], letter;
cout << "Enter a string (up to 50 characters): ";
cin.getline(userString, 51);
cout << "Enter a character and I will tell you how many\n";
cout << "times it appears in the string: ";
cin >> letter;
cout << letter << " appears ";
cout << countChars(userString, letter) << " times.\n";
}
10
Program 5 continues
// Definition of countChars. The parameter strPtr is a pointer
// that points to a string. The parameter ch is a character that
// the function searches for in the string. The function returns
// the number of times the character appears in the string.
int countChars(char *strPtr, char ch)
{
int times = 0;
while (*strPtr != '\0')
{
if (*strPtr == ch)
times++;
strPtr++;
}return times;
}
11
Program Output With Example input
12
LABORATORY EXERCISE
STRING MANIPULATION
13
Lab. Exer 1
Write a program that will read in a line of text
and output the number of words in the line and
the number of occurrences of each of the letter.
Ask the user to enter any word to be any string of
letters that is delimited at each end by either
whitespace, a period, a comma or the beginning
or end of the line. Assume that the input consists
entirely of letters, whitespace, commas, and
periods. In displaying the number of letters that
occur in a line, be sure to count uppercase and
lowercase versions of a letter as the same letter.
Finally, display the letters in alphabetical order
and list by only those letters that occur in the
input line.
14
Lab. Exer 1
15
Lab Exer 2
16
Practical Exam 1
Sample output:
Enter an Arabic number: 30
Equivalent Roman numeral is: XXX
17
Practical Exam 2
Write a program that will ask the user to enter the last names of
four candidates in a class officer’s president election and the
number of votes received by each candidate. Afterwards, your
program should display each candidate’s name, the number and
the percentage of the total votes received by the candidate.
18
LABORATORY EXERCISE
CHARACTER MANIPULATION
19
Practical Exam 3
20