Question

#include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n characters from
int main() char userInput[ 81]; char *pChar = NULL; User input to be manipulated // Pointer to the result for some of the str
// Stage 3: implement mystrrchr cout<<Enter the character to be reverse-found: cin >> c pChar mystrrchr (userInput, c); if Use C++
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code is commented and variables are named accordingly.

Code:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <cstring>

using namespace std;

// Copy n character from the source to destiation

void mystrncpy(char* destination, char* source, int num){

for(int i=0;i<num&&source[i]!='\0';i++)

destination[i]= source[i];

}

char *mystrchr(char* source, char c){

for(int i=0;source[i]!='\0';i++)

if(source[i]==c)

return (source+i);

return NULL;

}

char *mystrrchr(char* source, char c){

char *pChar= NULL;

for(int i=0;source[i]!='\0';i++)

if(source[i]==c)

pChar= (source+i);

return pChar;

}

int main() {

char userInput[81]; // User input to be manipulated
char *pChar = NULL; // Pointer to the result for some of the string function
  
cout<< "Provide a line of input: ";
fgets(userInput, 81, stdin);
  
// Stage 1: implement mystrncpy
int numberOfCharacters;
char destination[81];
  
cout<< "Enter number of characters to be copied: ";
cin>>numberOfCharacters;
mystrncpy(destination,userInput,numberOfCharacters);
cout<< "The resulting string is: "<< destination <<endl <<endl;
  
// Stage 2: implement mystrchr
cout<< "Enter the character to be found: ";
char c;
cin>>c;
pChar = mystrchr(userInput,c);
if(pChar==NULL){

cout<<c<<" Not Found!";

}
else{

// Update pChar from pChar - userInput to get position instead of the result that we are getting

cout<<"Found: "<<pChar<<endl;

}
  
// Stage 3: implement mystrrchr
cout<< "Enter the character to be reverse-found: ";

cin>>c;
pChar = mystrrchr(userInput,c);
if(pChar==NULL){

cout<<c<<" Not Found!";

}
else{

// Update pChar from pChar - userInput to get position instead of the result that we are getting

cout<<"Found: "<<pChar<<endl;

}
return 0;

}

Code Screenshot:

1 #include <iostream» 2 #include <stdlib.h> #include <stdio.h> 4 #include <CString> 6 using namespace std; 8 // Copy n charac

36 37 38 39 40 41 42 43 /Stage 1: implement mystrncpy int numberOfcharacters; char destination[81]; cout<< Enter number of c

Output:

Provide a line of input: Hello World Enter number of characters to be copied: 7 The resulting string is: Hello W Enter the ch

Add a comment
Know the answer?
Add Answer to:
Use C++ #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • C++ Debug nextVowel.cpp #include <iostream> using namespace std; // Recursion on a single value // return...

    C++ Debug nextVowel.cpp #include <iostream> using namespace std; // Recursion on a single value // return next character (in ASCII order) // which is a vowel, starting with parameter // If there are no more vowels, return null character ('\0') char nextVowel(char start) { // recursive call return nextVowel(start + 1); // base case if (start > 'z') return '\0'; // incorrect base case if ('a' == start || 'i' == start || 'o' == start || 'u' == start)...

  • please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName();...

    please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...

  • Convert to use functions where possible #include<iostream> #include<string> using namespace std; int main() {    string...

    Convert to use functions where possible #include<iostream> #include<string> using namespace std; int main() {    string first, last, job;    double hours, wages, net, gross, tax, taxrate = .40;    double oPay, oHours;    int deductions;    // input section    cout << "Enter First Name: ";    cin >> first;    cout << "Enter Last Name: ";    cin >> last;    cin.ignore();    cout << "Enter Job Title: ";    getline(cin, job);    cout << "Enter Hours Worked:...

  • Time.cpp: #include "Time.h" #include <iostream> using namespace std; Time::Time(string time) {    hours = 0;   ...

    Time.cpp: #include "Time.h" #include <iostream> using namespace std; Time::Time(string time) {    hours = 0;    minutes = 0;    isAfternoon = false;    //check to make sure there are 5 characters    if (//condition to check if length of string is wrong)    {        cout << "You must enter a valid military time in the format 00:00" << endl;    }    else    {        //check to make sure the colon is in the correct...

  • #include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be...

    #include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be implemented by you } int main() { cout << "To calculate x^y ..." << endl; double x; int y; cout << "Please enter x: "; cin >> x; cout << "Please enter y: "; cin >> y; if(x == 0) { if (y > 0) cout << 0 << endl; else cout << "x^y is not defined" <<endl; } else { cout << improvedPow(x,y)...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT