Skip to main content

Character Processing

Character Processing (<ctype.h>)
The <ctype.h> header file declares several functions for testing characters. For each function, the argument is an int whose value must be EOF or representable as an unsigned char , and the return value is an integer.
 
Functions
 
int isalnum(int c);
Returns a nonzero integer if the character passed to it is an alphanumeric ASCII character. Otherwise, isalnum returns 0.
 
int isalpha(int c);
Returns a nonzero integer if the character passed to it is an alphabetic ASCII character. Otherwise, isalpha returns 0.
 
int iscntrl(int c);
Returns a nonzero integer if the character passed to it is an ASCII DEL character (177 octal, 0x7F hex) or any nonprinting ASCII character (a code less than 40 octal, 0x20 hex). Otherwise, iscntrl returns 0.
 
int isdigit(int c);
Returns a nonzero integer if the character passed to it is a decimal digit character (0 to 9). Otherwise, isdigit returns 0.
 
int isgraph(int c);
Returns a nonzero integer if the character passed to it is a graphic ASCII character (any printing character except a space character). Otherwise, isgraph returns 0.
 
int islower(int c);
Returns a nonzero integer if the character passed to it is a lowercase alphabetic ASCII character. Otherwise, islower returns 0.
 
int isprint(int c);
Returns a nonzero integer if the character passed to it is an ASCII printing character, including a space character. Otherwise, isprint returns 0.
 
int ispunct(int c);
Returns a nonzero integer if the character passed to it is an ASCII punctuation character (any printing character that is nonalphanumeric and greater than 40 octal, 0x20 hex). Otherwise, ispunct returns 0.
 
int isspace(int c);
Returns a nonzero integer if the character passed to it is white space. Otherwise, isspace returns 0. The standard white space characters are:
space (' ')
form feed ('\f')
new line ('\n')
carriage return ('\r')
horizontal tab ('\t')
vertical tab ('\v')

int isupper(int c);
Returns a nonzero integer if the character passed to it is an uppercase alphabetic ASCII character. Otherwise, isupper returns 0.

int isxdigit(int c);
Returns a nonzero integer if the character passed to it is a hexadecimal digit (0 to 9, A to F, or a to f). Otherwise, isxdigit returns 0.

int tolower(int c);
Converts an uppercase letter to lowercase. c remains unchanged if it is not an uppercase letter.

int toupper(int c);
Converts a lowercase letter to uppercase. c remains unchanged if it is not a lowercase letter.

Example:
The following program will count number of alphabets, digits and spaces in a string

#include <stdio.h>
#include <string.h>
int main(void)
{
   char str[100];
int i,dc=0,al=0,sp=0;
printf("Enter a string \n");
fgets(str,100,stdin);
for(i=0;i<strlen(str);i++)
{
  if(isalpha(str[i])) al++;
  if(isdigit(str[i])) dc++;
  if(isspace(str[i])) sp++;
}
printf("Number of digits=%d\n",dc);
printf("Number of alphabets=%d\n",al);
printf("Number of spaces=%d\n",sp);
}
Output
Enter a string
this is test123
Number of digits=3
Number of alphabets=10
Number of spaces=3
 
Write a C program to read an English Alphabet through keyboard and display whetherthe given Alphabet is in upper case or lower case.

#include <stdio.h>
#include <ctype.h>
int main()
{
    char ch;
    printf("Enter a character\n");
    ch=getchar();
    if(isupper(ch))
      printf("uppercase\n");
    else
      printf("lower case\n");
}
Develop a C program to accept a string from the user. Display the count of upper case and lowercase characters in that string. ( university question)
#include <stdio.h>

int main() {
    char str[100];
    int uppercase_count = 0, lowercase_count = 0;
    int i = 0;

    // Input string from the user
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Count uppercase and lowercase characters
    while (str[i] != '\0') {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            uppercase_count++;
        } else if (str[i] >= 'a' && str[i] <= 'z') {
            lowercase_count++;
        }
        i++;
    }

    // Display counts
    printf("Number of uppercase characters: %d\n", uppercase_count);
    printf("Number of lowercase characters: %d\n", lowercase_count);

    return 0;
}
Note: we can also use builtin functions

Comments

Popular posts from this blog

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n ...

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper EST 102 Programmin in C University Question Papers  and evaluation scheme   EST 102 Programming in C  Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Devel...