帮我看下应该怎么调整,谢谢!
本帖最后由 有爱的日子 于 2023-12-5 19:41 编辑C Primer Plus编程练习第11章第5题,提示错误,帮看下怎么调整。
#include <stdio.h>
#define SIZE 80
char* string_char(char* st, char c);
int main(int argc, char *argv[])
{
char source;
char dest =' ';
char *position;
printf("Enter a String: ");
fgets(source,SIZE, stdin);
while (dest != EOF)
{
printf("Enter a char to find (EOF for Quit):");
while ((dest = getchar()) == '\n')
{
continue;
}
if((position = string_char(source,dest)) != NULL)
{
printf("Found the char %c in the %p\n",*position,position);
}
else
{
printf("Char %c not found.Try another?\n",dest);
}
return 0;
}
char* string_char(char* st, char c)
{
while (*st != '\0')
{
if(*st == c)
{
return st;
}
else
{
st++;
}
}
return NULL;
}
本帖最后由 yes2 于 2023-12-6 16:24 编辑
少了一个大括号
下次编译报错最好把报错信息一起贴上来。#include <stdio.h>
#define SIZE 80
char* string_char(char* st, char c);
int main(int argc, char *argv[])
{
char source;
char dest =' ';
char *position;
printf("Enter a String: ");
fgets(source,SIZE, stdin);
while (dest != EOF)
{
printf("Enter a char to find (EOF for Quit):");
while ((dest = getchar()) == '\n')
{
continue;
}
if((position = string_char(source,dest)) != NULL)
{
printf("Found the char %c in the %p\n",*position,position);
}
else
{
printf("Char %c not found.Try another?\n",dest);
}
}// <===少了一个大括号
return 0;
}
char* string_char(char* st, char c)
{
while (*st != '\0')
{
if(*st == c)
{
return st;
}
else
{
st++;
}
}
return NULL;
} 本帖最后由 有爱的日子 于 2023-12-6 17:59 编辑
yes2 发表于 2023-12-6 16:23
少了一个大括号
下次编译报错最好把报错信息一起贴上来。#include
#define SIZE...
好的,感谢!
页:
[1]