Recall, a C-string is a null-terminated character array. The null characters pre
ID: 3698894 • Letter: R
Question
Recall, a C-string is a null-terminated character array. The null characters preceding the null terminator ('') are considered to be the characters of the string.
For example, the word "hello" would be stored in a char array of size six or bigger and the initial six characters would be 'h', 'e', 'l', 'l', 'o', ''.
Any array elements after the null terminator are ignored.
To get familiar with C-strings, write the following functions:
int getLength(const char w[]); // return the length of the C-string (excluding the null-terminator)
void toUppercase(char w[]); // change any lowercase letters 'a' to 'z' to uppercase 'A' to 'Z'.
bool equal(const char v[], const char w[]); // return true if v and w contain the same characters in same order, up to and including null terminators
void copy(char destination[], const char source[]); // copy source into destination including the null terminator. Assume the underlying array for destination is big enough to hold all the characters of source.
void removeLeadingSpaces(char w[]); // remove any leading spaces from w e.g. " hello" --> "hello". If w only contains spaces then a null terminator will be assigned to w[0], so " " --> "".
char * getPointerToFirstNonSpace(const char w[]); // return a pointer to the first character that's not a space.
int countVowels(const char w[]) ; // return the number of vowels in w, where a vowel is 'a' , 'e', 'i' , 'o' or 'u' (We're not counting 'y' as a vowel.)
bool comesBefore(const char v[], const char w[]); // return true if v comes before w in dictionary order. The means compare character by character and return true or false based on first nonmatching character. If all character match, return false.
Upload your functions along with a main that tests them
Explanation / Answer
Please find my implementation:
#include<iostream>
using namespace std;
int getLength(const char w[]){
int i = 0;
while(w[i] != ''){
i++;
}
return i;
}
void toUpperCase(char w[]){
int i = 0;
while(w[i] != ''){
if((int)(w[i]) >= 97){
w[i] = (char)((int)w[i] - 32);
}
i++;
}
}
bool equal(const char w1[],const char w2[]){
int n = getLength(w1);
if(n != getLength(w2)){
return false;
}
for(int i = 0;i<n;i++){
if(w1[i] != w2[i]){
return false;
}
}
return true;
}
void copy(char dest[],const char w[]){
int i = 0;
while(w[i] != ''){
dest[i] = w[i];
i++;
}
dest[i] = w[i];
}
char* getPointerToFirstNonSpace(const char w[]){
int i = 0;
while(w[i] == ' '){
i++;
}
return (char*)(&w[i]);
}
void removeLeadingSpace(char w[]){
int i = 0,j,k;
while(w[i] == ' '){
i++;
}
for(j = i,k = 0;w[j] != '';j++){
w[k] = w[j];
k++;
}
w[k] = '';
}
int countVowels(const char w[]){
int i = 0;
int count = 0;
while(w[i] != ''){
switch(w[i]){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
count++;
}
}
return count;
}
bool comesBefore(const char v[],const char w[]){
int n1 = getLength(v);
int n2 = getLength(w);
if(n1 < n2){
for(int i = 0;i<n1;i++){
if(v[i] < w[i]){
return true;
}else if(v[i] > w[i]){
return false;
}
}
return true;
}else{
for(int i = 0;i<n2;i++){
if(v[i] < w[i]){
return true;
}else if(v[i] > w[i]){
return false;
}
}
return false;
}
}
int main(){
char w[] = " hello";
removeLeadingSpace(w);
cout<<w;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.