字符串比较功能.. !! [英] String Comparison Function.. !!

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

问题描述

我正在编写一个比较两个c字符串的函数..

无法完成该功能..请帮助.. !!

不可预知的输出。 。!!

我这样做.. !!



=>评估字符串1的长度,字符串2

=>使用较小的长度作为循环控制变量

=>如果相同字符的字符串然后比较它们的长度。



如果我对代码进行了一些修改,则发出警告信息并非所有控制路径都返回值。



请帮助我太沮丧了........



I''m writing a function that compare two c-strings..
unable to complete the function .. please help .. !!
unpredictable output .. !!
i''m doing this .. !!

=> evaluate the length of string 1, and string 2
=> using lesser length as a loop control variable
=> if the strings of same characters then comparing their length.

If i made some modification with the code, gives warning message "not all control paths return value.???"

Please help I''m too much frustrated nOw........

#include <cstring>
#include <iostream>

using namespace std;

int length(char a[])
{
	int temp = 0;
	for (int i = 0; i !='\0' ; i++)
	{
		temp ++;
	}

	return temp;
}

int compare(char a[], char b[])
{
	int a1, a2, var, x;
	a1 = length(a);
	a2 = length(b);
	if (a1 > a2)
	{
		var =  a2;
	}
	else
		var = a1;
	for (int i = 0; i < var; i++)
	{
		if (a[i]==b[i])
		
			x = 0;
		
		else if (a[i]<b[i])
		
			return -1;
		
		else if (a[i]>b[i])
		
			return 1;
		
	}
	if (x==0)
	{
		if (a1>a2)
		
			return 1;
		
		if (a2>a1)

			return -1;

	}
	return x;
}

int main()
{
	char discard;
	const int size = 30;
	char a[size], b[size];
	cin.get(a, size+1);
	cin.get(discard);
	cin.get(b, size+1);
	length(a); length(b);
	cout << compare(a, b);
	system("Pause");
	return 0;
}

推荐答案

参见粗体文字



see bold text

int length(char a[])
{
    int temp = 0;
    for (int i = 0; a[i] !='\0' ; i++)
    {
        temp ++;
    }

    return temp;
}


Quote:

=>评估字符串1的长度,字符串2

=>使用较小的长度作为循环控制变量

=>如果相同字符的字符串然后比较它们的长度。

=> evaluate the length of string 1, and string 2
=> using lesser length as a loop control variable
=> if the strings of same characters then comparing their length.





类C字符串是零终止的,因此你通常不需要知道提前他们的长度(你可以在比较过程中检查''\'''。



在每次迭代时你都应该停止比较: />

  • 两个字符串的相应字符不同。
    • 两个字符串中的一个终止(或两者都终止) 。


    代码中有一些错误。我不打算马上给你解决方案,但希望你自己找到它 - 这会更有趣。所以我想引导你找到那些东西。



    功能 长度

    正如Style-z指出的那样,将i与''\''进行比较并不会有所帮助。 ''\'''将转换为0,你所做的只是比较i == 0.他已经展示了解决方案,所以就是这样。



    函数比较

    是否有可能将for-loop和x NOT设置为0? (是的,有!试着找出发生的情况)。在这种情况下x将具有什么价值(未定义?)。



    您的比较功能将起作用,但到目前为止它并不是最佳的。您将对字符串进行三次传递,每次传递确定长度,一次在for循环中。尝试一次通过;这是可能的,代码将比现在更小更快。



    功能 main

    您再次犯了与上一篇文章相同的错误。如果告诉cin.get数组a的大小为 size + 1 ,则get会相信并写入缓冲区的末尾并销毁其他变量!并且get不会采用换行符!



    一旦你解决了所有问题,我们可能会考虑让你的代码对其他人可读。但这是下一步。
    There are a couple of bugs in your code. I am not going to give you the solution right away, but want you to find it yourself -- that''s much more fun. So I am trying to guide you to finding those things.

    Function length:
    As Style-z pointed out correctly, comparing i with ''\0'' is not going to help. ''\0'' will translate into 0 and all you do is compare i == 0. He has already shown the solution, so that is that.

    Function compare:
    Is there any chance that you leave the for-loop and x NOT being set to 0? (yes, there is! try to figure out in which case that happens). And what value will x have in that case (undefined?).

    Your compare function will work, but by far it is not optimal. You will be doing three passes over the strings, one each determining the length and one in the for-loop. Try to do it in one pass; it''s possible and the code will be smaller and faster than what you have now.

    Function main:
    You are again making the same mistake as in your last post. If you tell cin.get that the array a has a size of size+1, get is going to believe that and write past the end of your buffer and destroy other variables! And get is not going to take a newline!

    Once you solved all that we might look at making your code readable for others. But that''s the next step.


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

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