吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 164|回复: 2
收起左侧

[求助] 帮我看下应该怎么调整,谢谢!

[复制链接]
有爱的日子 发表于 2023-12-5 19:39
本帖最后由 有爱的日子 于 2023-12-5 19:41 编辑

C Primer Plus编程练习第11章第5题,提示错误,帮看下怎么调整。
Dingtalk_20231205193849.jpg


#include <stdio.h>
#define SIZE 80
char* string_char(char* st, char c);
int main(int argc, char *argv[])
{
    char source[SIZE];
    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:23
本帖最后由 yes2 于 2023-12-6 16:24 编辑

少了一个大括号
下次编译报错最好把报错信息一起贴上来。
[C++] 纯文本查看 复制代码
#include <stdio.h>
#define SIZE 80
char* string_char(char* st, char c);
int main(int argc, char *argv[])
{
    char source[SIZE];
    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:57
本帖最后由 有爱的日子 于 2023-12-6 17:59 编辑
yes2 发表于 2023-12-6 16:23
少了一个大括号
下次编译报错最好把报错信息一起贴上来。[mw_shl_code=cpp,true]#include
#define SIZE  ...

好的,感谢!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-10 09:13

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表