比较数组的每行字符串的前5个字符和astro相等吗
#include <stdio.h>
#include <string.h>
char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;
ret_val = fgets(st, n, stdin);
if (ret_val) //即,ret_val != NULL
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
#include <string.h>
#define LISTSIZE 6
int main()
{
//表示数组里的每个元素都是指向char类型的指针
//带双引号的 "字符串" 本来就相当于指针
const char * list[LISTSIZE] =
{
"astronomy","astounding",
"astrophysics","ostracize",
"asterism","astrophobia"
};
int count = 0;
int i;
//LISTSIZE 行
for (i = 0; i < LISTSIZE; i++)
//比较数组的每行字符串的前5个字符和astro相等吗
if (strncmp(list[i], "astro", 5) == 0)
{
printf("Found: %s\n", list[i]);
count++;
}
printf("The list contained %d words beginning"
" with astro.\n", count);
return 0;
}