Question
/* This function takes two equal length c-style strings and determines which of them has a Q. The address of the string with a Q
/*
This function takes two equal length c-style strings and determines
which of them has a Q. The address of the string with a Q should be
set in the output field. If neither string contains a Q, the output
address should be set to NULL.
Remember that in C, all strings are terminated with a '\0' character
so that's what you should check to see if you're at the end of the
string.
Also remember you can treat pointers like arrays so if you want to get
the 5th character of a string, you can do either of these:
char* letters = "abcde";
char second_letter = letters[1]; //fine
char second_letter_with_pointer_math = *(letters + 1); //also fine
printf("letters %c %c ", second_letter, second_letter_with_pointer_math);
You can assume:
1. The strings are equal length
2. At most one string will contain a Q
You can use C string search functions if you wish, but I think it's
easier to just write a little for loop.
Look at the test cases for example of usage
*/
void string_with_q(char *s1, char* s2, char** output) {
}
void test_string_with_q(CuTest *tc) {
char* s1 = "abQ";
char* s2 = "abc";
char* output;
string_with_q(s1, s2, &output);
CuAssertPtrEquals(tc,s1,output);
s1 = "xyz";
string_with_q(s1, s2, &output);
CuAssertPtrEquals(tc,NULL,output);
//a few more test cases in a slightly shorter style
string_with_q("12345","zzQzz",&output);
CuAssertStrEquals(tc,"zzQzz",output);
string_with_q("Q2345","zzzzz",&output);
CuAssertStrEquals(tc,"Q2345",output);
string_with_q("","",&output);
CuAssertPtrEquals(tc,NULL,output);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started