다음은 알파벳, 숫자 및 특수 문자를 찾기 위해 구현한 논리입니다. -
for(number=0;string[number]!='\0';number++) {// for loop until endof string if(string[number]>='a'&&string[number]<='z'||string[number]>='A'&&string[number]<='Z') //checking alphabets in string{ alphabets=alphabets+1; //counting alphabets //alphabets++; } else if(string[number]>='0'&&string[number]<='9'){ //checking numbers in string digits=digits+1; //counting numbers //digits++; } else { special=special+1; //counting special characters //special++; } }
다음 프로그램은 문자열에서 알파벳, 숫자 및 특수 문자의 총 수를 식별하는 것입니다 -
예시
#include<stdio.h> #include<ctype.h> void main(){ //Declaring integer for number determination, string// int number; char string[50]; int alphabets=0; int digits=0; int special=0; //Reading User I/p// printf("Enter the string :"); gets(string); for(number=0;string[number]!='\0';number++){ if(string[number]>='a'&&string[number]<='z'||string[number]>='A'&&string[number]<='Z'){ alphabets=alphabets+1; //alphabets++; } else if(string[number]>='0'&&string[number]<='9'){ digits=digits+1; //digits++; } else{ special=special+1; //special++; } } //Printing number of alphabets, number of digits, number of special characters// printf("The number of alphabets in the string is : %d\n",alphabets); printf("The number of digits in the string is : %d\n",digits); printf("The number of special characters in the string is : %d\n",special); }
출력
Enter the string :The number of alphabets in the string is : 0 The number of digits in the string is : 0 The number of special characters in the string is : 1