Answer to Exercise 4-1, page 71
Solution by Rick Dearman
Write the function strrindex(s,t) , which returns the position of the rightmost occurrence of t in s , or -1 if there is none.
/* Test driver by Richard Heathfield
* Solution (strrindex function) by Rick Dearman
*/
#include <stdio.h>
/* Write the function strrindex(s,t), which returns the position
** of the rightmost occurrence of t in s, or -1 if there is none.
*/
int strrindex( char s[], char t )
{
int i;
int count = -1;
for(i=0; s[i] != '\0'; i++)
{
if(s[i] == t)
{
count = i;
}
}
return count;
}
typedef struct TEST
{
char *data;
char testchar;
int expected;
} TEST;
int main(void)
{
TEST test[] =
{
{"Hello world", 'o', 7},
{"This string is littered with iiiis", 'i', 32},
{"No 'see' letters in here", 'c', -1}
};
size_t numtests = sizeof test / sizeof test[0];
size_t i;
char ch = 'o';
int pos;
for(i = 0; i < numtests; i++)
{
pos = strrindex(test[i].data, test[i].testchar);
printf("Searching %s for last occurrence of %c.\n",
test[i].data,
test[i].testchar);
printf("Expected result: %d\n", test[i].expected);
printf("%sorrect (%d).\n", pos == test[i].expected ? "C" : "Inc", pos);
if(pos != -1)
{
printf("Character found was %c\n", test[i].data[pos]);
}
}
return 0;
}
'The C Programming Language' 카테고리의 다른 글
Chapter 4 - Functions and Program Structure 3 (0) | 2009.03.27 |
---|---|
Chapter 4 - Functions and Program Structure 2 (0) | 2009.03.27 |
Chapter 3 - Control Flow 6 (0) | 2009.03.25 |
Chapter 3 - Control Flow 5 (0) | 2009.03.25 |
Chapter 3 - Control Flow 4 (0) | 2009.03.25 |