Question
Write this function in c++.Use of string and <cstring> is not allowed .You are required to use char* and implement the use of ** in this question .

Gtest case to pass:

#include "q1.cpp"
#include <gtest/gtest.h>

//-------------------Q1_8-----------------
TEST(Question1_8, First) {
char t1[]="Hello World";
char res1[] = "Hello";
char res2[] = "World";

char** r= StrTok(t1,' ');
ASSERT_EQ(0, strcmp(r[0],res1) );
ASSERT_EQ(0, strcmp(r[1],res2) );
}

TEST(Question1_8, Second) {
char t1[]="Hello?World?OOP";
char res1[] = "Hello";
char res2[] = "World";
char res3[] = "OOP";

char** r= StrTok(t1,'?');
ASSERT_EQ(0, strcmp(r[0],res1) );
ASSERT_EQ(0, strcmp(r[1],res2) );
ASSERT_EQ(0, strcmp(r[2],res3) );
}


int main(int argc, char **argv) {

testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();
}


1 char **Str Tok char *sl, const char s2) 2 / A call to Str Tok breaks stringsl into token 3 (logical pieces such as words i

pointer to pointer must be used
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello,

Here is the code for the method using pointers. Kindly comment below in case of any queries.

Code:

char** StrTok(char* s1, const char s2)
{
   char *c_temp = new char[COL];       // Holds single string
   char **s_temp = new char*[ROW];       // Holds 2-D array of string
   char **dest = s_temp;               // Stores final output result

   *s_temp = c_temp;   // Pointer to current string

   // Iterate char by char
   while (*s1 != '\0')
   {
       if (*s1 == s2)       // Move to next row on delimiter match with current character
       {
           *c_temp = '\0';           // Complete the current string
           *s_temp++;               // Move to next row
           *s1++;                   // Skip the delimiter
           c_temp = new char[COL];   // Prepare the next string
           *s_temp = c_temp;       // Pointer to current string
           continue;
       }

       *c_temp = *s1;       // Copy single character
       *c_temp++, *s1++;   // Move to next character
   }

   *c_temp = '\0';       // Complete the current string

   return dest;       // Return the result string
}

Output:

test result: ok. 5 passed; O failed; _ ignored; measured; filtered out

Add a comment
Know the answer?
Add Answer to:
Write this function in c++.Use of string and <cstring> is not allowed .You are required to...
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
  • C++

    What will be the output of the following C++ program?#include <iostream> #include <string>#include <cstring>using namespace std; int main(int argc, char const *argv[]){ const char *a = "Hello\0World"; cout<<a; return 0;}a) Hellob) Worldc) Errord) Hello World

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

  • Help please this is C/C++ code /* * Lab12, the purpose of this lab is to...

    Help please this is C/C++ code /* * Lab12, the purpose of this lab is to improve your skills in handling c strings * with pointers . * use of : strlen ,string concatenation strcat, compare strcmp, * copy strcpy and search strrchr * */ #include <cstdlib> #include <iostream> #include<cstring> using namespace std; /** * Create a method that prompts the user for only lowercase letters to represent * a name. * Start a Label, then prompt the user to...

  • For the following task, I have written code in C and need help in determining the...

    For the following task, I have written code in C and need help in determining the cause(s) of a segmentation fault which occurs when run. **It prints the message on line 47 "printf("Reading the input file and writing data to output file simultaneously..."); then results in a segmentation fault (core dumped) I am using mobaXterm v11.0 (GNU nano 2.0.9) CSV (comma-separated values) is a popular file format to store tabular kind of data. Each record is in a separate line...

  • You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function...

    You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function into a file named Q1.cpp. Q1.cpp should only include your function implementation, the necessary #include directives if needed, and should not contain anything else such as the main function or global variable declarations. Test your code using a separate main.cpp file where you implement a sufficient number of test cases. You should use Q1.h as a header file and Q1main.cpp as a main function...

  • Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You...

    Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You will place all of your function prototypes in a header file named string utils.h and all of your function definitions in a source file named string utils.c. You should implement your own main test driver program to test your functions, but you need not hand it in. a. void addChar(char *str, char c, int n) - this function should add a char c at...

  • -I need to write a program in C to store a list of names (the last...

    -I need to write a program in C to store a list of names (the last name first) and age in parallel arrays , and then later to sort them into alphabetical order, keeping the age with the correct names. - Could you please fix this program for me! #include <stdio.h> #include <stdlib.h> #include <string.h> void data_sort(char name[100],int age[100],int size){     int i = 0;     while (i < size){         int j = i+1;         while (j < size){...

  • use c code to Develop a function void printToggled(char str[]) that gets a string as a...

    use c code to Develop a function void printToggled(char str[]) that gets a string as a parameter and prints every lowercase character as an uppercase one and vise versa. For example, if the string submitted as a parameter is Hello WorlD! The function should print hELLO wORLd! Hint:answer must include output

  • Write C programs named mystring.h and mystring.c, containing the headers and implementations of the following functions....

    Write C programs named mystring.h and mystring.c, containing the headers and implementations of the following functions. int letter_count(char *s) computes and returns the number of English letters in string s. int word_count(char *s) computes and returns the number of words in string s. void lower_case(char *s) changes upper case to lower case of string s. void trim(char *s) removes the unnecessary empty spaces of string s. mystring.h #include <stdio.h> int letter_count(char *); void lower_case(char *); int word_count(char *); void trim(char...

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