需要用到哪个函数
编写函数void doublestr(char s[]),接收一个字符串,进行字符数组变换,要求把字符串的每个字符都重复一次。例如接收字符出 abcde,字符数组变换结果aabbccddee。 #include <stdio.h>#include <stdlib.h>
#include <string.h>
void double_str(char s[]) {
size_t size = strlen(s);
char *p = s + size - 1;
char *q = s + (size * 2) - 1;
q = '\0';
while(p >= s) {
*q-- = *p;
*q-- = *p--;
}
}
int main(void) {
char buff;
strcpy(buff, "hello");
double_str(buff);
puts(buff);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void double_str(char s[]) {
char *p = strdup(s);
char *q = s;
for(size_t i = 0; p; ++i) {
*q++ = p;
*q++ = p;
}
*q = '\0';
free(p);
}
int main(void) {
char buff;
strcpy(buff, "hello");
double_str(buff);
puts(buff);
return 0;
}
C里面我没有好用的正则表达式库
用C++的了
#include <iostream>
#include <string>
#include <regex>
using std::cout, std::endl;
using std::string;
using std::regex, std::regex_replace;
const string double_str(const string &s) {
regex pattern(R"(.)");
return regex_replace(s, pattern, R"($0$0)");
}
int main(void) {
string s = "hello";
cout << double_str(s) << endl;
return 0;
}
页:
[1]