鱼C论坛

 找回密码
 立即注册
查看: 2072|回复: 1

字符串

[复制链接]
发表于 2019-6-22 16:25:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <ctype.h>
#define SIZE 80

void getword(char *str,int size);

int main(void)
{
        char input[SIZE];
        int num = 3;

        printf("Please enter the MAX size:");
        scanf_s("%d", &num);
        printf("Please enter a string:");
        gets(input);
        getword(input,num);
        puts(input);

        getchar();
        getchar();
        return 0;
}

void getword(char *str,int size)
{
        char *ch;
        ch = str;
        while (!isspace(*ch++));
        if ((ch - str) > size)
                str[size] = '\0';
        else
                str[ch - str] = '\0';
}


求救一下哪里出错,以编译就不行,在gets那里输入不了字符
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-22 16:40:05 | 显示全部楼层
本帖最后由 newu 于 2019-6-22 16:43 编辑

在scanf和gets()之间加个getchar(),这样就能过滤掉scanf输入后那个回车
int main(void)
{
        char input[SIZE];
        int num = 3;

        printf("Please enter the MAX size:");
        scanf_s("%d", &num);
          getchar();
        printf("Please enter a string:");
        gets(input);
        getword(input,num);
        puts(input);

        getchar();
        getchar();
        return 0;
}

这篇文章很清晰:
https://blog.csdn.net/fobdddf/article/details/19479181
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 21:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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