Strings And it's App in C

Embed Size (px)

Citation preview

  • 8/3/2019 Strings And it's App in C

    1/28

    STRINGS AND ITS

    APPLICATIONS

  • 8/3/2019 Strings And it's App in C

    2/28

    CONTENTSCONTENTS

    Introduction to Strings.

    Two Ways of using Strings.

    Initializing a string.Print a string.

    String Functions and its applications.

  • 8/3/2019 Strings And it's App in C

    3/28

    Introduction to StringsIntroduction to StringsA String is an array of characters.

    Strings are stored in an array of char type along with the nullterminating character "\0" at the end.

    When sizing the string array we need to add plus one to the actual size

    of the string to make space for the null terminating character, "\0"

    But the null character is not included in the String.

    Character Strings are often used to build meaningful and Readableprograms.

    The Common Operations performed on Character Strings include

    Reading and Writing Strings.

    Combined Strings together.

    Copying one String to another.

    Comparing Strings for Equality.

    Extracting a portion of a String.

  • 8/3/2019 Strings And it's App in C

    4/28

    Two ways of using Strings:Two ways of using Strings:

    Using with a CharacterArray

    Using with a String Pointer

    With a Character Array:

    A character array is declared in the same way as a normal array.

    char ca[10];

    We must set the value of each individual element of the array to thecharacter We want and we must make the last character as 0. Remember

    to use %s when printing the string.

  • 8/3/2019 Strings And it's App in C

    5/28

    With String PointersWith String Pointers

    String pointers are declared as a pointer to a char.

    char *sp;

    When we assign a value to the string pointer it will automatically put

    the 0 .

    char *sp;sp=Hello;

    printf("%s",sp);

    We can read a string into only a character array using scanf and not

    a string pointer.

    If we want to read into a string pointer then we must make it

    point to a character array.char ca[10],*sp;

    scanf("%s",ca);

    sp = ca;

    scanf("%s",sp);

  • 8/3/2019 Strings And it's App in C

    6/28

    Syntax for declaring a String:Syntax for declaring a String:

    Syntax to declare a string in C:char string_name[size];

    Sample Code:

    char fname[4];

    The above statement declares a string called fname that can take up

    to 3 characters. It can be indexed just as a regular array as well.

    fname[] = {'t','w','o'};

    The last character is the null character having ASCII value zero.

    Initializing a StringInitializing a String

    To initialize our fname string store the name Ravi,

    char fname[31] = {Ravi"};

    We can observe from the above statement that initializing a string is

    same as with any array. However we also need to surround the string

    with quotes.

  • 8/3/2019 Strings And it's App in C

    7/28

    Print a StringPrint a String :

    To write strings to the terminal, we use a file stream known as stdout. The

    most common function to use for writing to stdout in C is the printffunction,

    defined as follows:

    int printf(constchar *format, ...);

    To print out a prompt for the user we can:

    printf("Please type aname: \n");

    The above statement prints the prompt in the quotes and moves the cursor

    to the next line.

  • 8/3/2019 Strings And it's App in C

    8/28

    FUNCTIONS IN

    STRING & ITS

    APPLICATION

  • 8/3/2019 Strings And it's App in C

    9/28

    MEMCPY - copy characters to

    non-overlapping string.

    "memcpy" copies exactly N characters from the string "s2" into the area of

    memory pointed to by "s1.

    Unlike the function "strncpy", "memcpy" does not check for the terminating

    '\0' of string "s2"; it simply copies N characters.

    It does not put a terminating '\0' on the end of string "s1".

    Example for memcpy():

    ptr = memcpy( s1, s2, N );

    void *s1; points to an area of memory that is to receive the copied

    characters. This must be able to hold at least N characters.

    const void *s2; points to the string from which characters will be copied.

    size_t N; gives the number of characters to copy.

    void *ptr; points to the copied string (i.e. "s1").

  • 8/3/2019 Strings And it's App in C

    10/28

    MEMMOVE - copy characters to (possibly

    overlapping) string.

    "memmove" moves exactly N characters from the string "s2" into the area

    of memory pointed to by "s1.

    Unlike the function "strncpy", "memmove" does not check for the

    terminating '\0' of string "s2"; it simply moves N characters.

    It does not put a terminating '\0' on the end of string "s1".

    EXAMPLEFORMEMMOVE():

    ptr = memmove( s1, s2, N );

    void *s1; points to an area of memory that is to receive the moved

    characters. This must be able to hold at least N characters.const void *s2; points to the string from which characters will be copied.

    size_t N; gives the number of characters to copy.

    void *ptr; points to the copied string (i.e. "s1").

  • 8/3/2019 Strings And it's App in C

    11/28

    STRCPY - copy one string to another.

    "strcpy" copies the string "s2" into the area pointed to by "s1.

    This area must be big enough to hold the contents of "s2.

    Since "strcpy" moves the '\0' at the end of "s2", the new string "s1"

    will have the usual terminating '\0'.

    EXAMPLEFOR STRCPY():ptr = strcpy( s1, s2 );

    char *s1; points to an area of memory that is to receive the copied

    characters.

    const char *s2; points to the string from which characters will be copied.This must end with the usual '\0.

    char *ptr; points to the copied string (i.e. "s1").

  • 8/3/2019 Strings And it's App in C

    12/28

    STRNCPY - copy characters from string.

    "strncpy" copies at most N characters from the string "s2" into the area ofmemory pointed to by "s1.

    If it encounters a '\0' before it has copied N characters, it pads "s1" to N

    characters by adding '\0' characters.

    If "s2" is more than N-1 characters long, the first N characters will be

    copied and the string "s1" will NOT receive the usual terminating '\0'.

    EXAMPLEFOR STRNCPY():

    ptr = strncpy( s1, s2, N );

    char *s1; points to an area of memory that is to receive the copied

    characters. This must be able to hold at least N characters.const char *s2; points to the string from which characters will be copied.

    size_t N; gives the number of characters to copy.

    char *ptr; points to the copied string (i.e. "s1").

  • 8/3/2019 Strings And it's App in C

    13/28

    STRCAT - append a string to another.

    "strcat" appends a copy of the string "s2" to the end of the string "s1.

    Both strings "s1" and "s2" must be terminated by the usual '\0' character.

    EXAMPLEFOR STRCAT():

    ptr = strcat( s1, s2 );

    char *s1; points to a string terminated by the usual '\0.

    const char *s2; points to a string that will be appended to "s1.

    char *ptr; points to the new string ("s1").

  • 8/3/2019 Strings And it's App in C

    14/28

    STRNCAT - append part of a string to

    another. "strncat" appends at most N characters from the string "s2" to the end of

    the string "s1.

    If string "s2" contains less than N characters, "strncat" will stop when it

    encounters the terminating '\0'.

    EXAMPLEFOR STRNCAT():

    ptr = strncat( s1, s2, N );

    char *s1;points to a string terminated by the usual '\0.

    const char *s2;points to a string whose characters will be appended to the

    end of "s1

    size_t N;is the maximum number of characters from string "s2" that should

    be appended to "s1.

    char *ptr;points to the new string ("s1").

  • 8/3/2019 Strings And it's App in C

    15/28

    MEMCMP - compare parts of two strings.

    "memcmp" compares the first N characters of the string "s1" to the first Ncharacters of the string "s2".

    Unlike the function "strncmp", "memcmp" does not check for a '\0'

    terminating either string. Thus it examines a full N characters, even if the

    strings are not actually that long.

    EXAMPLEFORMEMCMP():

    i = memcmp( s1, s2, N );

    const void *s1, *s2; are the strings to be compared.

    size_t N; gives the number of characters to be examined.

    int i; gives the results of the comparison. "i" is zero if the first N characters of

    the strings are identical. "i" is positive if string "s1" is greater than string "s2",

    and is negative if string "s2" is greater than string "s1". Comparisons of

    "greater than" and "less than" are made according to the ASCII collating

    sequence.

  • 8/3/2019 Strings And it's App in C

    16/28

    STRCMP - compare two strings.

    "strcmp" compares the string "s1" to the string "s2". Both strings must beterminated by the usual '\0' character.

    EXAMPLEFOR STRCMP():

    i = strcmp( s1, s2 );

    const char *s1, *s2; are the strings to be compared.

    int i; gives the results of the comparison. "i" is zero if the strings are identical.

    "i" is positive if string "s1" is greater than string "s2", and is negative if string

    "s2" is greater than string "s1". Comparisons of "greater than" and "less than"

    are made according to the ASCII collating sequence.

  • 8/3/2019 Strings And it's App in C

    17/28

    STRNCMP - compare parts of two

    strings. "strncmp" compares the first N characters of the string "s1" to the first N

    characters of the string "s2.

    If one or both of the strings is shorter than N characters (i.e. if "strncmp"

    encounters a '\0'), comparisons will stop at that point.

    Thus N represents the maximum number of characters to be examined,

    not the exact number.

    EXAMPLEFOR STRNCMP():

    i = strncmp( s1, s2, N );

    const char *s1, *s2; are the strings to be compared.

    size_t N; gives the number of characters to be examined.

    int i; gives the results of the comparison. "i" is zero if the first N characters of

    the strings are identical. "i" is positive if string "s1" is greater than string "s2",

    and is negative if string "s2" is greater than string "s1". Comparisons of

    "greater than" and "less than" are made according to the ASCII collating

    sequence.

  • 8/3/2019 Strings And it's App in C

    18/28

    MEMCHR - find first occurrence of a

    character in a string.

    "memchr" returns a pointer to the first occurrence of the character "c" in

    the first N characters of the string "s".

    Unlike "strchr", "memchr" ignores any '\0' bytes it comes across; thus it

    does not stop if it finds the end of the string, but keeps on going until it

    has looked at N characters.

    EXAMPLEFORMEMCHR():

    ptr = memchr( s, c, N );

    const void *s; points to the string to be scanned.

    int c; is the character to look for.

    size_t N; is the number of characters to look at before giving up.void *ptr; points to the first occurrence of the character in the string. If the

    character is not found, the NULL pointer is returned.

  • 8/3/2019 Strings And it's App in C

    19/28

    STRCHR - find first occurrence of a

    character in a string.

    "strchr" returns a pointer to the first occurrence of the character "c" in the

    string "s".

    The '\0' that terminates the string is considered part of the string; thus it is

    possible to find the end of the string with a call like

    ptr = strchr( s, '\0' );

    EXAMPLEFOR STRCHR():

    ptr = strchr( s, c );

    const char *s; points to the string that is to be scanned for the presence of

    the character.int c; is the character to look for.

    char *ptr; points to the first occurrence of the character in the string. If the

    character does not appear in the string, the NULL pointer is returned.

  • 8/3/2019 Strings And it's App in C

    20/28

    STRCSPN - scan string for one or more

    characters.

    "strcspn" scans through "s" and finds the first character in "s" that alsoappears in "stop.]

    It returns the position of that character within "s.

    Another way of saying this is that "strcspn" returns the number of

    characters at the beginning of "s" that are NOT found in "stop".

    EXAMPLEFOR STRCSPN():

    i = strcspn( s, stop );

    const char *s; points to the string to be scanned.

    const char *stop; points to a string containing the set of "stopping"

    Characters.

    size_t i; gives the position of the first character from "stop

    that appears in "s".

  • 8/3/2019 Strings And it's App in C

    21/28

    STRPBRK - find first character from set

    in string.

    "strpbrk" looks through "string" character by character until it finds one ofthe characters in "set.

    For example, if "set" is a string consisting of a blank and a tab, "strpbrk"

    would return a pointer to the first blank or tab appearing in "string".

    EXAMPLE

    F

    OR

    STRPBRK

    ():ptr = strpbrk(string,set);

    const char *string; is the string you want to examine.

    const char *set; gives a set of characters to look for.

    char *ptr; points to the first character in "string" that also appears in "set". If

    no characters from "set" can be found in "string", "strpbrk" returns a null

    pointer.

  • 8/3/2019 Strings And it's App in C

    22/28

    STRRCHR - find last occurrence of a

    character in a string.

    "strrchr" returns a pointer to the last occurrence of the character "c" in the

    string "s".

    EXAMPLEFOR STRRCHR():

    ptr = strrchr( s, c );

    const char *s; points to the string that is to be scanned for the presence of

    the character.

    int c; is the character to look for.

    char *ptr; points to the last occurrence of the character in the string. If the

    character does not appear in the string, a null pointer is returned.

  • 8/3/2019 Strings And it's App in C

    23/28

    STRSPN - scan string for span of

    characters.

    "strspn" returns the number of characters from the string "set" that appearat the beginning of the string pointed to by "s".

    EXAMPLEFOR STRSPN().

    i = strspn( s, set );

    const char *s; points to the string to be scanned.

    const char *set; points to a string containing the set of characters to scan for.

    size_t i; is the number of characters at the beginning of "s" which also appear

    in the string "set".

  • 8/3/2019 Strings And it's App in C

    24/28

    STRSTR - obtain first substring of string.

    "strstr" returns a pointer to the first occurrence of a substring withinanother string.

    EXAMPLEFOR STRSTR():

    ptr = strstr( s, subs );

    const char *s; points to the string to be scanned.

    const char *subs; points to the (sub)string to scan for.

    char *ptr; points to the first occurrence of the substring in the given string. If

    the substring is not found, this will be a null pointer.

  • 8/3/2019 Strings And it's App in C

    25/28

    MEMSET - set memory to specific value.

    "memset" sets N bytes to the given value, beginning at the byte pointed to

    by "p".

    EXAMPLEFORMEMSET():

    ret = memset( p, value, N );Wh

    ere:

    void *p; points to the beginning of the memory that should be set to the

    given value.

    int value; is the value that is to be assigned to each byte.

    size_t N; is the number of bytes whose value is to be set.

    void *ret; points to the beginning of the initialized memory.

  • 8/3/2019 Strings And it's App in C

    26/28

    STRERROR - error message

    associated with number. The "strerror" function returns a string describing the error associated with

    the given error number.

    EXAMPLEFOR STRERROR():

    msg = strerror(errnum); Where:

    int errnum; is an error number (that is, one of the recognized values that

    "errno" may take).

    char *msg; is a message explaining the meaning of the given error number.

  • 8/3/2019 Strings And it's App in C

    27/28

    STRLEN - find the length of a string.

    "strlen" returns the number of characters in a string.

    EXAMPLEFOR STRLEN():

    i = strlen( s );

    const char *s; points to the string whose length is to be determined. Thisstring must end with the usual '\0.

    size_t i; is the number of characters in the string "s" (not counting the '\0').

  • 8/3/2019 Strings And it's App in C

    28/28

    THANKYOU