C++/C++ 기타

C++ 아스키 코드 영문 모음, 자음, 단어 갯수 세기

kimc 2021. 7. 3. 23:47


 

이번 글을 통해 배워갈 내용

  1.  char 배열을 통해 문자에 모음을 찾아 보겠습니다.
  2.  char 배열을 통해 문자의 자음을 찾아 보겠습니다.
  3.  char 배열을 통해 문자의 갯수를 찾아 보겠습니다.

 

영문에서 모음은  a e i o u 라는 단어를 뜻합니다

 

정확한 뜻은 아래를 참조 해주시면 됩니다

https://ko.wikipedia.org/wiki/%EB%AA%A8%EC%9D%8C

 

모음 - 위키백과, 우리 모두의 백과사전

모음(母音, 영어: Vowel) 또는 홀소리는 음성학에서 자음과 차별되는 음운으로, /a/, /e/, /i/,/o/, /u/와 같이 성문(glottis) 위의 어떤 특정 지점에서 공기 압력이 형성될 때 열린 성도를 통해 특별한 장

ko.wikipedia.org

 

"Hello" 라는 단어에는 e 가 한개 o 가 한개 총 두개의 모음이 있습니다.

 

자 그럼

 

컴퓨터로 모음 찾기를 한번 프로그래밍 해보겠습니다.

 

아래 함수와 같이 코딩을 하였으며

전체 프로그램은 글 맨 끝에 있습니다.

 

int countVowelFn(char Arr[])
{
	int VowelCount = 0;

	for(int i=0; Arr[i] != '\0'; i++)
	{ 
		if (isVowel(Arr[i]))
		{
			VowelCount++;
		}
	}
	return VowelCount;
}

bool isVowel(char ch)
{
	if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
		ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
	{
		return true;
	}
	else
	{
		return false;
	}
}

 

영어 단어 소문자 이거나 대문자 a e i o u일 경우

 

카운트 가 1 증가 하며

 

순차적으로 모든 문자를 조회 합니다.

 


자음의 경우 모음을 제외한 나머지 문자라 생각 하시면 편하며 

 

int countConsonentFn(char Arr[])
{
	int i = 0;
	int ConsonentCount = 0;
	for (int i = 0; Arr[i] != '\0'; i++)
	{
		if (isConsonent(Arr[i]))
		{
			ConsonentCount++;
		}
	}
	return ConsonentCount;
}

bool isConsonent(char ch)
{
	if (isVowel(ch))
	{
		return false;
	}
	else if(isLowerCaseAlphabet(ch) || isHigherCaseAlphabet(ch))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool isLowerCaseAlphabet(char ch)
{
	if (ch >= 'a' && ch <= 'z') //97 ~ 122
	{
		return true;
	}
	else return false;
}

bool isHigherCaseAlphabet(char ch)
{
	if (ch >= 'A' && ch <= 'Z') //65 ~ 90
	{
		return true;
	}
	else return false;
}

위에 모음 코드와 연계해서

 

소문자 a 부터 z 대문자 A 부터 Z 까지 문자 중 모음이 아닌 문자의 경우 Count에 1을 더했습니다.

 


글자 갯수도 위에 방법과 비슷한 방법으로 진행 하였으며

 

아래 코드를 보시면

 

공백의 갯수와 비례해서 단어가 있다고 가정하였습니다.

 

 

영문과 공백만 사용되었다고 가정한 코드입니다.

 

int countWord(char Arr[])
{
	int count = 1;
	for (int i=0; (Arr[i] != '\0'); i++)
	{
		if (Arr[i] == ' ' && Arr[i-1] != ' ')
		{
			count++;
		}
		
	}
	return count;
}

 


전체 코드는 다음과 같습니다.

 

#include <iostream>
#include <cstring>
using namespace std;

bool isVowel(char ch);
bool isConsonent(char ch);
bool isLowerCaseAlphabet(char ch);
bool isHigherCaseAlphabet(char ch);


int countVowelFn(char Arr[])
{
	int VowelCount = 0;

	for(int i=0; Arr[i] != '\0'; i++)
	{ 
		if (isVowel(Arr[i]))
		{
			VowelCount++;
		}
	}
	return VowelCount;
}

bool isVowel(char ch)
{
	if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
		ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
	{
		return true;
	}
	else
	{
		return false;
	}
}


int countConsonentFn(char Arr[])
{
	int i = 0;
	int ConsonentCount = 0;
	for (int i = 0; Arr[i] != '\0'; i++)
	{
		if (isConsonent(Arr[i]))
		{
			ConsonentCount++;
		}
	}
	return ConsonentCount;
}

bool isConsonent(char ch)
{
	if (isVowel(ch))
	{
		return false;
	}
	else if(isLowerCaseAlphabet(ch) || isHigherCaseAlphabet(ch))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool isLowerCaseAlphabet(char ch)
{
	if (ch >= 'a' && ch <= 'z') //97 ~ 122
	{
		return true;
	}
	else return false;
}

bool isHigherCaseAlphabet(char ch)
{
	if (ch >= 'A' && ch <= 'Z') //65 ~ 90
	{
		return true;
	}
	else return false;
}

int countLengthFn(char Arr[])
{
	int count = 0;
	char* ch = Arr;
	for (; (*ch != '\0'); ch++)
	{
		count++;
	}
	return count;
}

int countWord(char Arr[])
{
	int count = 1;
	for (int i=0; (Arr[i] != '\0'); i++)
	{
		if (Arr[i] == ' ' && Arr[i-1] != ' ')
		{
			count++;
		}
		
	}
	return count;
}

int main()
{
	char myStr[] = "hello hello hello";

	std::cout << countVowelFn(myStr) << endl;
	std::cout << countConsonentFn(myStr) << endl;
	std::cout << countWord(myStr) << endl;
	return 0;
}

입력된 스트링
hello hello hello

결과
4
6
2

 


 

오늘도 즐거운 코딩하시길 바랍니다 ~ :)

 

참조 및 인용

 

C++ Primer

 

https://stackoverflow.com/questions/47846406/c-fastest-way-to-check-if-char-is-vowel-or-consonant

 

c++ fastest way to check if char is vowel or consonant

I have a problem that involves using backtrack to find a number of "words"(they don't have to be real) with various rules. Some of the rules involve the number of vowels that I can have after each ...

stackoverflow.com

https://stackoverflow.com/questions/3672234/c-function-to-count-all-the-words-in-a-string/44793971

 

C++ function to count all the words in a string

I was asked this during an interview and apparently it's an easy question but it wasn't and still isn't obvious to me. Given a string, count all the words in it. Doesn't matter if they are repeated.

stackoverflow.com


 

728x90