2017-03-10 4 views
2

Cで文字列を取得するプログラムを作成しました(50文字が上限です)大文字の文字をupperという文字列に、小文字をlowerに、最後にそれらの文字列(upperを最初に)を出力することになっています。私の問題は、文字列を入力するときに文字列が1つだけ(つまり、文字列が上の文字で始まり、upperが印刷される場合)2つではなく、1つの文字列が印刷されることです。あなたの配列の両方のためのイテレータとしてJを使用しているCで文字列の文字を数えて別のグループに割り当てる方法(小文字と大文字)

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <time.h> 
#include <math.h> 

#define MAX_LEN 50 

int main() 
{ 
    char str[MAX_LEN] = { 0 }; 
    char upper[MAX_LEN] = { 0 }; 
    char lower[MAX_LEN] = { 0 }; 
    int i = 0; 
    int j = 0; 

    printf("Enter a string: "); 
    fgets(str, MAX_LEN, stdin); 
    str[strcspn(str, "\n")] = 0; 

    for (i = 0; i < strlen(str); i++) 
    { 
     if (str[i] > 'A' && str[i] < 'Z') 
     { 
      upper[j] = str[i]; 
     } 
     else if (str[i] > 'a' && str[i] < 'z') 
     { 
      lower[j] = str[i]; 
     } 
     j++; 
    } 

    printf("%s", upper); 
    printf("%s", lower); 

    getch(); 
    return 0; 
} 
+4

lowerおよびupper変数に同じインデックス変数jを使用しています。両方に異なる変数を使用します。 – Ayush

+1

また、 '> 'A'' - >'> =' A'' – BLUEPIXY

+1

[ctype.hはisupperとislowerを提供します](https://en.wikipedia.org/wiki/C_character_classification#Overview_of_functions)現在のロケール。 – Schwern

答えて

1

2つのアレイに1つのカウンタを使用します。あなたが記入した配列に関係なく、カウンターをインクリメントします。結果として、処理される最初の文字はではなく、の最初の文字がC文字列NULL終端文字になります。

だから?したがって、printf()を使用すると、%sを使用すると、NULLターミネータを満たした時点で、<stdio.h>のすべての機能と同じように、印刷が中止されます。ところで、あなたはそのライブラリを含めるのを忘れていました。

一つの解決策は、2つのカウンタ、すべてのアレイのいずれかを使用して、我々はただで満たされた配列のカウンタをインクリメントすることであろう。

また、考慮に入れる代わりに>>=を使用する「」あまりにも。同様に 'z'とその資本についても同様です。一緒にすべてを置く

あなたはこのような何かを得る:

#include <string.h> 
#include <time.h> 
#include <math.h> 
#include <stdio.h> // you hadn't include that! 

#define MAX_LEN 50 

int main() 
{ 
    char str[MAX_LEN] = { 0 }; 
    char upper[MAX_LEN] = { 0 }; 
    char lower[MAX_LEN] = { 0 }; 
    int i = 0; 
    int j = 0; // counter for 'upper' 
    int k = 0; // counter for 'lower' 

    printf("Enter a string: "); 
    fgets(str, MAX_LEN, stdin); 
    str[strcspn(str, "\n")] = 0; 

    for (i = 0; i < strlen(str); i++) 
    { 
     if (str[i] >= 'A' && str[i] <= 'Z') // use the equal operator as well for reading 'A' and 'Z' as well 
     { 
      upper[j++] = str[i]; // increment the counter 'j' 
     } 
     else if (str[i] >= 'a' && str[i] <= 'z') // use the equal operator as well for reading 'a' and 'z' as well 
     { 
      lower[k++] = str[i]; // increment the counter 'k' 
     } 
    } 

    // print your strings, but use a newline for aesthetics 
    printf("%s\n", upper); 
    printf("%s\n", lower); 

    return 0; 
} 

が出力:upperlowerため、あなたif-statements

Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
Enter a string: Samaras 
S 
amaras 
0

は、ここに私のコードです。あなたはそれをしません。そうすると、他の配列の最初の部分に '\ 0'を付けることができ、それは書き込まれません。

ので、あなたがそれを行う必要があります:あなたは一つの値を追加するときにのみ

#include <stdlib.h> 
    #include <stdio.h> 
    #include <string.h> 
    #include <time.h> 
    #include <math.h> 

    #define MAX_LEN 50 

    int main() 
    { 
     char str[MAX_LEN] = { 0 }; 
     char upper[MAX_LEN] = { 0 }; 
     char lower[MAX_LEN] = { 0 }; 
     int i = 0; 
     int j = 0, h = 0; 

     printf("Enter a string: "); 
     fgets(str, MAX_LEN, stdin); 
     str[strcspn(str, "\n")] = 0; 

     for (i = 0; i < strlen(str); i++) 
     { 
      if (str[i] >= 'A' && str[i] <= 'Z') 
      { 
       upper[j++] = str[i]; 
      } 
      else if (str[i] > 'a' && str[i] < 'z') 
      { 
       lower[h++] = str[i]; 
      } 
     } 

     printf("%s", upper); 
     printf("%s", lower); 

     return 0; 
    } 

ここでは、あなたのアレイでiteringされているが。 また、コメントに記載されている通り、str[i] >= 'A' && str[i] <= 'Z'はありませんstr[i] > 'A' && str[i] < 'Z'

3

使用異なるインデックス変数を、>=>オペレータを変更し、同様に<<=

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <time.h> 
#include <math.h> 

#define MAX_LEN 50 

int main() 
{ 
    char str[MAX_LEN] = { 0 }; 
    char upper[MAX_LEN] = { 0 }; 
    char lower[MAX_LEN] = { 0 }; 
    int i = 0; 
    int up = 0, low = 0; 

    printf("Enter a string: "); 
    fgets(str, MAX_LEN, stdin); 
    str[strcspn(str, "\n")] = 0; 

    for (i = 0; i < strlen(str); i++) 
    { 
     if (str[i] >= 'A' && str[i] <= 'Z') 
     { 
      upper[up] = str[i]; 
      up++; 
     } 
     else if (str[i] >= 'a' && str[i] <= 'z') 
     { 
      lower[low] = str[i]; 
      low++; 
     } 
    } 

    printf("%s\n", upper); 

    printf("%s", lower); 

    getch(); 
    return 0; 
} 
関連する問題