난이도 : 🥉브론즈Ⅱ
포인트 : a-z, A-Z 범위에 속하는 문자들의 번호매김, char과 int, ASCII코드
KEY WORD : 정수론, 소수 판정, 문자열
#include <stdio.h>
#include <string.h>
int alpha_to_num(char alpha);
int word_num_sum(char word[]);
int is_prime(int val);
///////////////////////////////////////////
//////////////////////////////////////////
int main(void)
{
char word[21];
scanf("%s", word);
if (is_prime(word_num_sum(word)) == 0)
{
printf("It is a prime word.");
}
else
{
printf("It is not a prime word.");
}
return 0;
}
//////////////////////////////////////////
/////////////////////////////////////////
int alpha_to_num(char alpha)
{
if (alpha >= 97)
{
return alpha - 'a' + 1;
}
else
{
return alpha - 'A' + 27;
}
}
int word_num_sum(char word[])
{
int str_len = strlen(word);
int sum = 0;
for (int i = 0; i < str_len; i++)
{
sum += alpha_to_num(word[i]);
}
return sum;
}
int is_prime(int val)
{
int Nprime_cnt = 0;
if (val <= 0)
{
return -1;
}
else
{
for (int i = 2; i < val; i++)
{
if (val % i == 0)
{
Nprime_cnt += 1;
}
}
return Nprime_cnt;
}
}