Announcement

Collapse
No announcement yet.

Comparazione di due stringhe

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Comparazione di due stringhe

    Il programma compara due stringhe immesse dall'utente e verifica se esse sono uguali o meno.

    PHP Code:
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     
     * Il programma verifica se due stringhe sono uguali o meno
     
     *
     
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     

    #include <st***.h>

    int stringhe_uguali(char *str1char *str2);
    int main (void)

    {
    char str_1[20];
    char str_2[20];

    printf("Inserisci la prima stringa:");
    scanf("%s"str_1);

    printf("Inserisci la seconda stringa:");
    scanf("%s"str_2);

    if (
    stringhe_uguali(str_1str_2)==1) {
    printf("Uguali!!\n");
    }

    if (
    stringhe_uguali(str_1str_2)==2) {
    printf("Disuguali!!\n");
    }
        
    return 
    0;
    }

    int stringhe_uguali(char *str1char *str2)

    {
    int i// Contatore

    for (i=0; *(str1+i)!='\0' || *(str2+i)!='\0'i++) { // Comparo stringhe
    if (*(str1+i)!=*(str2+i)) {  // Caratteri diversi!
    return 2;
    }
    }

    return 
    1;



  • #2
    Questa parte non mi piace poichè la scanf va al di la della dimensione data dell'array
    PHP Code:
    printf("Inserisci la prima stringa:"); scanf("%s"str_1);   printf("Inserisci la seconda stringa:"); scanf("%s"str_2); 
    Questa parte della funzione è ottima, all'inizio sembra essere buggata ma riesce a gestire tutti i casi, ottimo!
    PHP Code:
    for (i=0; *(str1+i)!='\0' || *(str2+i)!='\0'i++) { // Comparo stringhe if (*(str1+i)!=*(str2+i)) {  // Caratteri diversi! return 2; } }   return 1; 
    | VFX Artist, C++ Programmer, HW Overclocker | Web: xgiovio.com Email: xgiovio@gmail.com Twitter: @xgiovio

    Comment


    • #3
      questa è la mia versione ma la tua è più veloce e leggibile
      PHP Code:
      /*
       *
       * DELLA SERIE "LO HANNO PRESO"
       *
       *
       * Verifichiamo se due stringhe sono uguali
       *
       * Versione 0.1 Build 0 purtroppo
       *
       * ps. defo farmi un nfo figo
       * ps2. funzione migliorabile e abbreviabile
       */
      ///////////////////////////////////////header
      #include <st***.h>

      /////////////////////////////////////// prototypes
      int stringhe_uguali (char *achar *b);
      ////////////////////////////////////// main
      int main(void){
          
      char *a="ciao";
          
      char *b="ciaof";
          
          if (
      stringhe_uguali(a,b)==1){
              
      printf("Uguali\n");
          }else{
              
      printf("Disuguali\n");
          }
          return 
      0;
      }

      //////////////////////////////////////////// funzione verifica stringhe uguali////////////////////////////////////
      int stringhe_uguali (char *achar *b){
          
      int i;
          
      ////////////////////////// verifca se cisono puntatori nulli/////////////////////////////////////////
          
      if (a==NULL || b==NULL){
              return 
      0;
          }else{
              
      ////////////////////////////// verifica fino al primo tappo se sono uguali/////////////////
              
      for (i=0;*(a+i)!= && *(b+i)!=0;i++){
                  if (*(
      a+i)!=*(b+i)){
                      return 
      0;
                  }
              }
              
      //////////////////////////// verifica se il tappo della prima c'è anche nella seconda///
              
      if (*(a+i)==*(b+i)) {
                  return 
      1;
              }else{
                  return 
      0;
              }
          }

      | VFX Artist, C++ Programmer, HW Overclocker | Web: xgiovio.com Email: xgiovio@gmail.com Twitter: @xgiovio

      Comment

      Working...
      X

      Google Profile


      My name is Giovanni Di Grezia, but people call me xgiovio.

      Here is my homepage:.

      I'm a VFX Artist and Software Developer.

      Giovanni Di Grezia