C++ question do all in one code plz no pointers /// thanks do (( functions )) St
ID: 668647 • Letter: C
Question
C++ question
do all in one code plz no pointers /// thanks
do (( functions ))
StrLen: Length of a string
StrCpy: Copy cstrings
StrCat: Concatenate cstrings
StrChr: Index of a given char in a string
SubStr: produces a "slice" of a string given starting point and length
StrStr: Finds one string within another. (return an integer index)
FirstOf:First occurance of any of the characters in one string in another: FirstOf("abcdef", "123a45") will return 3 (position of 'a')
FirstNotOf: First occurence of a character within a string that's not a member of another string: FirstNotOf("12345", "123 Elm") will return 3 (Position of ' ')
functions.
Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[10] = "Hello";
char str2[10] = "World";
char str3[10];
int len ;
// copy str1 into str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;
// concatenates str1 and str2
strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;
// total lenghth of str1 after concatenation
len = strlen(str1);
cout << "strlen(str1) : " << len << endl;
char *pch=strchr(str1,'e');
while (pch!=NULL)
{
cout<< " strchr(str1,'e'):found at ",pch-str1+1);
pch=strchr(pch+1,'e');
}
string text = "hello world, this is a test";
string fragment = text.substr (6, 5);
cout<< "text.substr (6, 5)"<<fragment;
char* mystrstr("hello", "el");
First_Of();
First_Not() ;
return 0;
}
const char* mystrstr(const char* str,
const char* search)
{
if (!str || !search)
{
return 0;
}
while(*str != '')
{
int len = 0;
const char* sub = search;
while(*sub != '')
{
if (*sub == *str)
{
sub++;
str++;
len++;
if (*sub == '')
{
return str - len;
}
}
else
{
str -= len;
break;
}
}
str++;
}
return 0;
}
void First_Of()
{
string str("C++ is best language");
int pos1, pos2;
pos1 = str.find ("best");
cout << "Word best is found on position " << pos1+1
<< endl;
pos2 = str.find ("best",pos1+1);
cout << "Word best is found on position " << pos2+1
<< endl;
// search for first occurrence of character
pos1 = str.find('g');
cout << "First character 'g' found on position "
<< pos1
<< endl;
// search for first occurrence of string
string s = "is";
pos1 = str.find (s);
cout << "Word 'is' is found on position " << pos1+1
<< endl;
return 0;
}
void First_Not()
{
string str("C++ is best language");
cout << "str is: " << str << endl;
int n = str.find_first_not_of("aeiouAEIOU");
cout << "First consonant found at " << n+1
<< " position" << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.