【发布时间】:2020-11-09 00:56:08
【问题描述】:
刚开始尝试使用 C,来自 Java 背景,我在尝试删除字符串的一部分时遇到了问题。这个的基本逻辑是我有一个字符串(我发现它是 C 中的一个字符数组,非常酷!),一旦在遍历这个字符串时满足某个条件,我想删除其余的细绳。例如,如果我的字符串是“hello world!”,并且我将条件设置为空格,我想删除该空格后面的所有内容,所以只需返回“hello”。我有一个想法,记下满足条件的索引,并创建第二个数组并填充它,然后删除前一个数组,但是我确信有更好的方法来做到这一点。如果有人可以提供帮助,将不胜感激,提前谢谢大家!
编辑: 想法是我想接受用户输入,我的具体情况是如果有一个点“。”它有一个“下一行”作为前一个元素和下一个元素,或者一个“下一行”作为前一个元素,一个空值作为下一个参数。所以基本上:
(if string[n] == ".")
{
if((string[n-1]==\n && string[n+1]==\n) || (string[n-1]==\n && string[n+1]==
null)
{ Then remove everything past this point}
}
input:
hello world
this is ok.
.
Everything here will be deleted.
Output:
hello world
this is ok.
.
编辑 2: 到目前为止,谢谢大家的一些很好的建议,但是我仍然遇到他程序的问题,所以在这里我将发布到目前为止的主要方法的代码(只是测试删除字符串部分的其余部分(尚未添加用户输入) )。
//main method
int main(void)
{
char test = "This is a sample text.\
The file will be terminated by a single dot: .\
The program continues processing the lines because the dot (.)\
did not appear at the beginning.\
. even though this line starts with a dot, it is not a single dot.\
The program stops processing lines right here.\
.\
You wont be able to feed any more lines to the program.";
int n =0;
while(test[n] != NULL)
{
if (test[n]=='.')
{
if ((test[n-1]=='\n' && test[n+1]=='\n') || (test[n-1]=='\n' && test[n+1]==NULL))
{
test[n] = '\0';
}
}
n++;
}
printf("%c\n",test);
return 0;
}
这里的想法是,我最终会将字符串逐字发送到插入排序链表函数,并在删除指定的点之后的所有内容后按字母顺序对字符串进行排序。现在的问题是由于某种原因我遇到了错误,如果有人可以帮助解决它们,我将不胜感激。
Errors in main:
345500375/source.c: In function ‘main’:
345500375/source.c:64:17: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
char test = "This is a sample text.\
^~~~~~~~~~~~~~~~~~~~~~~~
The file will be terminated by a single dot: .\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The program continues processing the lines because the dot (.)\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
did not appear at the beginning.\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
. even though this line starts with a dot, it is not a single dot.\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The program stops processing lines right here.\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\
~~
You wont be able to feed any more lines to the program.";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
345500375/source.c:73:15: error: subscripted value is neither array nor pointer nor vector
while(test[n] != NULL)
^
345500375/source.c:75:17: error: subscripted value is neither array nor pointer nor vector
if (test[n]=='.')
^
345500375/source.c:77:21: error: subscripted value is neither array nor pointer nor vector
if ((test[n-1]=='\n' && test[n+1]=='\n') || (test[n-1]=='\n' && test[n+1]==NULL))
^
345500375/source.c:77:40: error: subscripted value is neither array nor pointer nor vector
if ((test[n-1]=='\n' && test[n+1]=='\n') || (test[n-1]=='\n' && test[n+1]==NULL))
^
345500375/source.c:77:61: error: subscripted value is neither array nor pointer nor vector
if ((test[n-1]=='\n' && test[n+1]=='\n') || (test[n-1]=='\n' && test[n+1]==NULL))
^
345500375/source.c:77:80: error: subscripted value is neither array nor pointer nor vector
if ((test[n-1]=='\n' && test[n+1]=='\n') || (test[n-1]=='\n' && test[n+1]==NULL))
^
345500375/source.c:79:19: error: subscripted value is neither array nor pointer nor vector
test[n] = '\0';
^
【问题讨论】:
-
这一切都取决于字符串的存储方式以及您想要实际执行的操作,如果您可以写入它,只需在您希望它结束的字符串中戳一个 '\0'
-
听起来你可能想要
strtok: link to documentation -
是的,很抱歉漏掉了。想法是我想接受用户输入,我的具体情况是如果有一个点“。”它有一个“下一行”作为前一个元素和下一个元素,或者一个“下一行”作为前一个元素,一个 null 作为下一个参数。
-
使用
char test[]创建数组,而不是char。 -
@abdcg 那是因为 NULL 是一个整数,而不是字符类型,我假设您正在寻找空字符,这意味着您可能想要更接近:
test[n] != '\0';。此外,要输出一个以 null 结尾的字符串,您可以使用%s;%c输出单个字符。