Hello.
I have been asked to write a function which Return the minimum index of
the nodes with a lexigraphically larger name".
But I am confused that what is lexigraphically

Link

If you use C's strcmp() function it will return an integer > 0 if string1 is lexicographically larger than string2. Example: "Z" is greater than "A" because A comes first in the English alphabet and Z comes last. Example2: "A" less than "Z" so strcmp() will return some number < 0, and third example "A" == "A" so strcmp() will return 0.

lexigraphically - I take that to mean strings sorted or compared. Is that how your nodes are named? Perhaps a bit more information would help.

Thank you for your reply.
My node use English name such as Ada, Chris,John...

If compare "Ada" and "John" , which one is lexigraphically larger name?

Basically you will compare the strings character by character.

So start with comparing "A" to "J", since J is higher up than A, "John" is lexicographically larger.

I understand now. Thank you

I have new problem about compare "ada" and "john"

the command "strcmp" seem cannot direct compare the first digit of a in ada and j in john.
Is there other command to compare them?

Thank you

This is the C++ forum, so why would you use strcmp?

You can use string objects. Include <string> and you can compare 2 string objects lexigraphically with the string.compare(string) method.

I'm not near a c++ compiler atm, but the implementation will looks something like this-

#include<iostream>
#include <string>

using namespace std;

int main(){

  string a ("John");
  string b ("Mark");
  
  int result = a.compare(b);

  cout << "result of comparison between John and Mark is: " << result << "\n"; 

  cin.get();
  return 0;
}

Edit: This isn't to trash on AD's post. C is very useful, and if performance isn't an issue then consider using the char-array--encapsulating class string.

if all you care to compare is the first character, just access it directly. Consider:

char n1[32] = "ada";
char n2[32] = "john";
if( n1[0] > n2[0] )
   cout << "n1 has the larger first char";
else
   cout << "n2 has a first char larger or equal to n1's first char";

If using string types, you can still use the array access brackets to get the first char, or the .at(0) member function.

Well, yeah, you can look at the first character, but that will not distinguish "Adam" from "Anthony". If the first characters are equal, then it's necessary to look at the second .... and then third, and so on - which is exactly what strcmp() does.

As a rough rule, in most character sets (at least, as far as English speakers are concerned);

- numbers are less than letters
- Uppercase letters are less than lower case ("Adam" comes before "adam", unless a case-insensitive comparison is performed with stricmp()).

The relative order of punctuation, whitespace, and graphical characters varies with character set, but normally the default character set makes sense to the locale (eg an English speaker will tend to use a character set that places whitespace less than common punctuation which is less than letters or numbers).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.