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
", str);
    return 0;
}

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

方法二:

#include <stdio.h>

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

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

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

    return 0;
}


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

博客园:https://www.cnblogs.com/newtol 微信公众号:Newtol 【转发请务必保留原作者,否则保留追责权利】
原文地址:https://www.cnblogs.com/newtol/p/10159149.html