8/7/2019 6- String and C String Manipulation
1/20
String and C-StringManipulation
CS101P -2
(Logic Formulation, Algorithms and ProblemSolving 2)
8/7/2019 6- String and C String Manipulation
2/20
Sample Program (String Class)
8/7/2019 6- String and C String Manipulation
3/20
8/7/2019 6- String and C String Manipulation
4/20
4
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!
8/7/2019 6- String and C String Manipulation
5/20
5
Program 2// This program demonstrates the C++ string class.
#include
#include // Required for the string classusing namespace std;
void main(void)
{
string movieTitle;
string name("William Smith");
movieTitle = "Wheels of Fury";
cout
8/7/2019 6- String and C String Manipulation
6/20
6
Program 3// This program uses the == operator to compare the string entered
// by the user with the valid stereo part numbers.
#include
#include
using namespace std;
void main(void)
{
const float aprice = 249.0, bprice = 299.0;string partNum;
cout
8/7/2019 6- String and C String Manipulation
7/207
Program 3 (continued)if (partNum == "S147-29A")
cout
8/7/2019 6- String and C String Manipulation
8/208
Program 4// This program demonstrates the C++ string class.
#include
#include using namespace std;
void main(void)
{
string str1, str2, str3;str1 = "ABC";
str2 = "DEF";
str3 = str1 + str2;
cout
8/7/2019 6- String and C String Manipulation
9/209
Program Output
ABC
DEF
ABCDEFABCDEFGHI
8/7/2019 6- String and C String Manipulation
10/2010
Program 5// This program demonstrates a function, countChars, that counts
// the number of times a specific character appears in a string.
#include int countChars(char *, char); // Function prototype
void main(void)
{ char userString[51], letter;
cout
8/7/2019 6- String and C String Manipulation
11/20
11
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;
}
8/7/2019 6- String and C String Manipulation
12/20
12
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 manytimes it appears in the string: t [Enter]
t appears 4 times.
8/7/2019 6- String and C String Manipulation
13/20
LABORATORY EXERCISE
STRING MANIPULATION
13
8/7/2019 6- String and C String Manipulation
14/20
Write a program that will read in a line of textand 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 eitherwhitespace, a period, a comma or the beginning
or end of the line. Assume that the input consistsentirely of letters, whitespace, commas, and
periods. In displaying the number of letters thatoccur in a line, be sure to count uppercase and
lowercase versions of a letter as the same letter.Finally, display the letters in alphabetical orderand list by only those letters that occur in the
input line.14
Lab. Exer 1
8/7/2019 6- String and C String Manipulation
15/20
Lab. Exer 1
15
8/7/2019 6- String and C String Manipulation
16/20
Lab Exer 2
16
8/7/2019 6- String and C String Manipulation
17/20
Practical Exam 1
17
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
8/7/2019 6- String and C String Manipulation
18/20
Practical Exam 2
18
Write a program that will ask the user to enter the last names of
four candidates in a class officers president election and the
number of votes received by each candidate. Afterwards, yourprogram should display each candidates name, the number and
the percentage of the total votes received by the candidate.
8/7/2019 6- String and C String Manipulation
19/20
LABORATORY EXERCISE
CHARACTER MANIPULATION
19
8/7/2019 6- String and C String Manipulation
20/20
20
Practical Exam 3
Top Related