Lecture-14-String Handling Functions PDF
Lecture-14-String Handling Functions PDF
STRINGS
C library supports a large number of string functions. The list given below depicts
the string functions
String Function Action
strcpy(dest, src) Copies the source string src to destination string dest
strncpy(dest, src, n) Copies at most n characters of the source string src to
destination string dest
1
Lecture-14 Character Arrays and Strings Unit-3
substr() works with 0, 1 or 2 parameters as both parameters have defaults. The equivalent of the
Basic string functions Left(), Mid() and Right() are
Left(string,len) - substr(0,len)
Ex: Left(str, 7): It displays 7 characters from the left side of the given string.
Mid(string,startpos,len) - substr(startpos,len)
Ex: Mid(str, 5, 7): It takes the characters from specified position i.e 5 of the given string
and displays 7 characters (towards right side of the string).
Ex: Right(str, 7): It displays 7 characters from the right side of the given string.
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.
2
Lecture-14 Character Arrays and Strings Unit-3
Example:
Text1= VERY \0
Text2= GOOD\0
Text3= BAD\0
1. strcat(text1,text2);
Text2= GOOD\0
2. strcat(text1,text3);
Text1= VERY BAD
Text2= BAD
We must make sure that the size of string1 is large enough to accommodate the final
string. Strcat function may also append a string constant to string variable.
For example:
strcat(text1,”GOOD”);
strcat(strcat(string1,string2),string3);
Is allowed and concatenates all the three strings together. The resultant string is stored in
string1.
strcmp(string1,string2);
3
Lecture-14 Character Arrays and Strings Unit-3
Examples are:
strcmp(name1,name2);
strcmp(name1, “ABHI”);
strcmp(“ROM”, “RAM”);
We have to determine whether the strings are equal, if not which is alphabetically above.
More examples:
strcmp(“yello”, “hello”) : Here strings are not equal and the letter y is greater than the
letter h, therefore the function return the value > 0.
strcmp(“hello”,“yello”): Here strings are not equal and the letter h is smaller than the
letter y, therefore the function return the value < 0.
strcmp(“hi”,“hi”): Here strings are equal and therefore the function return the value 0.
strcmpi(“Hi”,“hi”): Here strings are equal because which ignores the case sensitive
characters and therefore the function return the value 0.
strcpy(string1,string2);
It copies the contents of string2 to string1. string2 may be a character variable or a string
constant.
strcpy(city , “BANGALORE”);
4
Lecture-14 Character Arrays and Strings Unit-3
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.
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.
String concatenation:
We cannot assign one string to another directly; we cannot join two strings together by
the simple arithmetic addition. The characters from string1 and string2 should be copied
into the string3 one after the other. The size of the array string3 should be large enough to
hold the total characters.
5
Lecture-14 Character Arrays and Strings Unit-3
Output
ATAL RAM KRISHNA
String comparison:
Comparison of two strings cannot be compared directly. It is therefore necessary to
compare the strings to be tested, character by character. The comparison is done until
there is a mismatch or one of the strings terminates into a null character.
6
Lecture-14 Character Arrays and Strings Unit-3
String copying:
strcpy(dest, src): If ‘dest’ and ‘src’ contains same string, then if we want to copy the
string from source „src‟ to the destination „dest‟ while coping it overlaps the contents then
the results are unpredictable or it displays the garbage value.
Program to show copying of two strings:
#include<stdio.h>
main()
{
char string1[80],string2[80];
int j;
printf(“Enter a string\n”);
printf(“?”);
scanf(“%s”, string2);
for(j=0;string2[i]!=‟\0‟;j++)
string1[j]=string2[j];
string1[j]=‟\0‟;
printf(“\n”);
printf(“%s\n”,string1);
printf(“Number of characters=%d\n”, j);
}
7
Lecture-14 Character Arrays and Strings Unit-3
For example:
y=‟a‟;
printf(“%d\n”, y);
For example:
y=‟z‟-1;
Is a valid statement. In ASCII , the value of „z‟ is 122 and therefore , the statement will
assign the value 121 to the variable Y.
For example:
Would test whether the character contained in the variable ch is an lower-case letter.
We can convert a character digit to its equivalent integer value using the following
relationship:
y=character –„0‟;
Where y is defined as an integer variable and character contains the character digit.
For example: Let us assume that the character contains the digit „7‟, then,
= 55-48
=7
8
Lecture-14 Character Arrays and Strings Unit-3
C library has a function that converts a string of digits into their integer values. The
function takes the form
y=atoi(string);
For example:
num=”1974”
year=atoi(num);
Num is a string variable which is assigned the string constant “1974”. The function atoi
converts the string “1974” to its numeric equivalent 1974 and assigns it to the integer
variable year.
What is the difference between a string copy (strcpy) and a memory copy (memcpy)?
When should each be used?
The strcpy() function is designed to work exclusively with strings. It copies each byte of
the source string to the destination string and stops when the terminating null character
(\0) has been moved. On the other hand, the memcpy() function is designed to work with
any type of data.
Because not all data ends with a null character, you must provide the memcpy() function
with the number of bytes you want to copy from the source to the destination.
The standard C library provides several functions for converting numbers of all formats
(integers, longs, floats, and so on) to strings and vice versa. One of these functions, itoa(),
is used here to illustrate how an integer is converted to a string:
#include <stdio.h>
#include <stdlib.h>
void main(void);
void main(void)
{
int num = 100;
char str[25];
itoa(num, str, 10);
printf("The number 'num' is %d and the string 'str' is %s.\n", num, str);
}
9
Lecture-14 Character Arrays and Strings Unit-3
Explanation: Here that the itoa() function takes three arguments: the first argument is
the number you want to convert to the string, the second is the destination string to put
the converted number into, and the third is the base, or radix, to be used when
converting the number. But this example uses the common base 10 to convert the
number to the string.
Note: the itoa(), ltoa(), and ultoa() functions are not ANSI compatible.
a to z : 97 to 122
10