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