SML# - Tutorial/015-2 Diff

  • Added parts are displayed like this.
  • Deleted parts are displayed like this.

[ [[トップ |Tutorial]]| [[目次|Tutorial/000]] | [[前ページ|Tutorial/015-1]] | [[ 次ページ |Tutorial/016]] ]
----
!!! 論理積を用いたstringCompare関数の定義

int stringCompare(char* s1,  char* s2) {
   while (*s1 != '\0' && *s2 != '\0')
     { if (*s1 != *s2) {
        break;
       }
       else {s1++; s2++;}
     };
   if (*s1 == '\0' && *s2 == '\0')
     { return(1);}
   else
     { return(0);}
}

!!! 大文字小文字の違いを無視して文字列を比較する関数

int toLower(char c) {
   if (c >= 'A' && c <= 'Z') {
    c = c + ('a' - 'A');
   };
   return (c);
}
int stringCompare(char* s1,  char* s2) {
   int result = 1;
   while (*s1 != '\0' || *s2 != '\0')
     { if (toUpper(*s1) != toUpper(*s2)) {
         result = 0;
         break;
       }
       else {s1++; s2++;}
     };
   return(result);
}

!!! 文字列を辞書式順序で比較する関数

int strComp(char* S1,  char* S2) {
   int result = 0;
   while (*S1 != '\0' || *S2 != '\0')
     { if (*S1 < *S2) {
         result = -1;
         break;
       }
       else if (*S1 > *S2) {
         result = 1;
         break;
       }
       else {S1++; S2++;}
     }
   return(result);
}

!!! 文字列の置き換え

int substitute(char result[], char *S1, char c, char *S2) {
   int i, pos = 0;
   while (*S1 != '\0') {
     if(*(S1) == c) {
       pos += strcopy3(result + pos, S2);
       S1++;
     } else {
       result[pos++] = *(S1++);
     }
   }
   result[pos] = '\0';
   return(pos);
}
以下はテスト用のコード.
main(){
   char to[100];
   char *s1 = " % % % ";
   char *s2 = "SML#";
   substitute(to, s1, '%', s2);
   printf("The result of substitute(to, s1, '#', s2) is :%s\n", to);
}
----
[ [[トップ |Tutorial]]| [[目次|Tutorial/000]] | [[前ページ|Tutorial/015-1]] | [[ 次ページ |Tutorial/016]] ]