Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need help on c++ program please. // SmpCStrings.cpp // Kim Buckner // COSC1030,

ID: 3816314 • Letter: N

Question

need help on c++ program please.

// SmpCStrings.cpp
// Kim Buckner
// COSC1030, Sp 2017
// Lecture 19
// Playing with null-terminated C-strings.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include<cstring>

int main()
{
char message1[]="Howdy";

cout << "message1: " << message1 << endl;
cout << "sizeof(message1): " << sizeof(message1) << endl;

char *msgPtr;
msgPtr = message1; // Try to compile and run without this line!
cout << "msgPtr (first): " << msgPtr << endl;
cout << "sizeof(msgPtr): " << sizeof(msgPtr) << endl << endl;

char message2[]="Here's a much longer message.";
cout << "message2: " << message2 << endl;
cout << "sizeof(message2): " << sizeof(message2) << endl;

msgPtr = message2;
cout << "msgPtr (second): " << msgPtr << endl;
cout << "sizeof(msgPtr): " << sizeof(msgPtr) << endl << endl;

char message3[]={'H','e','r','e',' ','w','e',' ','g','o'};
cout << "message3: " << message3 << endl;
cout << "sizeof(message3): " << sizeof(message3) << endl;

msgPtr = message3;
cout << "msgPtr (third): " << msgPtr << endl;
cout << "sizeof(msgPtr): " << sizeof(msgPtr) << endl << endl;

msgPtr = new char[56];
for(int i=0;i<10;i++) {
msgPtr[i]=65+i;
}
msgPtr[10]='';


cout << "msgPtr (fourth): " << msgPtr << endl;
cout << "sizeof(msgPtr): " << sizeof(msgPtr) << endl << endl;

cout << " Now enter 5 strings << " << endl;
char buffer[256];
int len;
for(int i=0;i<5;i++) {
cin >> buffer;
len=strlen(buffer);
cout << i << ". " << len << ": " << buffer << endl;
}

return 0;
}

Procedure Now we will write a simple program that will interact with the user. You will prompt the user for various inputs. You will read in strings and lines. Then you will report some information on them and modify them. 1. Construct a new Microsoft Visual Studio Win32 Console Application project for Lab 11 2. I suggest that you refer to the file SmpCStrings.cpp from Lecture 19. You will need to read strings in from the user into a buffer (an array that is used to "buffer" data between the program and external devices). That file has examples of the some the things you will need to do. 3. Prompt the user for a string, read it in, and "echo it back out one character at a time, treating the string buffer as a simple array 4. Prompt the user for sentence (less than 256 characters long) Read in the entire sentence as a single string. You will need the cin.getline0 function to do this; this function is slightly different from the std: getline. You need to do a little research on how to use it. Now report the length of the string (use strlen() to get that). Also report on what the cin.gcount0 function returned. There is problem here. That new line that the user typed in the last time, is still in the stream. So that means this will immediately return with a 0 length string. So, just BEFORE you prompt the user for the sentence, use cin.getline() to read in whatever is left in the stream, and just ignore it. Here you can use the same buffer and just not worry about whatever is in it. Everytime you use cin.getline0 it will overwrite the storage with the new data. 5. Prompt the user to enter an integer. You will use that number to shorten the string. That means that you are going to change the character stored in that position in the buffer. BUT, before you change that character (to 0) you need to save the original character. Because after you print out the modified string, you will restore the removed character and print out the original string

Explanation / Answer

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include<cstring>
int main()
{

char sentence[256], tmpChar;
int inputIt = 0;
  
cout << "Please enter a string" << endl;
cin >> sentence;
cout << "Echoing that string: "<<sentence << endl << "Please enter a sentence (less than 256 characters): " << endl;
   cin.ignore();
cin.getline (sentence,256);
cout << "This is that sentence not more than 256 characters. "<< endl << "You entered " << strlen(sentence) << " characters. And " << strlen(sentence) << " were removed from the stream." << endl;
cout << "Please give me an integer (between 0 and 51) : " << endl;
cin >> inputIt;
tmpChar= sentence[inputIt];
sentence[inputIt] = '0';
cout << " The sentence is now: " << sentence << endl << "This is that sentence "<<endl ;
sentence[inputIt] = tmpChar;
cout << " The original is now: " << sentence <<endl;
  
return 0;
}