字符串比较 [英] String Comparision

查看:80
本文介绍了字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(对不起,如果这次发布两次)

大家好。我正在努力学习C.作为一个简单的练习,我试着编写一个

代码,它将使用指针比较两个字符串。

但是我没有得到正确的结果。

在跟踪程序时,我注意到字符串s和t得到

在comp函数中以某种方式修改了因此我得到了

不正确的结果。

有人可以指出错误吗?

#include< stdio.h>


int comp(char * s,char * t)

{

for(; * s == * t; s ++,t ++);

if(* s ==''\''')

return 0;

其他

返回(* s - * t);

}


int main(无效)

{

char str [] =" Hello" ;;

char str2 [] =" Hello";


if(comp(str,str2)!= 0)

printf(" strings are different);

else

printf(字符串相同);

返回0;

}

解决方案

" user34"写道:


大家好。我正在努力学习C.作为一个简单的练习,我试着编写一个

代码,它将使用指针比较两个字符串。

但是我没有得到正确的结果。

在跟踪程序时,我注意到字符串s和t在comp函数中以某种方式得到修改

因此我得到了错误

结果。

任何人都可以指出错误吗?


#include< stdio.h>


int comp(char * s,char * t)

{

for(; * s == * t; s ++,t ++);

if(* s ==''\''')

返回0;

else

return(* s - * t);



无论结果如何,你都会在检查完第一个字符后返回。

重新思考你的逻辑。


}


int main(无效)

{

char str [] =" ; Hello" ;;

char str2 [] =" Hello";


if(comp(str,str2)!= 0)

printf(" strings are different);

else

printf(" strings are same);

return 0;

}



osmium写道:


" user34"写道:


>大家好。我正在努力学习C.作为一个简单的练习,我试着写一个
代码,用比较两个字符串。
然而我没有得到正确的结果。
跟踪程序我注意到字符串s和t在comp函数中得到修改
因此我得到的结果不正确。
任何人都可以指出错误吗?

#include< stdio。 h>

int comp(char * s,char * t)
{
for(; * s == * t; s ++,t ++);
if (* s ==''\''')
返回0;
其他
返回(* s - * t);



检查第一个字符后,无论结果如何,都会返回。

重新思考你的逻辑。



你能详细说明吗? for循环应该解析字符串,直到两个字符串中的$ / b $ b字符不同,是不是?


user34写道,On 29 / 04/07 17:53:


(对不起,如果这次发布两次)

大家好。我正在努力学习C.作为一个简单的练习,我试着编写一个

代码,它将使用指针比较两个字符串。

但是我没有得到正确的结果。

在跟踪程序时,我注意到字符串s和t得到

在comp函数中以某种方式修改了因此我得到了

不正确的结果。

有人可以指出错误吗?



首先,您是否复制并粘贴或重新输入?我怀疑你重新输入并且正确输入了
你正在运行的副本中出错了什么。


#include< stdio.h>



他们昨天停止为空白区域充电,所以你可以通过以下方式让

更容易阅读

#include< stdio.h>


int comp(char * s,char * t)



因为这个函数不是为了修改字符串所以更好地使它变得明确

int comp(const char * s,const char * t)


这也意味着如果你试图修改

,编译器会惹你生气。


{

for(; * s == * t; s ++,t ++);



很容易找不到上面的分号,对于人类读者来说

类似

for (; * s == * t; s ++,t ++)继续;



for(; * s == * t; s ++,t ++)/ *什么都不做* /;



for(; * s == * t; s ++,t ++){}


此外,如果两个字符串完全相同怎么办?在这种情况下,你将会在字符串结尾处停止
但是会继续进入

狂野的蓝色边缘。

for( ; * s&& * s == * t; s ++,t ++)继续;


if(* s ==''\''')

返回0;

其他

返回(* s - * t);



您不需要括号

返回* s - * t;


另外,在某些嵌入式系统中,一个字节(以C语言表示)为16位或

较大且与int相同,这可能会失败。

return(* s * t)? 1:-1;


}


int main(无效)

{

char str [] =" Hello" ;;

char str2 [] =" Hello" ;;


if(comp( str,str2)!= 0)

printf(" strings are different);

else

printf(" strings are same" ;);

返回0;

}



你应该在最后添加换行符字符串。一个简单的方法是

你没有使用任何格式说明符是调用puts而不是printf。

-

Flash Gordon


(Sorry if this gets posted twice)
Hi all. I am trying to learn C.As a simple exercise i tried to write a
code which would compare two strings using pointers.
However i am not getting the correct result.
On tracing the program i noticed that the strings "s" and "t" get
modified somehow in the comp function and as a result i am getting
incorrect results.
Can anyone please point out the error?
#include<stdio.h>

int comp(char *s,char *t)
{
for(;*s==*t;s++,t++);
if(*s==''\0'')
return 0;
else
return (*s -*t);
}

int main(void)
{
char str[]="Hello";
char str2[]="Hello";

if(comp(str,str2)!=0)
printf("strings are different");
else
printf("strings are same");
return 0;
}

解决方案

"user34" writes:

Hi all. I am trying to learn C.As a simple exercise i tried to write a
code which would compare two strings using pointers.
However i am not getting the correct result.
On tracing the program i noticed that the strings "s" and "t" get modified
somehow in the comp function and as a result i am getting incorrect
results.
Can anyone please point out the error?
#include<stdio.h>

int comp(char *s,char *t)
{
for(;*s==*t;s++,t++);
if(*s==''\0'')
return 0;
else
return (*s -*t);

You return after examining the first char, no matter how it turns out.
Rethink your logic.

}

int main(void)
{
char str[]="Hello";
char str2[]="Hello";

if(comp(str,str2)!=0)
printf("strings are different");
else
printf("strings are same");
return 0;
}



osmium wrote:

"user34" writes:

>Hi all. I am trying to learn C.As a simple exercise i tried to write a
code which would compare two strings using pointers.
However i am not getting the correct result.
On tracing the program i noticed that the strings "s" and "t" get modified
somehow in the comp function and as a result i am getting incorrect
results.
Can anyone please point out the error?
#include<stdio.h>

int comp(char *s,char *t)
{
for(;*s==*t;s++,t++);
if(*s==''\0'')
return 0;
else
return (*s -*t);


You return after examining the first char, no matter how it turns out.
Rethink your logic.

Can you elaborate? The for loop should parse the string until the
characters in the two strings differ ,is''nt it?


user34 wrote, On 29/04/07 17:53:

(Sorry if this gets posted twice)
Hi all. I am trying to learn C.As a simple exercise i tried to write a
code which would compare two strings using pointers.
However i am not getting the correct result.
On tracing the program i noticed that the strings "s" and "t" get
modified somehow in the comp function and as a result i am getting
incorrect results.
Can anyone please point out the error?

First off, did you copy and paste or retype? I suspect you retyped and
in the process typed correctly what is wrong in the copy you ran.

#include<stdio.h>

They stopped charging for white space yesterday, so your could make the
above easier to read by doing
#include <stdio.h>

int comp(char *s,char *t)

Since this function is not meant to modify the strings it would be
better to make it explicit
int comp(const char *s,const char *t)

This will also mean the compiler will bitch at you if you try to modify
them.

{
for(;*s==*t;s++,t++);

It is easy to fail to spot the semicolon above, for the human reader
something like
for (; *s==*t; s++,t++) continue;
Or
for (; *s==*t; s++,t++) /* do nothing */ ;
Or
for (; *s==*t; s++,t++) {}

Also, what if both strings are entirely the same? In that case you will
not stop at the end of the strings but will continue going in to the
wild blue yonder.
for (; *s && *s==*t; s++,t++) continue;

if(*s==''\0'')
return 0;
else
return (*s -*t);

You do not need the parenthesis
return *s - *t;

Also, on some embedded systems where a byte (in C terms) is 16 bits or
larger and the same size as an int this could fail.
return (*s *t) ? 1 : -1;

}

int main(void)
{
char str[]="Hello";
char str2[]="Hello";

if(comp(str,str2)!=0)
printf("strings are different");
else
printf("strings are same");
return 0;
}

You should add a newline on to the end of the strings. One easy way as
you are not using any format specifiers is to call puts instead of printf.
--
Flash Gordon


这篇关于字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆