Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C function called secondWord that has the prototype char* secondWord(cha

ID: 3907772 • Letter: W

Question

Write a C function called secondWord that has the prototype

char* secondWord(char* inputString, char* outputString);

Your function should do the following:

Finds the second word (separated by spaces or tabs in inputString)

Copies the second word into outputString

Returns a pointer to outputString

The data inside inputString is not changed by this function

It can be assumed that inputString is less than 150 characters

It can be assumed that outputString is able to support the length of the second word

If inputString contains no second word, the function should put a NULL into outputString and return a NULL

For an inputString of “One Two Three Three Four” the outputString should be:

      “Two”

For an inputString of “OneTwoThreeThreeFour” the outputString should be:

      NULL

Write a C program in Code::Blocks to test your secondWord function

This program should create at least 10 use cases for the function that tests its behavior under different input conditions. These use cases are hard coded (no user input). Your code should be able to test any version of the secondWord function (it should be able to test another student’s secondWord function to confirm it operates correctly).

Test cases must include inputs with only 1 word, 2 words, 3 words, use of tabs, use of spaces, uses of tabs and spaces, capital letters, numbers, punctuations, appropriate combinations of test input.

The program should have a known expected output for each example and test the output to see if the output matches this expected output.

The program will output to the terminal and a txt file

The program will output information about what is being tested (secondWord)

The program will output “Function Test Case #” followed by the number of the test case, then output “Input: ” followed by the input, “Expected Output:” followed by the expected output, and “Output: “ followed by the output for each case. The program will print out a “PASS” or a “FAIL” after each output line. Output can be formatted on separate lines as shown below to make things easy to read.

The program will print out a “PASS” or “FAIL” at the end of the output for the whole function test. A FAIL is outputted if any individual test fails.

An example output for the first couple lines of your program are:

Functional Test of secondWord function.

Tests multiple inputs to the secondWord function to confirm operation.

Function Test Case #1

Input:                                 One Two Three

Expected Output:             Two

Output:                              Two

Result:                                PASS

-------------------------------------------

Function Test Case #2

Input:                                 ONE

Expected Output:             NULL

Output:                              NULL

Result:                                PASS

-------------------------------------------

-------------------------------------------

OVERALL RESULT = PASS

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

//Code

#include<stdio.h>

#include<string.h>

//required method to copy second word of inputstring to output string

char* secondWord(char* inputString, char* outputString){

                int i=0;

                //initializing indices to -1

                int startIndex=-1;

                int endIndex=-1;

                //loopin through the inputstring

                while(inputString[i]!=''){

                                //finding second word

                                if(inputString[i]==' ' || inputString[i]==' '){

                                                if(startIndex==-1){

                                                                //start of second word

                                                                startIndex=i+1;

                                                }else if(endIndex==-1){

                                                                //end of second word

                                                                endIndex=i;

                                                                break;

                                                }

                                               

                                }

                                i++;

                }

               

                if(endIndex==-1){

                                //second word ends at last index

                                endIndex=i;

                }

                if(startIndex!=-1){

                                //there is a second word

                                int j=0,k=0;

                                //copying the second word into outputstring

                                for(j=startIndex;j<endIndex;j++){

                                                outputString[k]=inputString[j];

                                                k++;

                                }

                                //appending a null value at the end

                                outputString[j]='';

                                return outputString;

                }else{

                                //no second word, assigning a NULL value to the pointer

                                outputString=NULL;

                                return outputString;

                }

}

int main(){

                char inputString[150];

                char *ip=inputString;//defining a pointer to input string

                char outputString[100];

                char *op=outputString;//defining a pointer to output string

               

                int passes=0;

                int fails=0;

                //first test

                ip="One Two Three";

                op=secondWord(ip,op);//copying second word

                printf("Function Test Case #1 ");

                printf("Input:                                                     One Two Three ");

                printf("Expected output:                              Two ");

                printf("Output:                                                 %s ",op);

                if(strcmp(op,"Two")==0){

                                passes++;

                                printf("Result:                                                   PASS ");

                }else{

                                fails++;

                                printf("Result:                                                   FAIL ");

                }

               

                //second test

               

                ip="ONE";

                op=secondWord(ip,op);

                printf("Function Test Case #1 ");

                printf("Input:                                                     ONE ");

                printf("Expected output:                              NULL ");

                if(op==NULL){

                                printf("Output:                                                 NULL ");

                                printf("Result:                                                   PASS ");

                                passes++;

                }else{

                                printf("Output:                                                 %s ",op);

                                fails++;

                                printf("Result:                                                   FAIL ");

                }

               

                //test 3

               

                //re assigning the pointer to change the current NULL value in the pointer

                op=&outputString;

                ip="Hello World";

                op=secondWord(ip,op);

                printf("Function Test Case #1 ");

                printf("Input:                                                     Hello World ");

                printf("Expected output:                              World ");

                printf("Output:                                                 %s ",op);

                if(strcmp(op,"World")==0){

                                passes++;

                                printf("Result:                                                   PASS ");

                }else{

                                fails++;

                                printf("Result:                                                   FAIL ");

                }

               

                //you can add more tests below as you like. Make sure you re assign the

                //pointer once it has been assigned NULL value

               

                printf("Total passes: %d Total fails: %d ",passes,fails);

                return 0;

}

/*OUTPUT*/

Function Test Case #1

Input:                          One Two Three

Expected output:                Two

Output:                         Two

Result:                         PASS

Function Test Case #1

Input:                          ONE

Expected output:                NULL

Output:                         NULL

Result:                         PASS

Function Test Case #1

Input:                          Hello World

Expected output:                World

Output:                         World

Result:                         PASS

Total passes: 3

Total fails: 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote