Tuesday, 27 March 2012

String Variables, Initializing a String, String Manipulation, Strlen(), Strcat(), Strcpy(), Strcmp()


String Variables:-
          Variables of type char can hold a single character, so they have limited usefulness. We also need a way to store strings, which are sequence of characters. An array of characters representing a string is defined as follows:-
          Char   array_name[size]
          String variables are used to hold text data, which is comprised of letters, numerals, punctuation_marks, and other symbols.

Initializing a String:-
                   char  ram[]  =  {‘s’, ‘I’, ‘t’, ‘a’, ‘10’}
                                                Or
                   char  ram[]  =  “sita”

String Manipulation:-
          The <string.h> header file has several built in functions for string manipulation. Some of them are described below:-

          Strlen():-
                   This function is used to find the length of the string or the number of characters in a given string, excluding the end of string character (null).
          Strcat():-
                   String concatenation function concatenates (joins) the two string resulting in a single string. It takes the two arguments, which are the destination and source strings.
          Strcpy():-
                   String copy function copies the contents of one string to another strcpy(s1,s2) copies string s2 to string s1.
Strcmp():-
                    The string comparison function compares two strings character by character. It accepts any two strings as parameters and returns an integer value. Two strings are equal if their contents are identical or exactly the same.

No comments:

Post a Comment