CHARACTER ARRAYS AND STRINGS
A string is a sequence of characters that is treated as a single data
item.
C does not support strings as a data type.
Any group of characters (except double quote sign) defined
between double quotation marks is a string constant.
The common operations performed on character strings include :
Reading and writing strings
Combining strings together
Copying one string to another
Comparing strings for equality
Extracting a portion of a string
Declaring and initializing string variable
A string variable is always declared as an array of characters. The general
form of declaration of a string variable is
char city[10];
char name[20];
When the compiler assigns a character string to a character array, it
automatically supplies a null character (\0) at the end of the string.
Like numeric arrays, character arrays may be initialized when they are
declared. C permits a character array to be initialized in either of the
following two forms :
char city[9] = “NEW YORK “;
0 1 2 3 4 5 6 7 8
N E W Y O R K \0
char city[9] = {‘N’,’E’,’W’,’ ‘,’Y’,’O’,’R’,’K’,’\0’};
During compile time initialization, the null terminator has to be supplied
explicitly
C also permits us to initialize a character array without specifying the
number of elements. In such cases, the size of the array will be
determined automatically, based on the number of elements initialized.
char str[ ] = {‘G’,’O’,’O’,’D’,’\0’};
defines the array str as a five element array.
We can also declare the size much larger than the string size in the
initializer. Ex.
char str[10] = “GOOD”;
In this case, the computer creates a character of size 10, places the value
“GOOD” in it, terminates with the null character, and initializes all other
elements to NULL.
G O O D \0 \0 \0 \0 \0 \0
If the size of the array is lesser than the values initialized, it is illegal and
it results in a compile time error.
char str[3] = “GOOD”;
Also we cannot separate the initialization from declaration
char str[5];
str = “GOOD”;
and
char str[4] = “abc”;
char str1[4];
str1 = str;
is not allowed. An array name cannot be used as the left operand of an
assignment operator.
Reading strings from Terminal
i) using scanf()
scanf() function is used to read string from keyboard. It will accept
sequence of characters until it encounters a space or carriage return. So with
scanf() it is not possible to give sentences with space in between.
Ex.
char str[20];
scanf(“%s”,str);
If the input is Computerscience then str will be assigned the value
str = Computerscience
If the input is Computer science the str will take the value before space only.
Hence,
str = Computer
To overcome the problem in scanf, we can use a concept called edit set
conversion code.
Edit set conversion code
scanf(“%[^\n]”,str);
If ‘[\n]’ is used in scanf(), then the scanf() will accept the sequence of
characters including blank space till it encounters a carriage return.
getchar()
This function is used to accept a single character from the keyboard.
General form
getchar();
Ex.
char ch;
ch = getchar();
So, ch=a
gets()
This function is used to get sequence of characters from the keyboard.
General form
gets(str);
Printing strings in the monitor
putchar()
This function is used to display a single character in the screen.
General form
putchar(ch);
puts()
This is used to display a sequence of characters or string in the screen
General form
puts(str);
printf()
This function is used to print the string value on the monitor.
printf(“%s”,str);
String Handling Functions
strcat() Concatenates two strings
strcmp() Compares two strings
strcpy() Copies one string over another
strlen() Finds the length of a string
strcat()
The strcat() function joins two strings together. It takes the following form :
strcat(string1,string2);
string1 and string2 are character arrays. When the function strcat() is
executed, string2 is appended to string1. It does so by removing the null
character at the end of string1 and placing string2 from there. The string at
string2 remains unchanged. Ex.
s1 = Good \0
s2 = Morning
strcat(s1,s2);
After execution of the above strcat(),
s1 = Good Morning
s2 = Morning
We must make sure that the size of string1 (to which string2 is
appended) is large enough to accommodate the final string.
strcat() function may also append a string constant to a string variable.
The following is valid.
strcat(s1,”Morning”);
C permits nesting of strcat functions. Ex.
strcat(strcat(s1,s2),s3);
is allowed and concatenates all the three strings together. The resultant
string is stored in string1.
strcmp()
The strcmp() function compares two strings given in the arguments and
has a value 0 if they are equal. If they are not , it has the numeric difference
between the first nonmatching characters in the strings. It takes the form
strcmp(string1,string2);
string1 and string2 may be string variables are string constants. Ex.
strcmp(s1,s2);
strcmp(s1,”God”);
strcmp(“God”,”Good”);
s1 = there
s2 = their
strcmp(s1,s2);
will return 9 since the ASCII value difference between ‘r’ and ‘i’ (114 - 104) is 9.
strcpy()
The strcpy() function works almost like a string assignment operator. It takes
the form :
strcpy(string1,string2);
and assigns the contents of string2 to string1. string2 may be a variable or
string constant. Ex.
strcpy(city,”DELHI”);
will assign the string “DELHI” to the string variable city. Similarly, the statement
strcpy(city1, city2);
will assign the contents of the string variable city2 to the string variable city1.
The size of the array city1 should be large enough to receive the contents of
city2.
strlen()
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. The counting ends at the first
null character.
Ex.
s1 = “God is Good”);
n = strlen(“God is Good\n”)
n = strlen(s1);
n is assigned with an integer 11.
Other String Functions
The header file <string.h> contains all the string manipulation functions.
strncpy()
This is a three-parameter function. The general form of strncpy()
function is
strncpy(string1, string2, n);
This function copies only the left-most n characters of the source string to the
target string variable.
Ex.
strncpy(s1,”BALAGURUSAMY”,4);
This statement copies the first 4 characters of the source string s2 into
the target string s1. Since the first 4 characters may not include the
terminating null character, we have to place it explicitly in the 6th position as
shown below:
s1[5]=’\0’;
Now, the string s1 contains a proper string.
After executing the above statement
s1 = BALA\0
strncmp()
A variation of the function strcmp is the function strncmp. This function
has three parameters. The general form is :
strncmp(s1,s2,n);
This compares the left-most n characters of s1 to s2 and returns
a. 0 if they are equal
b. Negative number, if s1 is less than s2
c. Positive number, if s1 is greater than s2
Ex.
s1 = there
s2 = their
strncmp(s1,s2,3);
will return 0 since the first three characters of s1 and s2 are equal.
strncat()
This is another concatenation function that takes three parameters. The
general form is :
strncat(s1,s2,n);
This will concatenate the left-most n characters of s2 to the end of s1.
Ex.
s1 = BALA\0
s2 = GURUSAMY\0
After executing strncat(s1,s2,4)
s1 = BALAGURU\0
s2 = GURUSAMY\0
strstr()
It is a two-parameter function that can be used to locate a sub-string in a
string. The general form is :
strstr(s1,s2);
The function strstr searches the string s1 to see whether the string s2 is
contained in s1. If yes the function returns the position of the first occurrence
of the sub-string. Otherwise, it returns a NULL pointer.
Ex.
s1=”God is Good”
s2=”Good”
if(strstr(s1,s2) == NULL)
printf(“Substring is not found”);
else
printf(“s2 is a substring of s1”);
strchr()
This function is used to determine the existence of a character in a
string. The general form is :
strchr(s1,char);
will locate the first occurrence of the character char in the string s1.
Ex.
strchr(s1,’m’);
will locate the first occurrence of ‘m’ in the string s1.
strrchr()
This function is used to determine the existence of a character in a
string. The general form is :
strrchr(s1,char);
will locate the last occurrence of the character char in the string s1.
Ex.
strrchr(s1,’m’);
will locate the last occurrence of ‘m’ in the string s1.