串比较

问题:一定要记得保存指针的地址。

代码:

#include <iostream>
using namespace std;
int main()
{
	char s1[]="helloworld";
	char s2[]="hellochina";
	char *c1=s1;
	char *c2=s2;
	char *c3;
	int length1=0,length2=0;
	while(s1[length1]!='\0')
		    length1++;
	while(c2[length2]!='\0')
		    length2++;
	if(length1<length2)
	{
		cout<<"串s1小于串s2"<<endl;
	}
	else if(length1>length2)
	{
		cout<<"串s1大于串s2"<<endl;
	}
	else
	{
		for(int i=0;i<length1;i++)
		{
			if(c1[i]!=c2[i])
			{
				if(c1[i]>c2[i])
					cout<<"串s1大于串s2"<<endl;
				else
					cout<<"串s1小于串s2"<<endl;
				break;
			}
			else
				continue;
		}
	}
	return 0;
}

运行结果:

posted @ 2013-04-26 14:26  xshang  阅读(179)  评论(0编辑  收藏  举报