About Me

Hello there! Before talking about qualifications and professional abilities, I would like to introduce myself. I am a Yuvraj Basnet and professional software developer and have been working in this field for 3 long years. I have finished my graduation from The University of Delhi on Computer Science Engineering I considered myself a reasonably experienced programmer with a particular interest in the human side of software development, computers are fascinating machine, but they're mostly reflection of the people using them. In the art of software development, studying code isn't enough; you have to study the people behind the software, too.

All String Functions in C Language (Part I)


All String Functions in C Language

All String Functions in C Language

In the previous article we discussed control structure in C programming Language and now in this article will discuss string functions which is supported by string.h file in C programming so without wasting further any time let's get started.

Definition of String :    

Strings are nothing but one-dimensional array of characters terminated by a special character i.e, null('\0'). Thus a null ended string contains the characters that comprise the string followed by a null.

Strings are always enclosed by double quotes (""). Whereas, character is always enclosed by single quotes in C.

The following declaration and initialization create a string containing of the word "Hello".

char Name[6] = {'H', 'e', 'l', 'l', 'o', '\0'};


Actually, you do not place the null character at the end of a string. The compiler automatically places the null character at the end of the string when it initializes the array. 

let us print the above output on the screen:

OUTPUT:
Your Message: Hello

String.h header file supports all the string functions in C language. All the string functions are given below :


1. strcat():
strcat( ) function in C language is used to concatenates two given strings. It combine source string at the end of destination string.

Syntax:
char * strcat ( char * destination, const char * source );



Method                                Description

strcat ( string2, string1 );   string1 is concatenated at the end of string2.

strcat ( strnig1, string2 );   string2 is concatenated at the end of string1.

PROGRAM TO ILLUSTRATE strcat():

OUTPUT:
Source string = Yuvraj
Target string = Basnet
Target string after strcat() = Yuvraj Basnet

2. strncat():
This is used for combine or concatenating (n) characters of one string into another string and the length of destination string must be greater than the source string.
The resultant concatenated string will be source string.

Syntax:
strncat(dest str, source string, n);

PROGRAM TO ILLUSTRATE strncat():

OUTPUT:
Source string = Welcome
Target string = Learn Code
Target string after strncat( ) = Learn CodeWelco

3. strcpy():
strcpy( ) function in C language is used for copying source string into destination string and length of destination string must be greater than or equal to the source string.

Syntax:
char *strcpy(char *str1, const char *str2);

Methods               Description
strcpy ( str1, str2); It copies contents of str2 into str1.
strcpy ( str2, str1); It copies contents of str1 into str2.

PROGRAM TO ILLUSTRATE strcpy():

OUTPUT:
source string =  We
target string = Learn Code
target string after strcpy( ) = We

4. strncpy():
strncpy() function in c Language is used for copying 'n' character of source string into destination string and the length of destination string must be greater than or equal to that of source string.
  
Syntax:
char *strncpy(char *str1, const char *str2, size_t count);

Methods                         Description
strncpy ( str1, str2, 4); It copies first 4 characters of str2 into str1.
strncpy ( str2, str1, 4); It copies first 4 characters of str1 into str2.

PROGRAM TO ILLUSTRATE strncpy():

OUTPUT:
source string = WelcometoCode
target string =
target string after strcpy( ) = Welco

5. strlen():
strlen() function gives the length of the string i.e, number of characters in the string.

Syntax:
size_t strlen(const char *str);

PROGRAM TO ILLUSTRATE strlen():

OUTPUT:
string length  = 17

6. strcmp():
strcmp() function is used for compare given two strings.
It returns American Standard Code for Information Interchange difference of the first two non matching characters in both the string.

Syntax:
int strcmp(const char *strng1, const char *strng2);

PROGRAM TO ILLUSTRATE strcmp():

OUTPUT:
1st run
Enter first string: yuvraj
Enter second string: yuvraj
You entered the same string two times

2nd run
Enter first string: BCA
Enter second string: CBA
Entered strings are not same!








Post a Comment

0 Comments