C++
1コード 2 3#include <stdio.h> 4#include <ctype.h> 5 6int stricomp(const char str1[], const char str2[]){ 7 8 int i, count1 = 0, count2 = 0, a; 9 10 while(str1[count1]) 11 count1++; 12 while(str2[count2]) 13 count2++; 14 15 printf("%d\n%d\n",count1,count2); 16 17 if( count1 != count2) 18 return 0; 19 20 for( i = 0; i < count1; i++){ 21 if( tolower( str1[i] ) != tolower( str2[i] )) 22 return 0; 23 } 24 25 return 1; 26 } 27 28 29 int main(void){ 30 char str1[100], str2[100], file1[10], file2[10]; 31 32 printf("ファイル1を入力してください:"); 33 scanf("%s", file1); 34 35 printf("ファイル2を入力してください:"); 36 scanf("%s", file2); 37 38 FILE *fp1,*fp2; 39 40 if((fp1 = fopen( file1, "r"))!=NULL){ 41 while( fgets(str1,100,fp1) != NULL ){ 42 printf("%s",str1); 43 } 44 } 45 46 if((fp2 = fopen( file2, "r"))!=NULL){ 47 while( fgets(str2,100,fp2) != NULL ){ 48 printf("%s",str2); 49 } 50 } 51 52 fclose( fp1); 53 fclose( fp2); 54 55 printf("比較した結果は%d\n",stricomp( str1, str2)); 56 57 return 0; 58}
このように、二つのファイルを読み込み、文字列に代入し、その文字列を比較するプログラムを作ったが、正しい結果を得られませんでした。(文字列比較する関数を自分で作る課題です。)
実行結果
#include <stdio.h>
int main(void){
printf("Hello, World 2014\n");
return 0;
}
#include <stdio.h>
int main(void){
printf("Hello, World 2014\n");
return 0;
}
2 //count1
2 //count2
比較した結果は1
#include <stdio.h>
int main(void){
printf("Hello, World 2014\n");
return 0;
}
#include <stdio.h>
int main(void){
printf("Hello, World 2013\n");
return 0;
}
2 //count1
2 //count2
比較した結果は1
このように、countを数えるときはうまくいけませんでした。ちなみに、文字列だけの比較なら、このstricomp関数でうまくいけました。
教えてくださいm_m
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/18 04:09
2017/12/18 04:21 編集