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

来源:互联网 发布:如何做人力资源矩阵图 编辑:程序博客网 时间:2024/05/27 14:13

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


方法一 :

#include <stdio.h>#include <string.h>#include <stdlib.h>#define N 100void 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;}
原创粉丝点击