0% found this document useful (0 votes)
8 views

06 CharactersandStrings

The document discusses C strings and character manipulation functions. It covers string representation as character arrays terminated with a null character. Functions for input, output, comparison and manipulation of strings are presented.

Uploaded by

ashokabc2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

06 CharactersandStrings

The document discusses C strings and character manipulation functions. It covers string representation as character arrays terminated with a null character. Functions for input, output, comparison and manipulation of strings are presented.

Uploaded by

ashokabc2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

6.

Characters and Strings


A C-string is an array of characters terminated with a null character, denoted as ​'\0' which is equivalent to
ASCII 0. For example,
char message[] = {'H', 'e', 'l', 'l', 'o', '\0'};
char message[] = "Hello"; ​// same as above

Clearly, the length of array is the length of string plus 1, to account for the terminating null character ​'\0'​.

You can use ​scanf() to input a string, and ​printf() to print a string, with %s conversion specifier. For
example,
1#include <stdio.h>
2#include <string.h>
3
4int main() {
5 char message[256];
6 ​// The length of char array shall be sufficient to hold the string
7 ​// plus the terminating null character '\0'
8 printf("Enter a message: ");
9 scanf("%s", message);
10 ​// Do not place an & before the array variable
11 printf("The message is: %s\n", message);
12 ​// Print up to but not including the terminating null character '\0'
13
14 ​// print each characters
15 int i;
16 for (i = 0; message[i] != '\0'; ++i) {
17 printf("'%c' ", message[i]);
18 }
19 printf("\n");
20
21 int len = strlen(message);
22 ​// Length of string does not include terminating null character '\0'
23 printf("The length of string is %d\n", len);
24 }

Enter a message: ​hello


The message is: hello
'h' 'e' 'l' 'l' 'o'
The length of string is 5

Take note that you need to allocate a ​char array that is big enough to hold the input string including the
terminating null character ​'\0'​.
6.1 Character Type and Conversion in <
​ ctype.h>​ Header
Function Description
int ​isalpha​(int c); [a-zA-Z] Check the character's type and return true
int ​isdigit​(int c); [0-9] (non-zero) or false (0)
int ​isalnum​(int c); [a-zA-Z0-9]
int ​isxdigit​(int c); [0-9A-Fa-f]
int ​isspace​(int c); [ \t\n]
int ​iscntrl​(int c); Control character
int ​ispunct​(int c); Punctuation character
int ​isprint​(int c); Printable character
int ​isgraph​(int c); Graphical character
​ supper​(int c);
int i [A-Z] Check if uppercase/lowercase and return true
int i​ slower​(int c); [a-z] (non-zero) or false (0)
​ oupper​(int c);
int t To Uppercase Return the uppercase/lowercase character, if ​c​ is a
int t​ olower​(int c); To Lowercase lowercase/uppercase character; otherwise, return ​c​.

6.2 String/Number Conversion in ​<stdlib.h>​ Header


The ​stdlib.h​ contains function prototypes for conversion between string and numbers.

Function Description
int ​atoi​(const char * str); String to ​int Convert the ​str​ to
double ​atof​(const char * str); String to ​double int/double/long/long long​.
long ​atol​(const char * str); String to ​long
long long ​atoll​(const char * str); String to ​long long
double ​strtod​(const char* str, String to ​double Convert the ​str​ to
char** endptr); String to ​float double/float​.
float ​strtof​(const char* str, If ​endptr​ is not a null pointer,
char** endptr); it will be set to point to the first
character after the number.
long ​strtol​(const char* str, String to ​long Convert the ​str​ to
char** endptr, int base); String to ​unsigned long/unsigned long​.
unsigned long ​strtoul​(const char* long
str, char** endptr, int base);

6.3 String Manipulation in ​<string.h>​ Header


Function Description
char* ​strcpy​(char* dest, const char* String copy Copy ​src​ into ​dest​.
src); String copy at most Return ​dest​.
char* ​strncpy​(char* dest, const char* n-chars
src, size_t n);
char* ​strcat​(char* dest, const char* String concatenation Concatenate ​src​ into
src); String concatenation at dest​.
char* ​strncat​(char* dest, const char* most n-char Return ​dest​.
src, size_t n);
int ​strcmp​(const char* s1, const char* String compare Comparing ​s1​ and
s2); String compare at most s2​.
int ​strncmp​(const char* s1, const char* n-char Return 0, less than 0,
s2, size_t n); more than 0
if ​s1​ is the same, less
than, more than ​s2​.
int ​strlen​(const char* str); String Length Return the length of
str
(excluding
terminating null char)
​ trchr​(const char* str, int c);
char* s Search string for char Return a pointer to
char* s​ trrchr​(const char* str, int c); Search string for char the first/last
reverse occurrence
of ​c​ in ​str
if present. Otherwise,
return ​NULL​.
char* ​strpbrk​(const char* str, const Search string for char in Locate the first
char* pattern); pattern occurrence in ​str
of ​any character in
pattern​.
char* ​strstr​(const char* str, const Search string for sub-string Return a pointer to
char* substr); the first occurrence
of ​substr​ in ​str
if present. Otherwise,
return ​NULL​.
char* ​ trspn​(const char* str, const
s Search string for span of
char* substr); substr
char* ​strcspn​(const char* str, const Search string for
char* substr); complement span of
substr
char* ​strtok​(char* str, char *delimit); Split string into tokens
void* ​memcpy​(void *dest, const void Memory block copy
*src, size_t n); Memory block move
void* ​memmove​(void *dest, const void Memory block compare
*src, size_t n); Search memory block for
int ​memcmp​(const void *p1, const void char
*p2, size_t n); Memory block set (fill)
void* ​memchr​(void *ptr, int value,
size_t n);
void* ​memset​(void *ptr, int value,
size_t n);
6.4 char/string IO in ​<stdio.h>​ Header
Function Description
int getchar(); Get character (from Input/Output a character
int putchar(int c); stdin​) from ​stdin/stdout​.
Put character (to
stdout​)
int getc(FILE *stream); Get character (from Input/Output a character
int putc(int c, FILE *stream); FILE stream) from ​FILE​ stream.
int ungetc(int c, FILE *stream); Put character (to FILE
stream)
Un-get character (to
FILE stream)
char* gets(char *str); Get string (from Input/Output string
int puts(const char* str); stdin​) from ​stdin/stdout​.
Put string (to ​stdout​)
int sprintf(char *str, const Formatted print (to Formatted string input/output.
char *format, ...); string) Similar to ​printf()​ and ​scanf()​,
int sscanf(char *str, const char Formatted scan (from except that the output/input comes
*format, ....); string) from the ​str​.

You might also like