C Programming Language

String

A string is a sequence of character that is treated as a single data item.
Any group of characters defined between double quotation mark is a string constant.

Declaring String Variable
C does not support string as a data type. However, it allows us to represent strings as character array.


The general form of declaration is:
where:
char

 - 

character defined data-type
string-name  -  user-defined string name
size  -  determine the number of characters
    in the string name.


Examples
   
  char city[10];
  char name[10];


 When the compiler assigns a character string to a character array, it automatically supplies a null character (‘\0’) at the end of the string. Therefore, the size should be equal to the maximum number of character in the string plus one.


Initialization of String
  • Character arrays may be initialized when they are declared.
    • char name[6] = “Mohan”;
    • char name[6] = {‘M’, ‘o’, ‘h’, ‘a’, ‘n’, ;\0’};


  • C permits us to initialize a character array without specifying the number of elements.
    In such case, the size of array will be determined automatically.

    • char string[ ] = {‘G’, ‘O’, ‘O’, ‘D’, ‘\0’};



String – Handling Function

The C library supports a large number of string handling functions that can be used to carry out many of the string manipulations.
Following are the most commonly used string-handling functions.

strcat()
- The strcat() function joins two string together.
The general form of declaration is:
where:
string2 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.


Examples
   


strcmp()
This is a function which compares two strings to find out whether they are same or different.

If the two string are identical, strcmp() returns a value zero. If they’re not, it returns the numeric difference between the ASCII values of the first non-matching pair of characters.
where:
string1 and string2

 - 

may be string variable or string constants

Examples
   


strcpy()
The strcpy function work almost like a string-assignment operator.

This function copies the contents of one string into another.

It assign the contents of string2 to string1. string2 may be a character array variable or a string constant.

Examples
   


strlen()
This function counts and returns the number of characters in a string.
where:
n

 - 

is an integer variable which receive the value of the length of the string.


Examples
   




   

google Ads.


Other - String Function

FunctionsDescriptions
strlwr()Convert a string to lowercase
strupr()Convert a string to uppercase
strncpy()Copies first n characters of one string into another
stricmp()Compares two string without regard to case (identical to strcmp)
strnicmp()Compares first n characters of two strings without regard to case.
strchr()Find the last occurrence of a given character in a string.
strstr()Find the first occurrence of a given string in another string.
strrev()Reverses string.


   

google Ads.


Previous
Next