Question

In C++ please!

*Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!*

4 THE FINDANY FUNCTION Write the function findAny().The function has two parameters: a const char * s pointing to the first c

/**
CS 150 C-Strings

Follow the instructions on your handout to complete the
requested function. You may not use ANY library functions
or include any headers, except for <cstddef> for size_t.
*/
#include <cstddef> // size_t for sizes and indexes
///////////////// WRITE YOUR FUNCTION BELOW THIS LINE ///////////////////////

///////////////// WRITE YOUR FUNCTION ABOVE THIS LINE ///////////////////////
// These are OK after the function
#include <iostream>
#include <string>
using namespace std;

void CHECK(const char*, const char *, int);

void studentTests()
{
cout << "Student testing. Add code in this function." << endl;
cout << "-------------------------------------------------------------" << endl;

CHECK("This is a test.", "!.,?;:", 14);
CHECK("This is a test.", "sth", 1);
CHECK("This is a test.", "aeiou", 2);
CHECK("This is a test.", " \t\n\r", 4);
CHECK("This is a test.", "Bob", -1);

cout << endl;
cout << "--done--" << endl;
}

int main()
{
studentTests();
}
#include <cstring>
string repl(const string& s)
{
string r;
for (auto c: s)
{
switch(c)
{
case '\t': r += R"(\t)"; break;
case '\n': r += R"(\n)"; break;
case '\r': r += R"(\r)"; break;
default: r += c;
}
}
return r;
}
void CHECK(const char * s1, const char * s2, int expected)
{
string msg = "findAny(\"" + string(s1) + "\", \"" + repl(string(s2)) + "\")";
char d1[1024], d2[1024];
strcpy(d1, s1);
strcpy(d2, s2);
int actual = findAny(d1, d2);
if (expected == actual)
cout << " + " + msg + "->OK" << endl;
else
cout << " X " + msg + " expected<" << expected << ">, found <" << actual << ">" << endl;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <cstddef> // size_t for sizes and indexes

///////////////// WRITE YOUR FUNCTION BELOW THIS LINE ///////////////////////
int findAny(const char* s, const char* letters){
   int len1 = 0, len2 = 0;
   //len1 for length of the first string
   for(int i = 0; s[i] != '\0'; i++) len1++;
   //len2 for length of the second string
   for(int i = 0; letters[i] != '\0'; i++) len2++;
  
   //Main loop
   for(int i = 0; i < len1; i++){
       int found = 0;
       //Checking if any character in s string matches with any character in letters string
       for(int j = 0; j < len2; j++){
           //if found then break the inner loop
           if(s[i] == letters[j]){
               found = 1;
               break;
           }
       }
       //if found then return the index
       if(found)
           return i;
   }
   //if not found return -1
   return -1;
}
///////////////// WRITE YOUR FUNCTION ABOVE THIS LINE ///////////////////////
// These are OK after the function
#include <iostream>
#include <string>
using namespace std;

void CHECK(const char*, const char *, int);

void studentTests()
{
       cout << "Student testing. Add code in this function." << endl;
       cout << "-------------------------------------------------------------" << endl;
      
       CHECK("This is a test.", "!.,?;:", 14);
       CHECK("This is a test.", "sth", 1);
       CHECK("This is a test.", "aeiou", 2);
       CHECK("This is a test.", " \t\n\r", 4);
       CHECK("This is a test.", "Bob", -1);
      
       cout << endl;
       cout << "--done--" << endl;
       }

int main()
{
studentTests();
}
#include <cstring>
string repl(const string& s)
{
   string r;
   for (auto c: s)
   {
   switch(c)
   {
   case '\t': r += R"(\t)"; break;
   case '\n': r += R"(\n)"; break;
   case '\r': r += R"(\r)"; break;
   default: r += c;
   }
   }
   return r;
}
void CHECK(const char * s1, const char * s2, int expected)
{
   string msg = "findAny(\"" + string(s1) + "\", \"" + repl(string(s2)) + "\")";
   char d1[1024], d2[1024];
   strcpy(d1, s1);
   strcpy(d2, s2);
   int actual = findAny(d1, d2);
   if (expected == actual)
   cout << " + " + msg + "->OK" << endl;
   else
   cout << " X " + msg + " expected<" << expected << ">, found <" << actual << ">" << endl;
}

Add a comment
Know the answer?
Add Answer to:
In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete t...
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
  • Using C++ Use the below program. Fill in the code for the 2 functions. The expected...

    Using C++ Use the below program. Fill in the code for the 2 functions. The expected output should be: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: #include #include #include using namespace std; //Assume a line is less than 256 //Prints the characters in the string str1 from beginIndex //and inclusing endIndex void printSubString( const char* str1 , int beginIndex, int endIndex ) { } void printWordsInAString( const char* str1 ) { } int main() { char str1[] = "The fox jumps over the...

  • Please help me figure out why my code is not working properly.I had thought my logic...

    Please help me figure out why my code is not working properly.I had thought my logic was sound but later found it will run one time through fine however if the user opts to enter another value it will always be returned as an error. #include <iomanip> #include <iostream> #include <cstdlib> using namespace std; int const TRUE = 1; int const FALSE = 0; // declares functions void GetZipcode(); void RunAgain(); int GetSum(char d1, char d2, char d3, char d4,...

  • you may not use any of the C-string library functions such as strlen(), strcpy(), strstr(), etc....

    you may not use any of the C-string library functions such as strlen(), strcpy(), strstr(), etc. You must write your own. You might consider writing helper functions to do tasks that many of these functions require, e.g. finding the last character of the string, and then use those helper functions where convenient. This function finds all instances of the char ‘target’ in the string and replaces them with ‘replacementChar’. It also returns the number of replacements that it makes. If...

  • In C++ write a program that will read three strings from the user, a phrase, a...

    In C++ write a program that will read three strings from the user, a phrase, a letter to replace, and a replacement letter. The program will change the phrase by replacing each occurrence of a letter with a replacement letter. For example, if the phrase is Mississippi and the letter to replace is 's' the new phrase will be "miizzizzippi". in the function, replace the first parameter is a modifiable string, the second parameter is the occurrence of a letter...

  • /// c ++ question plz help me fix this not a new code and explain to...

    /// c ++ question plz help me fix this not a new code and explain to me plz /// Write a function to verify the format of an email address: bool VeryifyEmail(char email[ ]); Do NOT parse the email array once character at a time. Use cstring functions to do most of the work. Take a look at the available cstring and string class functions on cplusplus website. Use a two dimensional array to store the acceptable top domain names:...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • I've posted 3 classes after the instruction that were given at start You will implement and...

    I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

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