AcWing 765. 字符串加空格
原题链接
简单
作者:
预定调和
,
2024-03-17 23:23:14
,
所有人可见
,
阅读 11
看很少c语言版本的 作为补充
c 语言版本
#include <stdio.h>
#include <string.h>
void addSpaces(char* str) {
int len = strlen(str);
char newStr[2 * len + 1]; // 新字符串的长度为原字符串长度的两倍加上一个字符用于存储结尾的'\0'
int j = 0;
for (int i = 0; i < len; i++) {
newStr[j++] = str[i];
newStr[j++] = ' ';
}
newStr[j] = '\0'; // 添加结尾的'\0'
printf("%s", newStr);
}
int main() {
char str[200];
fgets(str, 200, stdin );
addSpaces(str);
return 0;
}
cpp
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str;
getline(cin, str);
for (int i = 0; i < str.size(); i ++)
{
printf("%c ", str[i]);
}
}