希望各位大佬给点思路
有一字符串,内有若干个字符,再输入一字符,要求程序在该字符串的首端中插入该字符。要求:分别定义3个外部函数用来输入字符串、插入字符、输出字符串。#include <stdio.h> #include <string.h> int main() { void input(char *str, size_t n); void output(char *str); void insert(char *str, const char c); char c, str[12]; //多2个位置保存插入字符和\0 printf("请输入字符串\n"); input(str, 10); printf("请输入插入的字符\n"); input(&c, 1); insert(str, c); output(str); return 0; } void input(char *str, size_t n) { char ch; size_t i = 0; for (; i < n; i++) { ch = getchar(); if (ch == '\n') break; *(str + i) = ch; } if (n > 1) *(str + i) = '\0'; } void output(char *str) { printf("%s\n", str); } void insert(char *str, const char c) { size_t len = strlen(str) + 1; char *p = str + len; while (len) { *p = *(p - 1); p--; len--; } *p = c; }