My char array would be looking something like below,
我的char数组看起来像下面的东西,
Org_arr[1first line text------2second line text----3third line-------4--------------------5fith line text------];
where '-' equal to blank spaces
其中' - '等于空格
The above array contains the control code(0, 1, 2, 3..) after every 20 characters starting from 0-th place.
上面的数组包含从第0位开始的每20个字符后的控制代码(0,1,2,3 ..)。
I would like to convert the above text array into below format,
我想将上面的文本数组转换成以下格式,
The blank spaces will be removed and a line feed will be added at the end of each line.
将删除空格,并在每行末尾添加换行符。
Conv_arr[1first line text/n2second line text/n3third line/n4/n5fith line text/n];
Please suggest a good method to implement this,
请建议一个很好的方法来实现这一点,
2 个解决方案
#1
1
the easiest way will be using regular expression to replace pattern "\s?" with "\n"
最简单的方法是使用正则表达式来替换模式“\ s?”用“\ n”
If you don't have access to a regex library, you can do something like this
如果您无法访问正则表达式库,则可以执行此类操作
int print_line_break = 1;
char* Conv_arr = (char*)malloc(sizeof(char) * strlen(Org_arr) + 1);
for(char* c=Org_arr; *c; ++c) {
if (*c == ' ') {
*(Conv_arr++) = *c;
print_line_break = 1;
} else {
// only print 1 '\n' for a sequence of space
if (print_line_break) {
*(Conv_arr++) = '\n';
print_line_break = 0;
}
}
}
free(Conv_arr);
#2
0
This code is ugly and smells funny:
这段代码很难看,闻起来很有趣:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int c;
char *src, *tgt;
char mystr[] = "1first line text "
"2second line text "
"3third line "
"4 "
"5fith line text ";
char *cur = mystr;
char *prev = NULL;
while ( ( c = *cur++ ) ) {
if ( c != ' ' && *cur == ' ' ) {
prev = cur;
continue;
}
if ( c == ' ' && isdigit(*cur) ) {
*prev++ = '\n';
src = cur;
tgt = prev;
while ( ( *tgt++ = *src++ ) );
cur = prev;
}
}
puts( mystr );
return 0;
}
[sinan@archardy]$ ./t
1first line text
2second line text
3third line
4
5fith line text
#1
1
the easiest way will be using regular expression to replace pattern "\s?" with "\n"
最简单的方法是使用正则表达式来替换模式“\ s?”用“\ n”
If you don't have access to a regex library, you can do something like this
如果您无法访问正则表达式库,则可以执行此类操作
int print_line_break = 1;
char* Conv_arr = (char*)malloc(sizeof(char) * strlen(Org_arr) + 1);
for(char* c=Org_arr; *c; ++c) {
if (*c == ' ') {
*(Conv_arr++) = *c;
print_line_break = 1;
} else {
// only print 1 '\n' for a sequence of space
if (print_line_break) {
*(Conv_arr++) = '\n';
print_line_break = 0;
}
}
}
free(Conv_arr);
#2
0
This code is ugly and smells funny:
这段代码很难看,闻起来很有趣:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int c;
char *src, *tgt;
char mystr[] = "1first line text "
"2second line text "
"3third line "
"4 "
"5fith line text ";
char *cur = mystr;
char *prev = NULL;
while ( ( c = *cur++ ) ) {
if ( c != ' ' && *cur == ' ' ) {
prev = cur;
continue;
}
if ( c == ' ' && isdigit(*cur) ) {
*prev++ = '\n';
src = cur;
tgt = prev;
while ( ( *tgt++ = *src++ ) );
cur = prev;
}
}
puts( mystr );
return 0;
}
[sinan@archardy]$ ./t
1first line text
2second line text
3third line
4
5fith line text