/* esStringheStrcmp.cpp

Scrivere il corpo della funzione
  int my_strcmp(const char* s1, 
      const char* s2)
che confronta due stringhe.

La funzione restituisce
    0 se le stringhe sono identiche
    numero <0 se s1 e' alfabeticamente 
	          minore di s2
    numero >0 se s1 e' alfabeticamente 
	          maggiore di s2

("alfabeticamente minore" e' in riferimento 
alle codifiche ASCII)


 Procedimento:
  -confronto i caratteri contenuti nelle 
   stringhe e appena trovo che  le due 
   stringhe hanno caratteri differenti, 
   restituisco +1 o -1
   
  -se tutti i caratteri sono uguali, 
   devo controllare che entrambe
   le stringhe abbiano stessa lunghezza 
   (una stringa potrebbe essere
   prefissa dell'altra)  
 
******************************************/

#include <iostream>
// #include <cstring>  // scommentare questa riga
                       // per poter usare le funzioni strlen, strcpy e strcmp
using namespace std;

int my_strcmp(const char* s1, const char* s2){
  int i = 0;	
  while(s1[i]!='\0' && s2[i]!='\0') {		
    if(s1[i] < s2[i]) 
      return -1; 
    else 
      if(s1[i] > s2[i])
        return 1;        
    i++;
  }

  if(s1[i]!='\0' && s2[i]=='\0')
    return 1;                
  else 
    if(s1[i]=='\0' && s2[i]!='\0')
      return -1;     
	
  return 0; // le due stringhe sono uguali	
}

int main(){
	char str1[] = "cobalto";
	char str2[] = "colore";
	cout<<my_strcmp(str1, str2)<<endl;

	// In alternativa si poteva utilizzare la funzione strcmp della libreria <cstring>
	// cout << strcmp(str1, str2) << endl;

	return 0;
}
