Chapter-8
Character Arrays and Strings
Copyright © 2016 McGraw Hill Education, All Rights Reserved.
PROPRIETARY MATERIAL © 2016 The McGraw Hill Education, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced
or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and
educators permitted by McGraw Hill for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without
permission.
Introduction
• A string is sequence of characters that is treated as a single data
item.
• Any group pf characters (except double quote sign) defined between
double quotation marks is a string constant.
• E.g.- “I am BCA student”
• Common operations performed on character strings are:
• Reading and writing strings
• Combining strings together
• Copying one string to another
• Comparing strings for equality
• Extracting a portion of a string
Declaring & Initializing
• C does not support strings as a data type. It allows to represent
strings as character arrays.
• General form of declaration of string variable is:
• char string_name[size];
• e.g. char city[10]; char name[30];
• It can be initialized as-
• char city[9] = “New York”;
• char city[9]= {‘N’, ‘E’, ‘W’, ‘ ’, ‘Y’, ‘O’, ‘R’, ‘K’, ‘\0’,};
• Any array name cannot be used as the left operand of an assignment
operator.
Reading Strings from Terminal
• Using scanf function:
• char address[10];
• scanf(“%s”, address);
• scanf terminates its input on the first white space it finds.
• In case of character array, ampersand (&) is not required before the
variable name.
• In the scanf statement, the field width can be specified using the form
%ws.
• E.g.- scanf(“%ws”, name);
Fig. 8.1 Reading a series of words using scanf function
Fig. 8.3 Copying one string into another
Using getchar and gets function
• getchar function can be used repeatedly to read successive single
characters from the input and place them into a character array.
• char ch;
• ch = getchar();
• gets function read a string of text containing whitespaces. It is
availablein <stdio.h> header file.
• gets(str); where str is a string variable declared properly.
Fig. 8.2 Program to read a line of text from terminal
Writing strings to screen
• Using printf function:
• printf(“%s”, name); //display entire contents of array name.
• %10.4s //precision with which the array is displayed. It indicates
//that the first four characters are to be printed in a field
//width of 10 columns
• %-10.4s //left-justified
Fig. 8.4 Writing strings using %s format
(Contd.)
Fig. 8.5 Illustration of variable field specifications by printing sequences of characters
Fig. 8.6 Further illustrations of variable specifications
Using putchar and puts function
• putchar function can be used to output the values of character
variables.
• char ch = ‘A’;
• putchar(ch);
• The above is equivalent to- printf(“%c”, ch);
• putchar can be used repeatedly to output string of character stored in
an array using a loop.
• Another more convenient way of printing string values is to use the
function puts declared in <stdio.h>
• puts <str>; //str is a string variable containing string value.
String Concatenation
• The process of combining two string together is called
concatenation.
Fig. 8.8 Concatenation of strings
String-Handling Function
Function Action
strcat() concatenates two strings
strcmp() compares two strings
strcpy() copies one string over another
strlen() finds the length of a string
strcat() Function
• The strcat function joins two strings together.
• strcat(string1, string2); //string1 and string2 are character array.
• string2 is appended to string1 by removing null character at the end
of string1 and placing string2 from there.
• The size of string1 must be large enough to accommodate the final
string.
• Appending string constant to a string variable-
• strcat(part1, “GOOD”);
• Nesting of strcat functions-
• strcat(strcat(string1, string2), string3);
strcmp() function
• It compares two strings identified by the arguments and has a value 0
if they are equal. If not equal, it has the numeric difference between
the first nonmatching characters in the strings.
• strcmp (string1, string2);
• string1 & string2 may be string variables or string constants.
• E.g.
• strcmp(name1, name2);
• strcmp(name1, “John”);
• strcmp(“Rom”, “Ram”);
strcpy() function
• It takes the form: strcpy(string1, string2);
• It assigns the contents of string2 to string1. String2 may be
character array variable or a string constant.
• E.g. strcpy(city, “DELHI”); strcpy(city1, city2);
• The size of string1 should be large enough to receive the contents of
string2.
strlen() function
• This function counts and returns the number of characters in a string.
• It takes the form: n = strlen(string);
• where n is an integer variable, which receives the value of the
length of the string.
• The argument may be a string constant.
Fig. 8.9 Illustration of string
handling functions
Other string functions
• Other functions defined in the <string.h> library are-
• strncpy- copy only the left-most n characters: strncpy(s1, s2, n);
• strncmp- compares the left-most n characters: strncmp(s1, s2, n);
• strncat- concatenate the left-most n characters: strncat(s1, s2, n);
• strstr- locate a sub-string in a string:
• strstr(s1, s2); strstr(s1, “ABC”);
• The function strstr searchs the string s1 to see whether the
string s2 is contained in s1.
• E.g.-
if(strstr (s1, s2) == NULL)
printf(“substring is not found”);
else
printf(“s2 is a substring of s1”);
• strchr- function to determine the existence of a character in a string.
• strchr(s1, ‘m’); locate first occurrence of character ‘m’ in s1.
• strrchr(s1,’m’); locate last occurrence of character ‘m’ in s1.