(programming c-string) Specifically you are not allowed to include string, cstdl
ID: 3698387 • Letter: #
Question
(programming c-string)
Specifically you are not allowed to include string, cstdlib or math libraries. Also you are not allowed to use any built-in functions of c-strings.
bool isSubstring(const char *s1, const char* s2)
{
/* returns true is s1 is a substring of s2 otherwise returns false Definition: s1 is a substring of s2 if s1 is found in s2. That is all characters of s1 are found TOGETHER in s2 in the SAME ORDER as they appear in s1 Example "set" is a substring of "massachussettes" But "ets" is not substring of "massachussettes" */
}
int countWords(const char* s)
{
/* Given a c-string that contains some words separated by spaces, return the number of words in the c-string.
}
//Test isSubstring function
cout << endl; flag = isSubstring(s1, s4);
if (flag)
cout << s1 << " is a substring of " << s4 << endl;
else
cout << s1 << " is not a substring of " << s4 << endl; char s5[] = "abort";
char s6[] = "abcabodefaborhuhabortabunny";
flag = isSubstring(s5, s6);
if (flag)
cout << s5 << " is a substring of " << s6 << endl;
else
cout << s5 << " is not a substring of " << s6 << endl;
//Test countWords function
cout << endl;
char s7[] = "";
c = countWords(s7);
cout << "There are " << c << " words in " << s7 << endl;
char s8[] = "Test";
c = countWords(s8);
cout << "There are " << c << " words in " << s8 << endl;
char s9[] = "Nice one";
c = countWords(s9);
cout << "There are " << c << " words in " << s9 << endl;
char s10[] = "This is a nice assignment and hopefully an interesting as well";
c = countWords(s10);
cout << "There are " << c << " words in " << s10 << endl;
the ideal output is:
massachussettes is not a substring of htsemsaesuatscs
abort is a substring of abcabodefaborhuhabortabunny
There are 0 words in
There are 1 words in Test
There are 2 words in Nice one
There are 11 words in This is a nice assignment and hopefully an interesting as well
Explanation / Answer
issubstring function is to return true if the substring found in the string or false if the substring not found in the string.
countWords() function returns the number of words in the given text.
code:
#include <iostream>
#include<iomanip>
using namespace std;
bool isSubstring(const char *s1, const char* s2)
{
//performs the loop until the last character of s2
for (int i = 0; s2[i] != ''; ++i) {
int j = 0; //initialise j to 0 to check s1 is present in s2
int k = i; //initialise i value to k
if (s2[i] == s1[j]) { //if the first letter of s1 is present in s2
//then check for next values present in s1 is in s2. if all the letters found return true.
while (((s1[j] != '') || (s2[k] != ''))&&(s1[j] == s2[k])) {
k++;
j++;
if (s1[j] == '') return true;
}
//if the letter mismatches again the i value will be incremented and checks for next letter of s2 with first letter of s1
}
}
//if no matches are found returns false
return false;
/* returns true is s1 is a substring of s2 otherwise returns false Definition: s1 is a substring of s2 if s1 is found in s2. That is all characters of s1 are found TOGETHER in s2 in the SAME ORDER as they appear in s1 Example "set" is a substring of "massachussettes" But "ets" is not substring of "massachussettes" */
}
int countWords(const char* s)
{
int count = 0; //to count the number of words in the string
for (int i = 0; s[i] != ''; ++i) { //loop is performed until the last character founds as
if ((s[i] == ' ') || (s[i + 1] == '')) { //if the character is space or and end of the text, word count is increment by 1
count++;
while ((s[i] != '')&&(s[i] == ' ')) //checks for the repeated space
i++;
}
}
return count;
//Given a c-string that contains some words separated by spaces, return the number of words in the c-string.
}
int main() {
char s1[] = "ets";
char s4[] = "massachussettes";
cout << endl;
bool flag = isSubstring(s1, s4);
if (flag)
cout << s1 << " is a substring of " << s4 << endl;
else
cout << s1 << " is not a substring of " << s4 << endl;
char s5[] = "abort";
char s6[] = "abcabodefaborhuhabortabunny";
flag = isSubstring(s5, s6);
if (flag)
cout << s5 << " is a substring of " << s6 << endl;
else
cout << s5 << " is not a substring of " << s6 << endl;
//Test countWords function
cout << endl;
char s7[] = "";
int c = countWords(s7);
cout << "There are " << c << " words in " << s7 << endl;
char s8[] = "Test";
c = countWords(s8);
cout << "There are " << c << " words in " << s8 << endl;
char s9[] = "Nice one";
c = countWords(s9);
cout << "There are " << c << " words in " << s9 << endl;
char s10[] = "This is a nice assignment and hopefully an interesting as well";
c = countWords(s10);
cout << "There are " << c << " words in " << s10 << endl;
return 0;
}
Sample run:
ets is not a substring of massachussettes
abort is a substring of abcabodefaborhuhabortabunny
There are 0 words in
There are 1 words in Test
There are 2 words in Nice one
There are 11 words in This is a nice assignment and hopefully an interesting as well
RUN SUCCESSFUL (total time: 166ms)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.