-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 15.2
73 lines (57 loc) · 3.11 KB
/
Exercise 15.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
1. (Practice) Determine the value of *text, *(textƒ+ƒ3), and *(textƒ+ƒ10), assuming text
is an array of characters and the following has been stored in the array:
a. Be of good purpose
b. Harry Houdini was a magician
c. What a great movie
d. Oops, my bad!
2. (Program) a. The following function, convert(), “marches along” the C-string passed to it
and sends each character in the string one at a time to the ToUpper() function until the NULL
character is encountered:
// convert a string to uppercase letters
void convert(char strng[])
{
int i = 0;
while (strng[i] != '\0')
{
strng[i] = ToUpper(strng[i]);
i++;
}
return;
}
char ToUpper(char letter) // convert a character to uppercase
{
if( (letter >= 'a') && (letter <= 'z') )
return (letter - 'a' + 'A');
else
return (letter);
}
The ToUpper() function takes each character passed to it and examines it to determine
whether the character is a lowercase letter (any character from a to z). Assuming that characters
are stored with the standard ASCII character codes, the expression letter - 'a' + 'A'
converts a lowercase letter to its uppercase equivalent. Rewrite the convert() function with
pointers.
b. Include the convert() and ToUpper() functions in a working program. The program
should prompt the user for a string and display the string in uppercase letters.
3. (Program) Using pointers, repeat Exercise 1 from Section 15.1.
4. (Program) Using pointers, repeat Exercise 2 from Section 15.1.
5. (Program) Using pointers, repeat Exercise 3 from Section 15.1.
6. (Program) Write a function named remove() that returns nothing and deletes all occurrences
of a character from a C-string. The function should take two arguments: the string name
and the character to remove. For example, if message contains the string Happy Holidays,
the function call remove(message,'H') should place the string appy olidays in message.
7. (Program) Using pointers, repeat Exercise 6 from Section 15.1.
8. (Program) Write a program using the cin.get() and toupper() library functions, along
with a cout stream object to display each entered letter in its uppercase form. The program
should terminate when the 1 key (the digit) is pressed.
9. (Program) Write a function that uses pointers to add a single character at the end of an existing
C-string. The function should replace the existing '\0' character with the new character
and add a new \0 at the end of the string. The function returns nothing.
10. (Program) Write a function that uses pointers to delete a single character from the end of a
C-string, which is achieved by moving the '\0' character one position closer to the start of
the string. The function returns nothing.
11. (Program) Write a function named trimfrnt() that deletes all leading blanks from a
C-string and returns nothing. Write the function by using pointers.
12. (Program) Write a function named trimrear() that deletes all trailing blanks from a
C-string and returns nothing. Write the function using pointers.
13. (Program) Write a function named strlen() that returns the number of characters in a
C-string. Don’t include the \0 character in the returned count.