Chapter10_CharacString

download Chapter10_CharacString

of 37

Transcript of Chapter10_CharacString

  • 8/11/2019 Chapter10_CharacString

    1/37

    CSEB114: PRINCIPLEOF PROGRAMMING

    Chapter 10: Character and String

  • 8/11/2019 Chapter10_CharacString

    2/37

    Objectives

    In this chapter, youll learn about; Fundamentals of Strings and Characters

    The difference between an integer digit and a

    character digitCharacter handling library

    String conversion functions

    Standard input/output library functions

    String manipulation functions

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    3/37

    Fundamentals of Characters and Strings

    Characters in C consist of any printable or

    nonprintable character in the computers

    character set including lowercase letters,

    uppercase letters, decimal digits, specialcharacters and escape sequences.

    A character is usually stored in the computer

    as an 8-bits (1 byte) integer. The integer value stored for a character

    depends on the character set used by the

    computer on which the program is running.prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    4/37

    Fundamentals of Characters and Strings

    There are two commonly used character sets:

    ASCII (American Standard Code for Information

    Interchange)

    EBCDIC (Extended Binary Coded Decimal Interchange

    Code)

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    5/37

    Difference Between an Integer Digit

    and a Character Digit

    char num = 1 and char num = 1 are not thesame.

    char num = 1 is represented in the computer

    as 00000001. char num = 1 on the other hand is number 49

    according to the ASCII character set.Therefore, it is represented in the computer as

    00110001.

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    6/37

    Example: ASCII character

    #include

    void main(void)

    {

    char my_A = 'A';

    char my_Z = 'Z';

    char my_a = 'a';char my_z = 'z';

    printf("\nASCII value for A is %d", my_A);

    printf("\nASCII value for Z is %d",my_Z);

    printf("\nASCII value for a is %d", my_a);

    printf("\nASCII value for z is %d",my_z);

    printf("\n");

    printf("\n65 in ASCII represents %c",65);

    printf("\n90 in ASCII represents %c",90);

    printf("\n97 in ASCII represents %c",97);

    printf("\n122 in ASCII represents %c",122);

    } prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    7/37

    Sample output

    ASCII value for A is 65

    ASCII value for Z is 90

    ASCII value for a is 97

    ASCII value for z is 122

    65 in ASCII represents A

    90 in ASCII represents Z

    97 in ASCII represents a

    122 in ASCII represents z

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    8/37

    Example cont

    #include

    void main(void)

    {

    char ch;

    printf("enter a character: ");

    scanf("%c", &ch);

    if (ch >= 'A' && ch = 65 && ch

  • 8/11/2019 Chapter10_CharacString

    9/37

    Fundamentals of Characters and Strings

    A string in C is an arrayof characters ending

    with the null character (\0). It is written inside

    a double quotation mark ( )

    A string may be assigned (in a declaration) toeither a char array or to a char pointer:

    char color[] = green; OR

    char *color = green;

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    10/37

    Fundamentals of Characters and Strings

    A string can also be defined by specifying the

    individual characters:

    char color[ ] = {g, r, e, e, n, \0};

    A string is accessed via a pointer to the firstcharacter in the string.

    In memory, these are the characters stored:

    prepared by NI, edited by MAF

    g r e e n \0

  • 8/11/2019 Chapter10_CharacString

    11/37

    Fundamentals of Characters and Strings

    Notice that even though there are only fivecharacters in the word green, six characters arestored in the computer. The last character, thecharacter \0, is the NULL character which indicates

    the end of the string. Therefore, if an array of characters is to be used to

    store a string, the array must be large enough tostore the string and its terminating NULL character.

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    12/37

    Briefly review about strings :

    We can initialize string variables at compile

    time such as;

    char name[10] = Arris;

    This initialization creates the following spaces instorage :

    prepared by NI, edited by MAF

    A r r i s \0 \0 \0 \0 \0

    [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

  • 8/11/2019 Chapter10_CharacString

    13/37

    Fundamentals of Characters and Strings

    If we happen to declare a string like this:

    char my_drink[3] = tea;

    We will get the following syntax error:error C2117: 'tea' : array bounds overflow

    Instead, we need to at least declare the array with(the size of the string + 1) to accommodate the null

    terminating character \0.

    char my_drink[4] = tea;

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    14/37

    Example: string and \0

    #include

    void main(void) /* a program that counts the number of characters in astring */

    {

    char sentence[] = "I love Malaysia";

    int i, count = 0;

    for (i = 0; sentence[i] != '\0'; i++)

    {count++;

    }

    printf(%s has %d characters including the whitespace", sentence,

    count);}prepared by NI, edited by MAF

    Sample output:

    I love Malaysia has 15 characters including the whitespace

  • 8/11/2019 Chapter10_CharacString

    15/37

    Briefly review about strings :

    Standard Functions Input

    scanf( )

    gets( )

    Standard Functions Output

    printf( )

    puts( )

    Use scanf function together with the format specifier %sfor interactive input string. (no whitespace character)

    If the string to be read as an input has embeddedwhitespace characters, use standard getsfunction.

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    16/37

    Example: gets/puts and scanf/printf

    #include

    int main(void){

    char string1[50];

    char string2[50];

    printf("Enter a string less than 50 characters with spaces: \n ");

    gets(string1);

    printf("\nYou have entered: ");

    puts(string1);

    printf("\nTry entering a string less than 50 characters, with spaces:\n");

    scanf("%s", string2);

    printf("\nYou have entered: %s\n", string2);

    return 0;

    } prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    17/37

    Example cont

    /* Sample output */

    Enter a string less than 50 characters with spaces:

    hello world

    You have entered: hello world

    Try entering a string less than 50 characters, with spaces:

    hello world

    You have entered: hello

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    18/37

    Character Handling Library

    Character handling library includes several function that

    perform useful tests and manipulation of character data.

    Each function receives a character, represented as an

    int or EOF, as an argument.

    When using functions from the character handling

    library, the header file needs to be included.

    Characters in these functions are manipulated as

    integers (since a character is basically a 1 byte integer).

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    19/37

    Functions in

    Prototype Function Descriptions

    int isdigit(int c) Returns a true if value c is a digit, and 0 (false) otherwise.

    int isalpha(int c) Returns a true if value c is a letter, and 0 otherwise.

    int isalnum(int

    c)

    Returns a true if value c is a digit or a letter, and 0 otherwise.

    int isxdigit(int c) Returns a true value if c is a hexadecimal digit character, and 0 otherwise.

    int islower(int c) Returns a true value if c is a lowercase letter, and 0 otherwise.

    int isupper(int

    c)

    Returns a true value if c is an uppercase letter, and 0 otherwise.

    int tolower(int c) If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower returns the

    argument unchanged.

    int toupper(int

    c)

    If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise toupper returns the

    argument unchanged.

    int isspace(intc) Returns true if c is a white space characternewline (\n), space ( ), form feed (\f), carriage return(\r), horizontal tab (\t) or vertical tab (\v) and 0 otherwise.

    int iscntrl(int c) Returns a true if c is a control character, and 0 otherwise.

    int ispunct(int c) Returns a true if c is a printing character other than a space, a digit or a letter, and 0 otherwise.

    int isprint(int c) Returns a true value if c is a printing character including space ( ), and 0 otherwise.

    int isgraph(int

    c)

    Returns a true value if c is a printing character other than space ( ), and 0 otherwise.

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    20/37

    String Conversion Functions

    These functions convert strings of digits to integer and

    floating-point values.

    To use these functions, the general utilities library

    , needs to be included.

    Note that these functions take a constant value as their

    argument. This means that we can only pass a constant

    string to the functions. For example:

    atoi (1234);

    const char *hello = 9999;

    atoi(hello);

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    21/37

    String Conversion Functions

    nPtr - The pointer to the string to be converted.

    endptr - The pointer to which the remainder of the string will be assigned

    after the conversion. We can pass a NULL if the remaining string is to be

    ignored.

    base - Indicates the format (base) of the string to be converted. If 0 is

    given, that means the value to be converted can be in octal (base 8),

    decimal (base 10) or hexadecimal (base 16).

    prepared by NI, edited by MAF

    Function Prototype Function Description

    double atof (const char *nPtr) Converts the sting nPtr to double.

    int atoi (const char *nPtr) Converts the string nPtr to int.

    long atol (const char *nPtr) Converts the string nPtr to long int.

    double strtod (const char *nPtr, char **endptr) Converts the string nPtr to double.

    long strtol (const char *nPtr, char **endptr, int base) Converts the string nPtr long.

    unsigned long strtoul (const char *nPtr, char **endptr,

    int base)

    Converts the string nPtr to unsigned long.

  • 8/11/2019 Chapter10_CharacString

    22/37

    Example

    /*1. Converting a String Into an int Using atoi. */

    #include

    #include

    int main()

    {char str1[ ] = "124z3yu87";

    char str2[ ] = "-3.4";

    char *str3 = "e24";

    printf("str1: %d\n", atoi(str1));

    printf("str2: %d\n", atoi(str2));

    printf("str3: %d\n", atoi(str3));

    return 0;

    }

    prepared by NI, edited by MAF

    Output:

    str1: 124str2: -3

    str3: 0

  • 8/11/2019 Chapter10_CharacString

    23/37

    Standard Input/Output Library Functions

    Include to use these functions.

    prepared by NI, edited by MAF

    Function

    Prototype

    Function Description

    int getchar(void) Get the next character from the standard input and return

    it as an integerchar *gets(char *s) Get characters from the standard input into the array s

    until a newline or end-of-file character is encountered. A

    terminating NULL character is appended to the array.

    int putchar(int c) Print the character stored in c

    int puts(const char *s) Print the string sfollowed by a newline characterint sprintf(char *s, const

    char *format, )

    Equivalent to printf except that the output is stored in the

    array sinstead of printing on the screen

    int sscanf(char *s, const

    char *format, )

    Equivalent to scanf except that the input is read from the

    array sinstead of reading from the keyboard

  • 8/11/2019 Chapter10_CharacString

    24/37

    String Manipulation Functions

    Include to use these functions.

    prepared by NI, edited by MAF

    Function Prototype Function Descriptionchar *strcpy (char *s1, const char

    *s2)

    Copies the string s2into the array s1. The value of

    s1is returned

    char *strncpy (char *s1, const char

    *s2, size_t n)

    Copies at mostncharacters of the string s2into

    the array s1. The value of s1is returned.

    char *strcat (char *s1, const char

    *s2)

    Appends the string s2to the array s1. The first

    character of s2overwrites the terminating NULL

    character of s1. The value of s1is returned.

    char *strncat (char *s1, const char*s2, size_t n) Appends at most ncharacters of string s2to arrays1. The first character of s2overwrites the

    terminating NULL character of s1. The value of s1

    is returned.

  • 8/11/2019 Chapter10_CharacString

    25/37

    String Comparison Functions

    Include to use these functions

    prepared by NI, edited by MAF

    Function Prototype Function Description

    int strcmp (const char *s1, const char *s2) Compares the string s1to the string s2. The

    function returns 0, less than 0 (negative value), or

    greater than 0 if s1is equal to, less than or greater

    than s2respectively.

    int strncmp (const char *s1, const char *s2, size_t

    n)

    Compares up to ncharacters of the string s1to

    the string s2. The function returns 0, less than 0,

    or greater than 0 if s1is equal to, less than or

    greater than s2respectively.

  • 8/11/2019 Chapter10_CharacString

    26/37

    Example

    int strcmp ( const char *s1, const char *s2 );

    strcmp will accept two strings. It will return aninteger. This integer will either be: Negative value if s1 is less than s2.

    Zero value if s1 and s2 are equal.

    Positive value if s1 is greater than s2.

    strcmp is case sensitive.

    strcmp also passes the address of thecharacter array to the function to allow it to beaccessed.

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    27/37

    Example cont

    char *strcat ( char *dest, const char *src );

    strcat is short for string concatenate, which

    means to add to the end, or append. It adds the

    second string to the first string. It returns a pointerto the concatenated string. Make sure that the

    size of destis large enough to hold the entire

    contents of srcas well as its own contents.

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    28/37

    Example cont

    char *strcpy ( char *dest, const char *src ); strcpy is short for string copy, which means it

    copies the entire contents of srcinto dest. Thecontents of destafter strcpy will be exactly the

    same as src. size_t strlen ( const char *s );

    strlen will return the length of a string, minus thenull character ('\0'). The size_t is nothing to worry

    about. Just treat it as an integer that cannot benegative, which it is.

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    29/37

    Example: strcpy

    #include

    #include

    int main()

    {

    char string1[100] = Malaysia";char string2[50] = Gemilang;

    strcpy(string1,string2);

    printf(string1: %s\n", string1);

    printf(string2: %s\n", string2);

    return 0;

    }

    Output :

    string1: Gemilang

    string2: Gemilang prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    30/37

    Example: strncpy

    #include

    #include

    void main(void)

    {

    char string1[100] = Hello";

    char string2[50] = UNITEN";

    strncpy(string1, string2, 4);

    printf(string1: %s\n", string1);

    }

    prepared by NI, edited by MAF

    Output:

    string1: UNITo

  • 8/11/2019 Chapter10_CharacString

    31/37

  • 8/11/2019 Chapter10_CharacString

    32/37

    Example cont

    You can only exit the program by entering

    "EDDIE".

    To perform a lowercase string comparison, use

    strcmp instead of strcmp but be warned:strcmp is NOT an ANSI C so it won't be

    supported by all C compilers.

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    33/37

    Example: strcat

    /* Concatenating Strings Using strcat */#include

    #include

    void main()

    {

    char str1[50] = "Hello ";char str2[ ] = "World";

    strcat(str1, str2);

    printf("str1: %s\n", str1);

    }

    Output:str1: Hello World

    prepared by NI, edited by MAF

    Note : This only works if you've

    defined the str1 array to be

    large enough to hold thecharacters of your string. If you

    don't specify a size, the

    program may crash.

  • 8/11/2019 Chapter10_CharacString

    34/37

    Example: strncat

    /* Concatenating Strings Using strncat */

    #include

    #include

    void main()

    {

    char str1[50] = "Hello ";

    char str2[ ] = "World";

    strncat(str1, str2, 2);

    printf("str1: %s\n", str1);

    }

    Output:

    str1: Hello Wo

    prepared by NI, edited by MAF

  • 8/11/2019 Chapter10_CharacString

    35/37

    Example: strlen

    #include

    #include

    void main(void)

    {

    char sentence[ ] = "I love C Prog";

    int i, count = 0;

    count = strlen(sentence);

    printf("%s has %d characters including the whitespace", sentence,

    count); prepared by NI, edited by MAF

    Sample output:I love Malaysia has 13 characters including the whitespace

  • 8/11/2019 Chapter10_CharacString

    36/37

    String Search Functions

    Include to use these functions.

    prepared by NI, edited by MAF

    Function Prototype Function Description

    char *strchr (const char *s, int c) Locates the first occurrence of character cin string s. If cis found, s pointer

    to cis returned. Otherwise a NULL pointer is returned.

    size_t strcspn (const char *s1, const

    char *s2)

    Determines and returns the length of the initial segment of string s1

    consisting of characters not found in string s2.

    size_t strspn (const char *s1, const

    char *s2)

    Determines and returns the length of the initial segment of string s1

    consisting only of characters contained in string s2.

    char *strpbrk (const char *s1, const

    char *s2)

    Locates the first occurrence of string s1of any character in string s2. If a

    character from string s2is found, a pointer to the character in string s1 is

    returned. Otherwise a NULL pointer is returned.

    char *strrchr (const char *s, int c) Locates the last occurrence of cin string s. If cis found, a pointer to cin

    string sis returned. Otherwise a NULL pointer is returned.

    char *strstr (const char *s1, const

    char *s2)

    Locates the first occurrence in string s1of string s2. If the string is found, a

    pointer to the string in s1is returned. Otherwise a NULL pointer is returned.

    char *strtok (char *s1, const char *s2) A sequence call to strtok breaks string s1into tokens logical pieces such

    as words in a line of textseparated by characters contained in strnig s2.

    The first call contains s1as the first argument, and subsequent calls to

    continue tokenizing the same string contain NULL as the first argument. A

    pointer to the current token is returned by each call. If there are no more

    tokens when the function is called, NULL is returned.

  • 8/11/2019 Chapter10_CharacString

    37/37

    SUMMARY

    C has a standard character-handling library that includes

    some useful functions for testing types of characters and

    for converting letters to uppercase and lowercase.

    String is another structured data type. C does not

    support strings as a data type. But we can usecharacter arrays to represent strings.

    Standard functions printf, puts

    Standard functions scanf, gets

    String manipulation functions => to copy strings, to

    compare, to compute length, to concatenate

    prepared by NI edited by MAF