← Back to C Course | Chapter 12: Advanced Topics | Lesson 13 of 20

C की ctype.h Functions

ctype.h library में helpers हैं जो check करते हैं आपके पास किस तरह का character है, जैसे एक letter, digit, या space, और इसका case बदल सकते हैं। यह characters के लिए एक sorter जैसा है।
Syntax
c
#include <ctype.h>

isalpha(character);
isdigit(character);
toupper(character);
tolower(character);

Alphabets Check करना

isalpha() check करता है कि एक character A-Z या a-z की ranges में आता है या नहीं और अगर हाँ तो एक non-zero value return करता है, जो user input को एक number या symbol की बजाय एक name या word की तरह process करने से पहले यह validate करने का standard तरीका है कि इसमें सिर्फ letters हैं।

उदाहरण: Checking for Alphabets

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include character-test functions (isdigit, toupper, ...)
#include <ctype.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", isalpha('A'));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Digits Check करना

isdigit() check करता है कि एक character decimal digits 0 से 9 में से एक है या नहीं, जो यह validate करने के लिए useful है कि एक number represent करने वाला input piece actually इसे atoi() जैसी किसी चीज़ से convert करने की कोशिश करने से पहले letters या punctuation नहीं रखता।

उदाहरण: Checking for Digits

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include character-test functions (isdigit, toupper, ...)
#include <ctype.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", isdigit('7'));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Alphanumeric Check करना

isalnum() check करता है कि एक character एक letter है या एक digit, isalpha() और isdigit() जो individually check करते हैं उसे combine करते हुए, जो usernames या identifiers जैसी चीज़ें validate करते समय convenient है जिन्हें letters और numbers दोनों allow करने चाहिए लेकिन punctuation reject करना चाहिए।

उदाहरण: Checking for Alphanumeric

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include character-test functions (isdigit, toupper, ...)
#include <ctype.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d %d", isalnum('A'), isalnum('!'));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Case Conversions

tolower() एक uppercase letter को इसके lowercase equivalent में convert करता है और कोई भी character जो uppercase letter नहीं है उसे unchanged छोड़ देता है, जबकि toupper() उल्टा conversion करता है — दोनों C में case-insensitive string comparisons के standard building blocks हैं।

उदाहरण: Case Conversions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include character-test functions (isdigit, toupper, ...)
#include <ctype.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%c %c", tolower('A'), toupper('b'));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Whitespace Check करना

isspace() check करता है कि एक character recognized whitespace characters (space, tab, newline, carriage return, और कुछ और) में से एक है या नहीं, जो text parse करते समय leading और trailing blank characters skip या trim करने के लिए आमतौर पर इस्तेमाल होता है।

उदाहरण: Checking for Whitespace

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include character-test functions (isdigit, toupper, ...)
#include <ctype.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d %d", isspace(' '), isspace('a'));
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. isalpha या toupper इस्तेमाल करने से पहले #include <ctype.h> भूल जाना।
  2. इन functions को एक negative char value pass करना, जो unsigned char में cast किए बिना undefined है।
  3. यह उम्मीद करना कि toupper एक पूरी string in place बदल देगा, जब यह एक character convert करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.