C에서 사용자로부터 문자열을 가져 오는 프로그램을 작성했습니다 (50자를 한계로 함). 대문자로 된 문자를 upper
으로, 소문자를 lower
으로 지정하면 결국 해당 문자열 (upper
)을 인쇄합니다. 내 문제는 문자열을 입력 할 때 한 문자열 만 인쇄하는 것입니다. 예를 들어 문자열이 위쪽 문자로 시작하는 경우 upper
이 인쇄됩니다. 두 개가 아닌. 당신은 당신의 배열 모두에 대한 반복자로 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;
}
하위 변수와 상위 변수에 대해 동일한 색인 변수 j를 사용하고 있습니다. 둘 다 다른 변수를 사용하십시오. – Ayush
또한'> 'A'' ->'> ='A'' – BLUEPIXY
[ctype.h는 isupper와 islower를 제공합니다] (https://en.wikipedia.org/wiki/C_character_classification#Overview_of_functions) 현재 로케일. – Schwern