I think you you use Turbo C, so string class wont work with your compiler, because Turbo C++ is old compiler and supports AT&T C++ not ANSI C++.
And yes you should also not use gets() and puts() functions. Also clrscr() and getch() are also not standardised.
Here's the code. I have removed some unnecessary variables.
#include<string.h>
#include<conio.h>
#include<stdio.h>
int main(void)
{
clrscr();
char name1[20],name2[20];
int *ptr;
gets(name1);
gets(name2);
int s1 = strlen(name1);
int s2 = strlen(name2);
(s1<=s2)?ptr=&s1:ptr=&s2;
int x, flag=0;
for( x=0;x<*ptr;x++)
{
int ch = (int)name1[x], _ch = (int)name2[x]; // takes one character
if(ch<97) { ch+= 32; } // converts to lowercase
else if(_ch<97) { _ch+= 32; }
// if bot characters are not equal, loop is terminated.
// flag is -1 for name2 and 1 for name2 and 0 if equal
if(ch!=_ch)
{
if(ch>_ch)flag=-1; else flag=1;
break;
}
}
if(flag==1)
puts(name1);
else if(flag==-1)
puts(name2);
getch();
return 0;
}
vishesh
Nearly a Posting Virtuoso
1,381 posts since Oct 2006
Reputation Points: 85
Solved Threads: 42
strcmp() doesn't interest you...oh well.:rolleyes:
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Ankit, like Iamthwee said, strcmp ( ) coupled with qsort ( ) function should work out to be fine in your case.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
>Here is a program that might help you
Somehow I kind of doubt that when the code doesn't even compile. :rolleyes:
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
>Then I suggest you get a new compiler
I suggest that you fix the 2 typos that you made.
/* Converts upper case letters to lower case */
void toLower(char *str) {
int len = strlen(str)-1, i;
for (i=0;i<len;i++)
/* 'name' has never been declared, perhaps you meant 'str'? */
if (!(name[i]>=97 && name[i] <= 122))
name[i] += 32;
}
int cmpstr(char *str1, char *str2) {
int len1, len2, i;
len1 = strlen(str1);
len2 = strlen(str2);
for (i=0;i<len1 && i<len2;i++) {
if (str1[i] < str2[i])
return 1;
/* change 'str' to 'str1' */
else if (str[i] > str2[i])
return 2;
}
return 0;
}
Really, when posting code snippets, the least you can do is make sure it works under normal circumstances. The corrections I showed makes it compile and run, although the code snippet still doesn't take into account numbers.
[edit] I have made a couple of other minor fixes to your toLower() function:
/* Converts upper case letters to lower case */
void toLower(char *str) {
int len = strlen(str), i; /* removed the -1 part... why skip the last character? */
for (i=0;i<len;i++)
if ((str[i]>='A' && str[i] <= 'Z')) /* only lowercases letters now*/
str[i] += 32;
}
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
>Only lowercases letters now???? The other one did handle lower case letters I just !=notted the result of the condition
No, I used "lowercases" as a verb. Thus, it only converts (or "lowercases") uppercase letters now, not any non-lowercase letter.
And this is just my opinion, but: I don't really see why using numbers in place of characters makes the code any clearer.
>Umm.... Lol.... I do make sure.
Being unable to compile code is not a good excuse for posting lousy code. If you are unable to check the code with a compiler, you'd be better off not posting anything. (You can actually get away with posting code with syntax errors here on the forum as long as you warn the person, but that's ONLY for the forums -- code snippets should be always compile.)
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Lazaro, make sure your code is up to the specified standards like Joey said. I see a lot of beginner style programming which may confuse other people trying to learn the language.
for (i=0;i<len1 && i<len2;i++) {
if (str1[i] < str2[i])
return 1;
else if (str[i] > str2[i])
return 2;
}
return 0 ;
}
It should be something like: (not the exact implementation)
{
for (i=0;i<len1 && i<len2;i++) {
if (str1[i] < str2[i])
return str1 [i] - str2 [i] ;
else if (str[i] > str2[i])
return str1 [i] - str2 [i]
}
return 0 ;
}
though I would even modify the loop to make it better, but then again, its okay.
This is not how string comparision function works. The string comparision functions returns the ASCII difference between the strings if they are unequal while a zero if they are equal.
Why try to write a half baked comparision function when the library itself provides for its implementation.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733