使用 getchar 从字符串中间删除换行符
Removing a newline from the middle of a string using getchar
现在我有一个看起来像这样的字符串:
A sentence
with a newline.
我正在通过 控制台输入读取 中的字符串,如下所示:
ch = getchar();
while (ch != '.') {
msg[i] = ch;
i++;
ch = getchar();
}
并且,读入后,我通过在函数中执行此操作并应用于 msg
字符数组来删除存在的空白:
char *remove_white_spaces(char *str) {
int i = 0, j = 0;
while (str[i]) {
if (str[i] != ' ')
str[j++] = str[i];
i++;
}
str[j] = '[=12=]';
return str;
}
我试过遍历它并在 \n
处停止,但这留下了“Asentence”的输出,因为字符串在 \n
设置为 0 时终止。
整个主线:
int main(void) {
char msg[MAX_MSG_LEN+1];
char ch;
int i = 0;
ch = getchar();
while (ch != '.') {
msg[i] = ch;
i++;
ch = getchar();
}
msg[i] = '.';
msg[i + 1] = '[=13=]';
remove_white_spaces(msg);
printf("%s\n", msg);
return 0;
}
您可以使用 isspace
function 来测试和跳过 any/all 白色 space 字符,包括正常的 space 和换行符:
#include <ctype.h> // For definition of "isspace"
char *remove_white_spaces(char *str) {
int i = 0, j = 0;
while (str[i]) {
if (!isspace((unsigned char)(str[i])))
str[j++] = str[i];
i++;
}
str[j] = '[=10=]';
return str;
}
关于将 isspace
的参数转换为 unsigned char
的原因,请参阅 this discussion。
函数删除和替换字符串中的任何字符。
toRemove
- 要删除的字符
addSpace
- 替换为 space
allowMultiple
- 替换更多相邻
时允许多个space
字符
allowEdges
- 允许在开头和结尾添加 spaces
char *removeChars(char *str, const char *toRemove, const int addSpace, const int allowMultiple, int const allowEdges)
{
char *rd = str, *wr = str;
int replaced = 0;
if(rd)
{
while(*rd)
{
if(strchr(toRemove, *rd))
{
if(addSpace)
{
if(!replaced || allowMultiple)
{
if(wr != str || (wr == str && allowEdges))
{
*wr++ = ' ';
replaced = 1;
}
}
}
}
else
{
*wr++ = *rd;
replaced = 0;
}
rd++;
}
if(allowEdges) *wr = 0;
else
while((wr - 1) > str)
{
if(*(wr - 1) == ' ') {*(wr - 1) = 0; wr--;}
else break;
}
}
return str;
}
int main(void)
{
char str[] = "%%%%%A sentence\n\n\nwith!@#$%^a newline.%%%%%%%";
printf("`%s`\n", removeChars(str,"\n!@#$%^", 1, 0, 0));
}
按照@MarkBenningfield 的建议,我执行了以下操作并检查了 '\n' 并将其替换为 space.
while (ch != '.') {
msg[i] = ch;
i++;
ch = getchar();
if (ch == '\n') {
ch = ' ';
}
}
现在我有一个看起来像这样的字符串:
A sentence
with a newline.
我正在通过 控制台输入读取 中的字符串,如下所示:
ch = getchar();
while (ch != '.') {
msg[i] = ch;
i++;
ch = getchar();
}
并且,读入后,我通过在函数中执行此操作并应用于 msg
字符数组来删除存在的空白:
char *remove_white_spaces(char *str) {
int i = 0, j = 0;
while (str[i]) {
if (str[i] != ' ')
str[j++] = str[i];
i++;
}
str[j] = '[=12=]';
return str;
}
我试过遍历它并在 \n
处停止,但这留下了“Asentence”的输出,因为字符串在 \n
设置为 0 时终止。
整个主线:
int main(void) {
char msg[MAX_MSG_LEN+1];
char ch;
int i = 0;
ch = getchar();
while (ch != '.') {
msg[i] = ch;
i++;
ch = getchar();
}
msg[i] = '.';
msg[i + 1] = '[=13=]';
remove_white_spaces(msg);
printf("%s\n", msg);
return 0;
}
您可以使用 isspace
function 来测试和跳过 any/all 白色 space 字符,包括正常的 space 和换行符:
#include <ctype.h> // For definition of "isspace"
char *remove_white_spaces(char *str) {
int i = 0, j = 0;
while (str[i]) {
if (!isspace((unsigned char)(str[i])))
str[j++] = str[i];
i++;
}
str[j] = '[=10=]';
return str;
}
关于将 isspace
的参数转换为 unsigned char
的原因,请参阅 this discussion。
函数删除和替换字符串中的任何字符。
toRemove
- 要删除的字符addSpace
- 替换为 spaceallowMultiple
- 替换更多相邻
时允许多个space 字符allowEdges
- 允许在开头和结尾添加 spaces
char *removeChars(char *str, const char *toRemove, const int addSpace, const int allowMultiple, int const allowEdges)
{
char *rd = str, *wr = str;
int replaced = 0;
if(rd)
{
while(*rd)
{
if(strchr(toRemove, *rd))
{
if(addSpace)
{
if(!replaced || allowMultiple)
{
if(wr != str || (wr == str && allowEdges))
{
*wr++ = ' ';
replaced = 1;
}
}
}
}
else
{
*wr++ = *rd;
replaced = 0;
}
rd++;
}
if(allowEdges) *wr = 0;
else
while((wr - 1) > str)
{
if(*(wr - 1) == ' ') {*(wr - 1) = 0; wr--;}
else break;
}
}
return str;
}
int main(void)
{
char str[] = "%%%%%A sentence\n\n\nwith!@#$%^a newline.%%%%%%%";
printf("`%s`\n", removeChars(str,"\n!@#$%^", 1, 0, 0));
}
按照@MarkBenningfield 的建议,我执行了以下操作并检查了 '\n' 并将其替换为 space.
while (ch != '.') {
msg[i] = ch;
i++;
ch = getchar();
if (ch == '\n') {
ch = ' ';
}
}