help with string hw

I did half of it, but I cannot for the life of me figure out how to code Excercises 1, 5, and 6. Any help would greatly be appreciated!



Exercise 1. Write the function int stringfind( const char * source, char c ) which
searches the character c inside the string source. The rst time such a character is found, the
function returns the position of the character. Otherwise, the function returns -1.
For example: if const char * source = "Daniel" and char c= 'i', the function returns 3.

Exercise 2. Write the function bool isletter( char c ) which returns true if char c is a
letter. Otherwise, the function returns false.
2
Hint: Use the ASCII code value of c.

Exercise 3. Write the function bool islowercase( char c ) which returns true if char c is
a lower case letter. Otherwise, the function returns false.
Hint: Use the ASCII code value of c.

Exercise 4. Write the function bool isuppercase( char c ) which returns true if char c is
an upper case letter. Otherwise, the function returns false.
Hint: Use the ASCII code value of c.

Exercise 5. Write the function char * touppercase( char * destination, const char *
source ) which copies the chars of source to destination. If the char to be copied is not a letter,
then it should be copied as is. If the char to be copied is a letter, then the uppercase of the char
should be copied in destination. Lastly, the destination is returned.
For example: If source = "DanieL 814*@" , then destination = "DANIEL 814*@" .

Exercise 6. Write the function char * tolowercase( char * destination, const char *
source ) which should behave like the previous exercise, this time using lower case.
For example: If source = "DaNieL 814*@" , then destination = "daniel 814*@" .

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include <cstring>

using namespace std;


void separator() {
cout << "\n--------------------------\n\n";
}


size_t stringlength( const char * s );

// you need to implement the 6 functions below
int stringfind( const char * source, char d );
bool isletter( char c );
bool islowercase( char c );
bool isuppercase( char c );
char * touppercase( char * destination, const char * source );
char * tolowercase( char * destination, const char * source );

int main() {


char s1[100], s5[100], d5[100], s6[100], d6[100];
char c;
char quit;

while ( 1 ) {

cout << "\nPress q or Q to quit: ";
cin >> quit;
if ( quit == 'q' or quit == 'Q' )
break;


cout << "Exercise 1: stringfind\n";
cout << "Enter a string : ";
cin.ignore( 255, '\n' );
cin.getline( s1, 99 );
cout << "Enter a char to found in the above string: ";
cin >> c;

if ( stringfind( s1, c ) != -1 )
cout << c << " found at position " << stringfind( s1, c);
else
cout << c << " is not found";

separator();


cout << "Exercise 2: isletter\n";
cout << "Enter a char : ";
cin >> c;

if ( isletter( c ) )
cout << c << " is a letter ";
else
cout << c << " is not a letter";

separator();


cout << "Exercise 3: islowercase\n";
cout << "Enter a char : ";
cin >> c;

if ( islowercase( c ) )
cout << c << " is a lower case letter ";
else
cout << c << " is not a lower case letter";

separator();


cout << "Exercise 4: isuppercase\n";
cout << "Enter a char : ";
cin >> c;

if ( isuppercase( c ) )
cout << c << " is an upper case letter ";
else
cout << c << " is not an upper case letter";

separator();


cout << "Exercise 5: touppercase\n";
cout << "Enter a string : ";
cin.ignore( 255, '\n' );
cin.getline( s5, 99 );
touppercase( d5, s5 );
cout << d5;

separator();

cout << "Exercise 6: tolowercase\n";
cout << "Enter a string : ";
cin.getline( s6, 99 );
tolowercase( d6, s6 );
cout << d6;

separator();

} //end of while ( 1 )

return 0;

}

size_t stringlength(  const char * s ) {

const char * t = s;

while ( *t != '\0' )
t++;
return t - s;

}


int stringfind(  const char * source, char c ) {

// Exercise 1: your code here . . .



}


bool isletter( char c ) {

// Exercise 2: your code here . . .
if ( c >= 'a' && c <= 'z')
{
return true;
if ( c >= 'A' && c <= 'Z')
{
return true;
}
else return false;
}
}

bool islowercase( char c ) {

// Exercise 3: your code here . . .
if ( c >= 'a' && c <= 'z')
{
return true;
}
else return false;

}

bool isuppercase( char c ) {

// Exercise 4: your code here . . .
if ( c >= 'A' && c <= 'Z')
{
return true;
}
else return false;

}

char * touppercase( char * destination,  const char * source ) {

// Exercise 5: your code here . . .




return destination;

}

char * tolowercase( char * destination,  const char * source ) {

// Exercise 6: your code here . . .





return destination;

}
Last edited on
Topic archived. No new replies allowed.