www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 29: String Functions Bookmark and Share
Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
C library provides many functin to manipulat string. Like functions to compare strings, copy a string and so on.

Below is a list of the string manipulation functions and their description. All these functions are defined in the header file string.h, in the C library. Example code is also part of this lesson.
Function Prototype Function description
char *strcpy( char *s1, const char *s2 ) Copies string s2 into character array s1. The value of s1 is returned.
char *strncpy( char *s1, const char *s2, size_t n )

Copies at most n characters of string s2 into array s1. The value of s1 is returned.

char *strcat( char *s1, const char *s2 ) Appends string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.
char *strncat( char *s1, const char *s2, size_t n )
Appends at most n characters of string
s2 to array s1. The first character of s2
overwrites the terminating null character
of s1. The value of s1 is returned.
int strcmp( const char *s1, const char *s2) Compares string s1 to s2. Returns a
negative number if s1 < s2, zero if s1 ==
s2 or a positive number if s1 > s2
int strncmp( const char *s1, const char *s2, size_t n )
Compares up to n characters of string s1
to s2. Returns a negative number if s1 <
s2, zero if s1 == s2 or a positive number
if s1 > s2.
int strlen ( const char *s) Determines the length of string s. The
number of characters preceding the
terminating null character is returned.

Let’s look at the string copy function which is strcpy. The prototype of this function is

char *strcpy( char *s1, const char *s2 )

Here the first argument is a pointer to a character array or string s1 whereas the second argument is a pointer to a string s2. The string s2 is copied to string s1 and a pointer to that resultant string is returned. The string s2 remains the same. We can describe the string s1 as the destination string and s2 as the source string. As the source remains the same during the execution of strcpy and other string functions, the const keyword is used before the name of source string. The const keyword prevents any change in the source string (i.e. s2). If we want to copy a number of characters of a string instead of the entire string, the function strncpy is employed. The function strncpy has arguments a pointer to destination strings (s1), a pointer to source string (s2) . The third argument is int n. Here n is the number of characters which we want to copy from s2 into s1. Here s1 must be large enough to copy the n number of characters.

The next function is strcat (string concatenation). This function concatenates (joins) two strings. For example, in a string, we have first name of a student, followed by another string, the last name of the student is found. We can concatenate these two strings to get a string, which holds the first and the last name of the student. For this purpose, we use the strcat function. The prototype of this function is

char *strcat( char *s1, const char *s2 )

This function writes the string s2 (source) at the end of the string s1(destination). The characters of s1 are not overwritten. We can concatenate a number of characters of s2 to s1 by using the function strncat. Here we provide the function three arguments, a character pointer to s1, a character pointer to s2 while third argument is the number of characters to be concatenated. The prototype of this function is written as

char *strncat( char *s1, const char *s2, size_t n )

Example Code

/*Program to display the operation of the strcpy() and strncpy()*/
# include<iostream.h>
# include<string.h>
void main()
{
char string1[15]="String1";
char string2[15]="String2";
cout<<"Before the copy :"<<endl;
cout<<"String 1:\t"<<string1<<endl;
cout<<"String 2:\t"<<string2<<endl;
//copy the whole string
strcpy(string2,string1); //copy string1 into string2
cout<<"After the copy :"<<endl;
cout<<"String 1:\t"<<string1<<endl;
cout<<"String 2:\t"<<string2<<endl;
//copy three characters of the string1 into string3
strncpy(string3, string1, 3);
cout << “strncpy (string3, string1, 3) = “ << string3 ;
}

Following is the output of the program.
Before the copy :
String 1: String1
String 2: String2
After the copy :
String 1: String1
String 2: String1
Strncpy (string3, string1, 3) = Str



NEXT>>>>>Lesson 30. String Search Functions





Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.