C语言实现在字符串中插入空格


C语言实现在字符串中插入空格


方法一 :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 100
void Insert(char *s);

int main()
{
    char str[N];
    printf("Input a string:");
    gets(str);
    Insert(str);
    printf("Insert results:%s\n", str);
    return 0;
}

void Insert(char *s)
{
    char str[N];
    char *t = str;
    strcpy(t, s);
    for (; *t != '\0'; s++, t++)
    {
        *s = *t;
        s++;
        *s = ' ';
    }
    *s = '\0';      /* 在字符串s的末尾添加字符串结束标志 */
}

方法二:

#include <stdio.h>

int main()
{
    char name[100];
    char *p = name;

    printf("请输入你的姓名:");
    scanf("%s", name);

    while (*p != '\0')
    {
        putchar(*p);
        putchar(' ');
        p++;
    }

    return 0;
}


这里写图片描述
扫码关注作者个人技术公众号,有关技术问题后台回复即可,不定期将有学习资源分享


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM