I am having a problem in sorting by name. for eg: if given two names Sameer and Sean, I want to print Sameer and not Sean. It needs to check character by character. I have to extend this to a greater no. of names but I need to get the basic right first. Here is the code i have written:

#include<string.h>
#include<conio.h>
#include<stdio.h>
int main(void)
{
 clrscr();
 int flag,x=0;
 char name1[20],name2[20],name3[20],ch,*ptr,*ptr2;
 gets(name1);
 gets(name2);
 flag=0;
  ch=97;
  for(x=0;x<(strlen(name1)) && x<(strlen(name2)) && flag==0;x++)
  {
   ch=97+x;
   ptr = strrchr(name1,ch);
   ptr2 = strrchr(name2,ch);
   if(ptr==ptr2)
   {
    flag=0;
   }
   if(ptr==0 && ptr2!=0)
   {
    strcpy(name3,name1);
    flag=1;
   }
   if(ptr2==0 && ptr!=0)
   {
    strcpy(name3,name2);
    flag=1;
   }
   if(ptr<ptr2)
   {
    strcpy(name3,name1);
   }
   if(ptr>ptr2)
   {
    strcpy(name3,name2);
   }
  }
 printf(" %s",name3);
 getch();
 return 0;
}

Some one pls help me out!

It is not good practice to use strlen in a for loop, especially for loops that must loop many times because strlen iterates through the array of characters one-by-one every time called (i.e. every loop) and takes up a lot of processor time. You could get the strlen then just used the variables.

Good luck, LamaBot

It is not good practice to use strlen in a for loop, especially for loops that must loop many times because strlen iterates through the array of characters one-by-one every time called (i.e. every loop) and takes up a lot of processor time. You could get the strlen then just used the variables.

Good luck, LamaBot

Ya rite. thnx. I will keep that in mind. Still waiting for the solution though!:)

for(x=0;x<(strlen(name1)) && x<(strlen(name2)) && flag==0;x++)
  {
   ch=97+x;
   ptr = strrchr(name1,ch);
   ptr2 = strrchr(name2,ch);

What happens if a string doesn't contain a character for ascii 97 to 97+strlen in its name? If strchr encountered a character not located in the string, it'll return NULL. What if the strlen of name1 is larger than name2? You'd be checking 8 char's for name1 and matching character 97+8 to name2 which could contain only 5 char's and even then, not all the ascii char's were checked since you're checking from 97+strlen of a given name, for instance. You could do something like this:

string name1, name2;
int len = name1.length(), len2 = name2.length(), x;
for (x=0;x<len-1 || x<len-1;x++) {
 if (name1[x]>name2[x]) 
  name1.swap(name2)
}

The above uses strings and not character arrays, but it is an example. Last, you might want to convert the string to check on lower case characters in my example, because character at x might be larger in name1 if it is lowercase but the alpha character still comes before x in name2 which is a capital letter and thus will always be smaller than any of its lower case counter parts.

Good luck, LamaBot

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;
}

>Here is a program that might help you
Somehow I kind of doubt that when the code doesn't even compile. :rolleyes:

>Here is a program that might help you
Somehow I kind of doubt that when the code doesn't even compile. :rolleyes:

Then I suggest you get a new compiler ;) Lol.

>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;
}

>Then I suggest you get a new compiler
I suggest that you fix the 2 typos that you made.
/* 'name' has never been declared, perhaps you meant 'str'? */

Yeah, the only compiler I have access to is on a Windows workstation, which I'm not allowed to transport any data electronically. So I simply copy from the compiler IDE to the code posting. But thanks for pointing that out. :mrgreen:

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;
}

Oh thanks! That is much better. I'll even make it better such as formatting :cheesy::

/* Converts upper case letters to lower case */
....
        if ((str[i] >= 65 && str[i] <= 90)) /* Only lowercases letters now???? The other one did handle lower case letters I just !=notted the result of the condition:)*/
 }

Joe, thanks for all your help....
LamaBot

>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.)

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.

>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.

Well considering the context of this debate, any considerate person would clarify such a deceiving addition.

And this is just my opinion, but: I don't really see why using numbers in place of characters makes the code any clearer.

Yeah.... well it is a good thing it is your opinion. :lol:

>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.)

Lousy code? Lol, um ok. You tell that to my colleague who happen to see me swiftly write the code and who checked it. As I specified earlier, as you seemed to have ignored, it did compile sucessfully. Perhaps not on the Linux machine I'm on now, but it did on the XP box, but I can just look the code over to make sure. :) You should be more considerate when make such absolute claims.

Comments
You are alright

>You tell that to my colleague who happen to see me swiftly write the code and who checked it.
I don't care how fast you wrote it. I'd rather have good code that someone took time to check, rather than to read quickly-written code that sucks ass.

>As I specified earlier, as you seemed to have ignored, it did compile sucessfully.
I don't know HOW those errors that I stated previously would compile on anything. And if it really did, the compiler deserves to be thrown out the window, because compiler errors are supposed to alert you to mistakes in your code (at least the ones the compiler can see).

>You should be more considerate when make such absolute claims.
Those errors were pretty absolute to me, and I'm sure you cannot deny the mistakes I already pointed out.

> Well if he is learning then he is a beginner. So what is your point?
So we have to make sure they doesn't pick up wrong programming practices.

All this time you seem to be ignoring, my and Joe's constructive criticism. This is not the way things are done. A working code is not the same as a concise, clear and helpful code. You have to understand that the code which you post in the code snippets section is used and referred by thousands of beginners everyday. A high standard has to be maintained.

Picking at people who have been here for a long time is no solution to the problem. Try to improve yourself.

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.

I'd really appreciate a link to a standard specification either for C (?) or for Daniweb, seriously.

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]; /* Forgot the semi-colon */
    }
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.

Ok, it does not exactly work like that, however, please inform me on how returning 1, 2..... is newbie code? Why because I don't subtract them? Lol, ok. Wow what an advanced technique.

Why try to write a half baked comparision function when the library itself provides for its implementation.

Half baked, it works doesn't it? And because I don't subtract the difference, doesn't render the function half baked. That is absurd.

> I'd really appreciate a link to a standard specification either for C (?) or for Daniweb, seriously.
It comes from practice. Just look around and read the code snippets and posts contributed by experienced people and you would know.

> Ok, it does not exactly work like that, however, please inform me on how returning 1, 2..... is
> newbie code? Why because I don't subtract them?

As for your implementation, read the actual specification of the strcmp function. Making your own functions which performs in a different way only adds to the confusion.

>
Making your own functions which performs in a different way only adds to the confusion.

Wow you've obviously got superior reasoning....:lol: Anyway, I guess I'll starting paying attention to such menial considerations. Regarding the programming suggestions I mean. Lol.

And this is just my opinion, but: I don't really see why using numbers in place of characters makes the code any clearer.

Yeah.... well it is a good thing it is your opinion. :lol:

Well, it's not just your opinion, Joe. I am in complete agreement that
if ((str[i] >= 'a' && str[i] <= 'z'))
is extremely more readable than
if ((str[i] >= 65 && str[i] <= 90))
And it will even work for code on EBCDIC machines... :p

I'd like to hear any dissenting opinion that is sensible...

Comments
Well, that's good to know, thank you. --joeprogrammer

make use of string comparision function strcmp() and any sorting method.....its goin to be simple.....
if in case u r not able to device a solution..... i ll post u the code... try to understand it.....k....

This article has been dead for over six months. Start a new discussion instead.