(c++)Redo exercise 3-9a, where a character array s is assigned to other characte
ID: 3730344 • Letter: #
Question
(c++)Redo exercise 3-9a, where a character array s is assigned to other character arrays in uppercase su and lowercase sl, going through the s array character by character. Change to using a separate variable character pointer and incrementing the variable character pointer through the s array, converting and assigning each character that the character pointer is pointing at into the su or sl arrays. Change the two loops to while loops where the incrementing character pointer is compared against the address at the end of the filled up portion of the s character array. You can leave the uppercase su and lowercase sl arrays in array notation with index variable i (make sure you keep variable i, initialize it to 0, and keep the i++ part of the loop). You can leave the statements, print statements, and strcmp statements as they are. ex3-9a2.cpp is attached (slightly modified with a couple of comment hints).
/*
Exercise 3-9a2
This program will print a line of text in uppercase and lowercase
character by character using c-strings
*/
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;
void main()
{
char s[50], su[50], sl[50];
// set up pointer to increment through s array
int i = 0;
cout << "Enter a line of text: ";
cin.getline(s, sizeof(s), ' ');
// change to while loop comparing pointer addresses
/* address in new pointer is less than address strlen
memory spots over from beginning of s array */
for (i = 0; i < strlen(s); i++)
su[i] = (char)toupper(s[i]);
// access s using pointer, keep su in array notation with i
su[i] = '';
cout << endl << su << endl;
if (strcmp(s, su) > 0)
cout << s << " is some lowercase ";
// change to while loop comparing pointer addresses
i = 0;
while (s[i] != '')
{
sl[i] = (char)tolower(s[i]); // access using pointer
i++;
}
sl[i] = '';
cout << endl << sl << endl;
if (strcmp(s, sl) < 0)
cout << s << " is some uppercase ";
}
Explanation / Answer
ScreenShot
------------------------------------------------------------------------------------------------------------------------------------------------------
Code:-
//Header Files
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;
//Main Method
void main()
{
char s[50], su[50], sl[50];
// set up pointer to increment through s array
int i = 0;
//Prompt the user to input string
cout << "Enter a line of text: ";
cin.getline(s, sizeof(s), ' ');
// change to while loop comparing pointer addresses
/* address in new pointer is less than address strlen
memory spots over from beginning of s array */
while (i < strlen(s)) { //While loop instead of for loop
su[i] = (char)toupper(*(s + i)); //put pointer instead of array indexing
i++; //increment pionter
}
// access s using pointer, keep su in array notation with i
su[i] = '';
cout << endl << su << endl;
if (strcmp(s, su) > 0)
cout << s << " is some lowercase ";
// change to while loop comparing pointer addresses
i = 0;
while (*(s+i) != '')
{
sl[i] = (char)tolower(*(s+i)); // access using pointer
i++;
}
sl[i] = '';
cout << endl << sl << endl;
if (strcmp(s, sl) < 0)
cout << s << " is some uppercase ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.