C Programming - Strings

Embed Size (px)

Citation preview

  • 8/8/2019 C Programming - Strings

    1/16

    C PROGRAMMING - STRINGS

  • 8/8/2019 C Programming - Strings

    2/16

    Strings

    A string is an array of characters.

    Strings must have a 0 or null character afterthe last character to show where the string

    ends. The null character is not included in thestring.

    Two ways of using strings are: The first is with a character array and

    the second is with a string pointer. A character array is declared in the same

    way as a normal array. Example: char ca[10];

  • 8/8/2019 C Programming - Strings

    3/16

    Strings

    Any sequence or set of characters defined

    within double quotation symbols is a

    constant string. Set the value of each individual element of

    the array to the character you want and

    you must make the last character a 0.

    Remember to use %swhen printing the

    string.

  • 8/8/2019 C Programming - Strings

    4/16

    Strings

    In C, it is required to do some meaningful

    operations on strings:

    Reading string displaying strings Combining or concatenating strings

    Copying one string to another.

    Comparing string & checking whether they are

    equal

    Extraction of a portion of a string

  • 8/8/2019 C Programming - Strings

    5/16

    Reading Strings from the terminal

    The function scanf with %s format

    specification is needed to read the character

    string from the terminal. Example:char address[15];

    scanf(%s,address);

    scanf statement has a draw back it just

    terminates the statement as soon as it finds a

    blank space

  • 8/8/2019 C Programming - Strings

    6/16

    Reading Strings from the terminal

    The function getchar can be used repeatedlyto read a sequence of successive singlecharacters and store it in the array.

    We cannot manipulate strings since C does notprovide any operators for string. For instance wecannot assign one string to another directly. For example:String=xyz;

    String1=string2;

    The example about is not valid. To copy thechars in one string to another string we may doso on a character to character basis.

  • 8/8/2019 C Programming - Strings

    7/16

    char ca[10];ca[0] = 'H';ca[1] = 'e';

    ca[2] = 'l';ca[3] = 'l';ca[4] = 'o';ca[5] = 0;

    printf("%s",ca); String pointers are declared as a pointer to a

    char. char *sp;

  • 8/8/2019 C Programming - Strings

    8/16

    String handling functions

    The string.h header file has some

    useful functions for working with strings.

    strcpy(destination,source) strcpy copies the source string to the

    destination string.

    s1 = "abc";

    s2= "xyz";

    strcpy(s1,s2); // s1 = "xyz"

  • 8/8/2019 C Programming - Strings

    9/16

    String handling functions

    strcat(destination,source)

    Joins the destination and source strings and

    puts the joined string into the destinationstring.

    s1 = "abc";

    s2= "xyz";

    strcat(s1,s2); // s1 = "abcxyz"

  • 8/8/2019 C Programming - Strings

    10/16

    String handling functions

    strcmp(first,second)

    Compares the first and second strings. If the

    first string is greater than the second one thena number higher than 0 is returned. If the first

    string is less than the second then a number

    lower than 0 is returned. If the strings are

    equal then 0 is returned.

    s1 = "abc";

    s2= "abc";

    i =strcmp(s1,s2); // i = 0

  • 8/8/2019 C Programming - Strings

    11/16

    String handling functions

    strcmp(first,second)

    strcmp(Newyork,Newyork)

    will return zero because the two strings are equal. strcmp(their,there)

    will return a 9 which is the numeric difference

    between ASCII i and ASCII r.

    strcmp(The, the)

    will return 32 which is the numeric difference

    between ASCII T & ASCII t.

  • 8/8/2019 C Programming - Strings

    12/16

    String handling functions

    strcmpi() function

    This function is same as strcmp() which compares

    two strings but not case sensitive.

    Examplestrcmpi(THE,the);

    will return 0.

  • 8/8/2019 C Programming - Strings

    13/16

    String handling functions

    strlen(string)

    Returns the amount of characters in a string.

    s= "abcde";i =strlen(s); // i = 5

  • 8/8/2019 C Programming - Strings

    14/16

    String handling functions

    strlen(string)

    Returns the amount of characters in a string.

    s= "abcde";i =strlen(s); // i = 5

  • 8/8/2019 C Programming - Strings

    15/16

    /* Everyones first C program. */

    #include

    char hello[13] = { 'H', 'e',

    'l', 'l', 'o', ' ,'W', 'o',

    'r', 'l', 'd', '!', '\0' };

    main() {clrscr();

    printf("%s\n", hello);

    getch;}

  • 8/8/2019 C Programming - Strings

    16/16