プロが教える店舗&オフィスのセキュリティ対策術

文字列の比較で、
文字列の長さが60以上の時、再入力を促します。
while文を使って書いてみたのですが、
文字列Bの入力の前に、もう一度意味もなく
「文字列Aを入力===>」が表示されたり。
文字列Aのほうが小さいのに「Aのほうが大きい」と
表示されるようになったり、変な感じです。
どなたかご指摘・ご指導のほどよろしくお願いします。

int main(void)
{
char moji1[100];
char moji2[100];

while(strlen(moji1)>60){
    printf("文字列Aを入力===>"); scanf("%80s" ,moji1);
}
while(strlen(moji2)>60){
    printf("文字列Bを入力===>"); scanf("%80s" ,moji2);
   }

if(compare(moji1,moji2)>0){
printf("===AはBより大きい===\n");
}
else if(compare(moji1, moji2)<0){
printf("===AはBより小さい===\n");
}
else if(compare(moji1, moji2)==0){
printf("===AとBは等しい===\n");
}
return 0;
}
int compare(char *x, char*y)
{
while(*x==*y && *x!=0){

x++;
y++;
}
return (*x-*y);
}

A 回答 (5件)

こんばんわ



コンパイラの種類によっては、バッファを確保時にゼロクリアしないものもありますので、
バッファを確保した場合は、必ず、バッファをクリアすることをお勧めします。
入力文字列は、80文字以内ですが、入力された文字列の最後のNULLが、
保障されていないため、strlenや、compareで、バッファオーバーランを起こしている可能性があります。

上記問題は、compare内部で、while の前で、printf()で、それぞれの、文字列を表示してみることで、
確認できると思います。

もし、バッファクリアの問題であれば、解決方法としては、単純に、
char moji1[100]={0};
char moji2[100]={0};

でOKだと思います。

また、80文字未満で入力した場合に、最初のenterキーが無視される可能性もあるので、
ご参考までに、scanfの説明のURLをつけておきます

参考URL:http://ja.wikipedia.org/wiki/Scanf
    • good
    • 0

#4です。



ふと、思い出して、見てみると・・・駄目ですね、私のサンプル^^;

while(strlen(moji1)>60){

は、

while(*moji1 == '\0' || strlen(moji1)>60){

にしてください。つまり、ゼロクリアした後に、1文字目が、NULLか、
または、長さが60文字を超える場合には、ループに入ると思います。同様に、

while(*moji2 == '\0' || strlen(moji2)>60){

となると思います。不十分で済みませんでした。m(_ _)m
    • good
    • 0

 


return (*x-*y);
は、
return (unsigned char)*x - (unsigned char)*y;
 
    • good
    • 0

#include <stdio.h>


#include <string.h>

#define MAX 64

int get_str(char str[], int n)
{
char *p;

if(!fgets(str, n, stdin)) return 0;
if(p = strchr(str, '\n')) *p = '\0';
else{
int c;

while((c = getchar()) != '\n' && c != EOF) ;
}
return 1;
}

int main(void)
{
char s[MAX];

while(1){
printf("文字列Aを入力===>");
if(!get_str(s, MAX)) break;
if(strlen(s) < 60) break;
puts("文字多すぎ、再入力");
}
return 0;
}
    • good
    • 0

#include <stdio.h>


#include <string.h>

static int g_debug = 1;

static void debug(char *message) {
  if (g_debug) {
    printf("DEBUG - %s\n", message);
  }
}

int main(void)
{
  char moji1[100];
  char moji2[100];
  char buffer[1000];

/*
  while(strlen(moji1)>60){ ← ×初期化していない変数を参照してはいけません。
    printf("文字列Aを入力===>"); scanf("%80s" ,moji1);
  }
  while(strlen(moji2)>60){ ← ×初期化していない変数を参照してはいけません。
    printf("文字列Bを入力===>"); scanf("%80s" ,moji2);
  }
*/
  do {
    printf("文字列Aを入力===>"); scanf("%80s" ,moji1);
  } while(strlen(moji1) > 60);
  
  do {
    printf("文字列Bを入力===>"); scanf("%80s" ,moji2);
  } while(strlen(moji2) > 60);
  
  /*
   * 確認の為moji1、moji2を表示
   */
  if (g_debug) {
    sprintf(buffer, "moji1{%s}", moji1);
    debug(buffer);
    sprintf(buffer, "moji2{%s}", moji2);
    debug(buffer);
  }
  
  if(compare(moji1,moji2)>0){
    printf("===AはBより大きい===\n");
  } else if(compare(moji1, moji2)<0){
    printf("===AはBより小さい===\n");
  } else if(compare(moji1, moji2)==0){
    printf("===AとBは等しい===\n");
  }
  
  return 0;
}

int compare(char *x, char*y)
{
  while(*x==*y && *x!=0){
    x++;
    y++;
  }
  
  return (*x-*y);
}
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!