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
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
ADVERTISEMENT