自己写一个strcmp函数。结果不太正确。

一起来C吧 2011-03-12 11:29:15
#include<stdio.h>

strcmp(char *p1,char *p2)
{
int i=0;
while(*(p1+i++)==*(p2+i++))
return (*(p1+i)-*(p2+i));

}

int main()
{
int m;
char str1[20],str2[20],*p1,*p2;
printf("input two stings:\n");
scanf("%s",str1);
scanf("%s",str2);
p1=&str1[0];
p2=&str2[0];
m=strcmp(p1,p2);
printf("result:%d\n",m);
return 0;
}
如题。当输入两个都是单个字符比较时。好像返回的值不正确。请指点一下。程序哪里有问题。谢谢
...全文
349 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
emailtome 2011-03-13
  • 打赏
  • 举报
回复
我贴个现成的别BS我

/* Copyright (C) 1991, 1996, 1997, 2003 Free Software Foundation, Inc.
This file is part of the GNU C Library.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */

#include <string.h>
#include <memcopy.h>

#undef strcmp

/* Compare S1 and S2, returning less than, equal to or
greater than zero if S1 is lexicographically less than,
equal to or greater than S2. */
int
strcmp (p1, p2)
const char *p1;
const char *p2;
{
register const unsigned char *s1 = (const unsigned char *) p1;
register const unsigned char *s2 = (const unsigned char *) p2;
unsigned reg_char c1, c2;

do
{
c1 = (unsigned char) *s1++;
c2 = (unsigned char) *s2++;
if (c1 == '\0')
return c1 - c2;
}
while (c1 == c2);

return c1 - c2;
}
libc_hidden_builtin_def (strcmp)

Roy_Smiling 2011-03-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 linuxbirdman 的回复:]

C/C++ code
int strcmp(char *p1,char *p2)
{
if(!p1 || !p2)
{
printf("illeg pointer\n");
exit(0);
}

while(*(p1)!= '\0' && *(p2)!= '\0')
{
if(*p1!=*……
[/Quote]

done a good job!
AnYidan 2011-03-13
  • 打赏
  • 举报
回复
while(*(p1+i++)==*(p2+i++)) // 假设此时 i = 5
return (*(p1+i)-*(p2+i)); // 此时 i = 6

是你的意思吗?
碎碎念 2011-03-13
  • 打赏
  • 举报
回复
strcmp(char *p1,char *p2)
{
int i=0;
while(*(p1+i++)==*(p2+i++))
return (*(p1+i)-*(p2+i));

}
lz这样写...strcmp返回值类型都没写- -

我也写个...
int strcmp(char* p1,char* p2)
{
if(strlen(p1)!=strlen(p2))
return 0;
int len=strlen(p1);
int rst;
for(int i=0;i<len;i++)
{
if(p1[i]!=p2[i])
{
rst=0;
break;
}
else
rst=1;
}
return rst;
}
ufozhoujie 2011-03-13
  • 打赏
  • 举报
回复
LZ啊,我不得不批评你了····这写得什么哟!
第一有明显的语法错误;
第二貌似逻辑不够清晰;
第三编程不规范额···对于自增的处理,你的确是在考验编译器!
quwei197874 2011-03-13
  • 打赏
  • 举报
回复
用unsigned转换后再比较.
nanbazhangbiao 2011-03-13
  • 打赏
  • 举报
回复
楼主,你这是在考研编译器。。不同的编译器,结果会不同的。
while(*(p1+i++)==*(p2+i++))
对于p1+i++,编译器处理不一样
masmaster 2011-03-13
  • 打赏
  • 举报
回复
这是我编写的比较两个字串练习(还没有学到指针)。


int cmpstr (char s1[],char s2[]) {
int i=0;
while(s1[i]!='\0'&&s2[i]!='\0') {
if(s1[i]>s2[i]) {
return 1;
}
if(s1[i]<s2[i]) {
return -1;
}
i++;
}
if(s1[i]=='\0'&&s2[i]=='\0') {
return 0;
}
}
兴文哥 2011-03-13
  • 打赏
  • 举报
回复
哦,错老,不好意思!
#include <stdio.h>
int strcmp(char *p1,char *p2)
{
while(*p1++==*p2++);
return *p1-*p2;
}
兴文哥 2011-03-13
  • 打赏
  • 举报
回复
#include <stdio.h>
int strcmp(char *p1,char *p2)
{
while(*p1++=*p2++);
return *p1-*p2;
}
crjwlaq 2011-03-13
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 freefice 的回复:]

引用楼主 yuyan_c 的回复:
#include<stdio.h>

strcmp(char *p1,char *p2)
{
int i=0;
while(*(p1+i++)==*(p2+i++))
return (*(p1+i)-*(p2+i));

}

int main()
{
int m;
char str1[20],str2[20],*p1,*p2;
……
[/Quote]

这个补充的不错
crjwlaq 2011-03-13
  • 打赏
  • 举报
回复
你那返回的差值是 anscii码的差值



eternalxlm 2011-03-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 yuyan_c 的回复:]

while(*(p1+i++)==*(p2+i++))
return (*(p1+i)-*(p2+i));

[/Quote]

你的return在while循环里,所以按你的程序只要前2个字母一样就会返回0。
单字符的字符串你都会返回0,因为你return的是'\0'-'\0'。
庄鱼 2011-03-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 yuyan_c 的回复:]
#include<stdio.h>

strcmp(char *p1,char *p2)
{
int i=0;
while(*(p1+i++)==*(p2+i++))
return (*(p1+i)-*(p2+i));

}

int main()
{
int m;
char str1[20],str2[20],*p1,*p2;
printf("input two st……
[/Quote]
while(*(p1+i)== *(p2+i))i++;
sxqinge 2011-03-13
  • 打赏
  • 举报
回复

int strcmp(char* str1, char* str2)
{
ASSERT( ( str1 != NULL ) && ( str2 != NULL ) );
if ( *str1 != *str2 )
return ( (*str1) - (*str2) );
else
{
if ( *str1 == '\0' && *str2 == '\0' )
return 0;
else
return strcmp( str1+1, str2+1 );
}
}
sxqinge 2011-03-13
  • 打赏
  • 举报
回复
啊,没看清楼主的问题,我以为是COPY呢!
sxqinge 2011-03-13
  • 打赏
  • 举报
回复

void strcpy(char* Dest, char* Src)
{
ASSERT((Dest != NULL) && (Src != NULL) );
while( *Src != '\0' && *Dest != '\0' )
{
*Dest++ = *Src++;
}
}
LinuxBirdMan 2011-03-12
  • 打赏
  • 举报
回复
int strcmp(char *p1,char *p2)
{
if(!p1 || !p2)
{
printf("illeg pointer\n");
exit(0);
}

while(*(p1)!= '\0' && *(p2)!= '\0')
{
if(*p1!=*p2)
return ((*p1)-(*p2));
else
{
p1++;
p2++;
continue;
}
}
if(*p1== '\0' && *p2=='\0')
return 0;
else
return ((*p1)-(*p2));
}
hrx1989 2011-03-12
  • 打赏
  • 举报
回复
strcmp就错了吧。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧