在我的代码中找不到从字符串中删除字符的错误 #包括 #包括 空隙挤压(字符s[],int c); int main(){ int i,最大值=200,c; 字符s[max]; 对于(i=0;i
您在调用在我的代码中找不到从字符串中删除字符的错误 #包括 #包括 空隙挤压(字符s[],int c); int main(){ int i,最大值=200,c; 字符s[max]; 对于(i=0;i,c,string,C,String,您在调用squence()之前从未终止s,因此它不是有效字符串。因此,此代码具有未定义的行为 那么这个, #include <stdio.h> #include <stdlib.h> void squeeze(char s[], int c); int main(){ int i, max = 200,c; char s[max]; for(i=0; i <max-1 && (c=getchar())!=EOF; +
squence()
之前从未终止s
,因此它不是有效字符串。因此,此代码具有未定义的行为
那么这个,
#include <stdio.h>
#include <stdlib.h>
void squeeze(char s[], int c);
int main(){
int i, max = 200,c;
char s[max];
for(i=0; i <max-1 && (c=getchar())!=EOF; ++i){
s[i] =c;
}
squeeze(s, c);
printf("%s", s);
}
void squeeze (char s[], int c){
int i, j;
for(i=j=0; s[i] != '\0'; i++){
if(s[i] != c){
s[j++] = s[i];
}
}
s[j] = '\0';
}
应该是
squeeze(s, c);
如果要删除字符
'c'
。现在传递的是变量c
,其值为EOF
;这是没有意义的。在调用挤压()
之前,决不会终止s
,因此它不是有效字符串。因此,此代码具有未定义的行为
那么这个,
#include <stdio.h>
#include <stdlib.h>
void squeeze(char s[], int c);
int main(){
int i, max = 200,c;
char s[max];
for(i=0; i <max-1 && (c=getchar())!=EOF; ++i){
s[i] =c;
}
squeeze(s, c);
printf("%s", s);
}
void squeeze (char s[], int c){
int i, j;
for(i=j=0; s[i] != '\0'; i++){
if(s[i] != c){
s[j++] = s[i];
}
}
s[j] = '\0';
}
应该是
squeeze(s, c);
如果要删除字符'c'
。现在传递的是变量c
,其值为EOF
;这没有意义。主要更改如下
squeeze(s, 'c');
主要变化如
squeeze(s, 'c');
如果我理解正确,您正在尝试删除
压缩
中读取的最后一个字符的所有匹配项,除其他外,它失败了,因为您正在传递空终止符
s[I]
asc
tosqueak
。这是行不通的。经过几次修改,它现在删除了上次读取的所有字符:
...
printf("Input string : ");
fgets(s, sizeof s, stdin);
printf("Enter you want to delete characters : ");
c = getchar();
squeeze(s, c);
...
#包括
空隙挤压(字符*s,整数c);
int main(){
int i=0,max=200,c=0;
字符s[max];
while((c=getchar())!=EOF&&c!='\n'&&i0)
挤压(s,s[i-1]);
printf(“\n%s\n\n”,s);
返回0;
}
空挤压(字符*s,整数c){
int i=0,j=0;
而(s[i]){
如果(s[i]!=c){
s[j++]=s[i];
}
i++;
}
s[j]=0;
}
输出
#include <stdio.h>
void squeeze(char *s, int c);
int main(){
int i = 0, max = 200, c = 0;
char s[max];
while ((c = getchar()) != EOF && c != '\n' && i < max-1)
s[i++] = c;
s[i] = 0;
if (i > 0)
squeeze (s, s[i-1]);
printf("\n %s\n\n", s);
return 0;
}
void squeeze (char *s, int c) {
int i = 0, j = 0;
while (s[i]) {
if(s[i] != c){
s[j++] = s[i];
}
i++;
}
s[j] = 0;
}
$./bin/squeak如果我理解正确,您试图删除squeak
中读取的最后一个字符的所有匹配项,除其他外,它失败了,因为您正在传递空终止符
asc
tosqueak
。这是行不通的。经过几次修改,它现在删除了上次读取的所有字符:
...
printf("Input string : ");
fgets(s, sizeof s, stdin);
printf("Enter you want to delete characters : ");
c = getchar();
squeeze(s, c);
...
#包括
空隙挤压(字符*s,整数c);
int main(){
int i=0,max=200,c=0;
字符s[max];
while((c=getchar())!=EOF&&c!='\n'&&i0)
挤压(s,s[i-1]);
printf(“\n%s\n\n”,s);
返回0;
}
空挤压(字符*s,整数c){
int i=0,j=0;
而(s[i]){
如果(s[i]!=c){
s[j++]=s[i];
}
i++;
}
s[j]=0;
}
输出
#include <stdio.h>
void squeeze(char *s, int c);
int main(){
int i = 0, max = 200, c = 0;
char s[max];
while ((c = getchar()) != EOF && c != '\n' && i < max-1)
s[i++] = c;
s[i] = 0;
if (i > 0)
squeeze (s, s[i-1]);
printf("\n %s\n\n", s);
return 0;
}
void squeeze (char *s, int c) {
int i = 0, j = 0;
while (s[i]) {
if(s[i] != c){
s[j++] = s[i];
}
i++;
}
s[j] = 0;
}
$./bin/squeeze另外,它是用c==EOF
调用的,这是没有意义的。当我调用EOF时它不应该终止吗?@diplodocus'\0'
是c的字符串终止符,它必须在字符串的最后一个有效字符之后,以标记结束。这就是squeeze()
中的循环所寻找的!@diplodocus。“但我自己手动调用EOF”,并将其分配给c
。现在使用第二个参数c
(EOF
)调用square
)没有意义,因为s
不会包含它。@diplodocus当然,因为您试图挤出EOF
,一个开头不在s
中的字符(事实上,它根本不是有效的char
-类型值)。您将错误的值作为第二个参数传递给square()
。你没有读注释吗?也许你是想传递字符“c”
。此外,它是用c==EOF
调用的,这是毫无意义的。当我调用EOF时它不应该终止吗?@diplocus“\0”
是c的字符串终止符,它必须在字符串的最后一个有效字符之后,以标记结束。Th这就是你在squeak()
中的循环要寻找的东西!@diplodocus。“但是我自己手动调用EOF”,并将其分配给c
。现在使用第二个参数c
(EOF
)调用squeak
)没有意义,因为s
不会包含它。@diplodocus当然,因为您试图挤出EOF
,一个开头不在s
中的字符(事实上,它根本不是有效的char
-类型值)。您将错误的值作为第二个参数传递给square()
。你没有读评论吗?也许你是想传递'c'
,字符'c'.0)s
不是NUL('\0'
)终止。1)在挤压时s
不包括c(EOF)
你想对调用挤压(s,c)做什么;
?要从字符串中删除哪个字符?@interjay我正在尝试使用挤压(s,c)从字符串中删除字符c,您认为这是什么特定字符?您认为它会删除字母“c”吗"如果是这样的话,你真的应该回到基础上来学习变量是什么。除非你回答我关于要删除哪个字符的问题。看起来你对C语言不太熟悉,只是拼凑了一些你在某处找到的代码。我建议你读一本好书,学习这门语言。0)s
不是吗L('\0'
)终止。1)在挤压s
处不包括c(EOF)
您试图如何处理调用挤压(s,c);
您想从字符串中删除哪个字符?@interjay我正在尝试使用挤压(s,c)从字符串中删除字符c你认为那是什么具体的字符?你认为它会删除字母“c”吗?如果是的话,你真的应该回到基础上来学习变量是什么。除非你回答我关于你想删除哪个字符的问题。看起来你对t几乎不熟悉