You are on page 1of 3

Nhập môn CNTT&TT 2016

Character Strings
TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI
HANOI UNIVERSITY OF SCIENCE AND TECHNOLOGY

A variable of type char may hold a single character


Example: char onechar = ‘a' ;
Introduction to C language
Unit 5: Strings Character strings are arrays of simple characters with a
special character inserted into the string at the very
end: null character ( '\0' ).

Presenter: Dr. Vu Van Thieu They are assigned values with a pair of double quotes:
Department of Computer Science Example: char arraychar[6] = "abcde" ;
School of Information and Communication Technology
Hanoi University of Science and Technology

1 2

Character Strings Character Strings

Strings are normally accessed by a pointer to


the first character in the string. Declaration and initialization:

This means that the value of a string is the char uni [ ] = “HUST" ;
address of it's first character. or
char *uni = “HUST" ;
Address in string: or
char university[5] = “HUST" ; char uni [5] = {‘H', ‘U', ‘S', ‘T', '\0'} ;
university[0] = ‘H’
NOTE: Allowance must always be made for the
university[2] = ‘S’ terminating null character.

3 4

String I/O Library Routines String I/O Library Routines

#include <stdio.h>
#include <stdio.h>
/* The following are function prototypes for some
of the String I/O and Handling Library Routines */ /* Print character stored in character variable c
*/
/* Input next character as an integer */
int putchar (int c) ;
int getchar (void) ;

/* Input string into array s until newline */ /* Print character string s followed by \n */
char *gets (char *s) ; // Do not use, ever int puts (const char *s) ;

5 6

1
Nhập môn CNTT&TT 2016

Input/Output example Character classification functions (ctype.h)

Function Returns TRUE When ch is

isalnum(char) A letter of the alphabet (upper- or lowercase) or a number

isalpha(char) An upper- or lowercase letter of the alphabet

isdigit(char) A character 0 through 9

isblank(char) A tab or space or another blank character

iscntrl(char) A control code character, values 0 through 31 and 127

isspace(char) A space, tab, or an Enter

islower(char) A lowercase letter of the alphabet, a to z

isupper(char) An uppercase letter of the alphabet, A to Z

7 8

Character classification functions (ctype.h) Character functions: Example

Function Returns

toascii(ch) The ASCII code value of ch, in the range


of 0 through 127
tolower(ch) The lowercase of character ch

toupper(ch) The uppercase of character ch

Nguyen Thi Thu Huong

9 10

Character functions: Example String-processing functions (string.h)

Nguyen Thi Thu Huong

11 12

2
Nhập môn CNTT&TT 2016

String-processing functions: Example String-processing functions: Example

13 14

Summary

What is string
How to declare and initialize a string
Input/Output a string
Character functions
String functions

Nguyen Thi Thu Huong

15

You might also like