[C] Characters

In C, a character is something like a number in a byte. It does not make sense for high-level languages but C is C. C standard library provides many useful functions for characters.

 

1. Characters

char” is 1-byte data type in C. You can assign char variables to other number variables or even you can downsize from int to char. It is very flexible indeed.

In itself, “char” is just a number from 0 to 255. But it can be interpreted by the character coding system such as ACSII.

 

2. Header Files for Character Functions

To use character related functions, you need to include the “ctype.h” file.

#include <ctype.h>

 

3. Functions

The following functions are useful to use:

int isalnum(int c); //returns nonzero if c is alphanumeric
int isalpha(int c); // returns nonzero if c is alphabetic only

int iscntrl(int c); // returns nonzero if c is a control chracter
int ispunct(int c); // returns nonzero if c is punctuation
int isspace(int c); // returns nonzero if c is space character

int islower(int c); // returns nonzero if c is a lower case character
int isupper(int c); // returns nonzero if c is upper case character

int isdigit(int c); // returns nonzero if c is a numeric digit
int isxdigit(int c); // returns nonzero if c is hexa digit

int tolower(int c); // returns the corresponding lowercase letter
int toupper(int c); // returns the corresponding uppercase letter

 

4.  Checking the Character Type

Checking the character is a number or a space can be useful before parsing the string to other types such as numbers.

int IsNumber(const char *str)
{
  int valid = 1;
  for(int i=0; i < (int)strlen(str); i++)
  {
    if (!isdigit(str[i]))
    {
      valid = 0;
      break;
    }
  }
  return valid;
}

void Test1()
{
  char number[] = "123";
  char wrongnumber[] = "12w";

  printf("%s is a valid number: %s n", number, IsNumber(number) ? "True" : "False");
  printf("%s is a valid number: %s n", wrongnumber, IsNumber(wrongnumber) ? "True" : "False");
}

 

5. ToLower and ToUpper

When you can compare strings you can use the “strcmp” function. The problem is that it performs case-sensitive comparing. What if you want to compare strings case-insensitively?

  • int tolower(int c)
  • int toupper(int c)
void StrToUpper(const char *src, char *des)
{
  int i = 0;
  for(; i < (int)strlen(src); i++)
  {
    des[i] = toupper(src[i]);
  }
  des[i] = '\0';
}

void StringTest2()
{
  char greeting1[] = "Hello, World";
  char greeting2[] = "hello, world";
  char greetingU1[13];
  char greetingU2[13];

  if (!strcmp(greeting1, greeting2))
    printf("Equal...  n");
  else
    printf("Not Equal... n");

  StrToUpper(greeting1, greetingU1);
  StrToUpper(greeting2, greetingU2);

  if (!strcmp(greetingU1, greetingU2))
    printf("Equal...  n");
  else
    printf("Not Equal... n"); }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s