(Program in C++) Using only pointer notation, implement and test the following f
ID: 3882950 • Letter: #
Question
(Program in C++) Using only pointer notation, implement and test the following functions that operate on strings:
1- int strCmp ( const char *s1, const char *s2) returns 0 if s1 and s2 are the same; it returns-1 when s1 precedes alphanumerically s2 or it returns 1 when s1 follows alphanumerically s2.
2- char * strPbrk (const char*s1, const char*s2) scans s1 for the first occurrence of any character appearing in s2 and returns a pointer to its location or NULL if no such occurrence is found.
3- char* ItoA ( int n, char*s) converts an integer n into string of numerals stored in s and returns s.
Explanation / Answer
#include<iostream>
#include<string.h>
#include <cstdio>
using namespace std;
int strCmp ( const char *s1, const char *s2){
// int i = s1.compare(s2);
int i;
if(s1 == s2){
i=0;
}else{
i=-1;
}
return i;
}
char * strPbrk (const char*s1, const char*s2){
char *ptr ;
ptr = strpbrk(s1, s2);
if(ptr){
return ptr;
}
else{
return 0;
}
}
int main(){
cout<<strCmp("sam","sam") <<endl;
cout<<strPbrk("sanju","n") <<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.