Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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........

C++
#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;
}
Posted
Comments
E.F. Nijboer 29-Mar-13 8:55am    
Sounds like a school assignment. By the way, what is the logic in using the lesser length when after that it says that only strings of the same length are compared? If you simply write down the requirements in the right order and without contradictions it should be a breeze.

see bold text

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

    return temp;
}
 
Share this answer
 
Comments
Usman Hunjra 29-Mar-13 11:52am    
nyc ..
thx
Quote:
=> 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-like strings are zero-terminated, hence you typically don't need to know their length in advance (you could check for '\0' while comparison is in progress).

At each iteration you should stop comparing if:
  • the corrensponding characters of the two strings are different.
or
  • one of the two strings terminates (or both of them terminate).
Hence your code might be very compact.
 
Share this answer
 
v2
Comments
nv3 29-Mar-13 10:58am    
You beat me by a couple of minutes :-)
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900