Write a C function called secondWord that has the prototype char* secondWord(cha
ID: 3907784 • 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:
Prompt the user to enter three to five words seperated by spaces or tabs
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 from 3 (part 3 should be a function inside part 4’s completed code)
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
Be able to test more than just this! Also, please try to avoid strtok, I have not learned it yet!!!!
Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char* secondWord(char* inputString, char* outputString){
int count = 0;
char data[100];
int i;
int cnt = 0;
for (i = 0; i<strlen(inputString); i++){
if (isalpha(inputString[i]) && count == 0){
count = 1;
}
else if (!isalpha(inputString[i]) && count == 1){
//printf("--------------------- ");
count = 2;
}
else if (isalpha(inputString[i]) && count == 2){
//printf("-****-------------------- ");
data[cnt] = inputString[i];
cnt++;
}
else if (!isalpha(inputString[i]) && count == 2){
if (cnt > 0){
strcpy(outputString,data);
}
break;
}
}
return outputString;
}
int main(){
char inp[100];
char out[100];
printf("Functional Test of secondWord ");
printf("Tests multiple inputs to the secondWord function to confirm operation. ");
printf("Function Test Case #1 ");
printf("Input: One Two three ");
printf("Expected Output: Two ");
strcpy(inp,"One Two Three");
printf("%s ",secondWord(inp,out));
if (strcmp(secondWord(inp,out),"Two") == 0){
printf("Result : Pass ");
}
else {
printf("Result : Fail ");
}
printf("Function Test Case #2 ");
printf("Input: One Two three ");
printf("Expected Output: Two ");
strcpy(inp,"One Two Three");
printf("%s ",secondWord(inp,out));
if (strcmp(secondWord(inp,out),"Two") == 0){
printf("Result : Pass ");
}
else {
printf("Result : Fail ");
}
printf("Function Test Case #3 ");
printf("Input: One,Two,three ");
printf("Expected Output: Two ");
strcpy(inp,"One Two Three");
printf("%s ",secondWord(inp,out));
if (strcmp(secondWord(inp,out),"Two") == 0){
printf("Result : Pass ");
}
else {
printf("Result : Fail ");
}
printf("Function Test Case #4 ");
printf("Input: One Two three ");
printf("Expected Output: Two ");
strcpy(inp,"One Two Three");
printf("%s ",secondWord(inp,out));
if (strcmp(secondWord(inp,out),"Two") == 0){
printf("Result : Pass ");
}
else {
printf("Result : Fail ");
}
printf("Function Test Case #5 ");
printf("Input: One - Two - three ");
printf("Expected Output: Two ");
strcpy(inp,"One Two Three");
printf("%s ",secondWord(inp,out));
if (strcmp(secondWord(inp,out),"Two") == 0){
printf("Result : Pass ");
}
else {
printf("Result : Fail ");
}
printf("Function Test Case #6 ");
printf("Input: One#Two#three ");
printf("Expected Output: Two ");
strcpy(inp,"One Two Three");
printf("%s ",secondWord(inp,out));
if (strcmp(secondWord(inp,out),"Two") == 0){
printf("Result : Pass ");
}
else {
printf("Result : Fail ");
}
printf("Function Test Case #7 ");
printf("Input: One!Two!three ");
printf("Expected Output: Two ");
strcpy(inp,"One Two Three");
printf("%s ",secondWord(inp,out));
if (strcmp(secondWord(inp,out),"Two") == 0){
printf("Result : Pass ");
}
else {
printf("Result : Fail ");
}
printf("Function Test Case #8 ");
printf("Input: One*Two*three ");
printf("Expected Output: Two ");
strcpy(inp,"One Two Three");
printf("%s ",secondWord(inp,out));
if (strcmp(secondWord(inp,out),"Two") == 0){
printf("Result : Pass ");
}
else {
printf("Result : Fail ");
}
printf("Function Test Case #9 ");
printf("Input: One%Two%three ");
printf("Expected Output: Two ");
strcpy(inp,"One Two Three");
printf("%s ",secondWord(inp,out));
if (strcmp(secondWord(inp,out),"Two") == 0){
printf("Result : Pass ");
}
else {
printf("Result : Fail ");
}
printf("Function Test Case #10 ");
printf("Input: One&Two&three ");
printf("Expected Output: Two ");
strcpy(inp,"One Two Three");
printf("%s ",secondWord(inp,out));
if (strcmp(secondWord(inp,out),"Two") == 0){
printf("Result : Pass ");
}
else {
printf("Result : Fail ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.