Sie sind auf Seite 1von 20

String and C-String

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;

cout << "Enter a sentence of no more than 79 characters:\n";


cin.getline(line, 80);
cout << "The sentence you entered is:\n";
while (line[count] != '\0')
{
cout << line[count];
count++;
}
}
3
Program Output with Example input

Enter a sentence of no more than 79 characters:


C++ is challenging but fun! [Enter]
The sentence you entered is:
C++ is challenging but fun!

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");

movieTitle = "Wheels of Fury";


cout << "My favorite movie is " << movieTitle << endl;
}

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;

cout << "The stereo part numbers are:\n";


cout << "\tBoom Box, part number S147-29A\n";
cout << "\tShelf Model, part number S147-29B\n";
cout << "Enter the part number of the stereo you\n";
cout << "wish to purchase: ";
cin >> partNum;
cout.setf(ios::fixed | ios::showpoint);
cout.precision(2);

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

The stereo part numbers are:


Boom Box, part number S147-29A
Shelf Model, part number S147-29B
Enter the part number of the stereo you
wish to purchase: S147-29A [Enter]
The price is $249.00

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

Enter a string (up to 50 characters):Starting Out With C++ [Enter]


Enter a character and I will tell you how many
times it appears in the string: t [Enter]
t appears 4 times.

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

Write a program to convert ordinary Hindu-Arabic


numerals to Roman numerals and vice-versa
( I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, and
M = 1000)

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

Das könnte Ihnen auch gefallen