what everybody ought to know about string in c.


String in C

String


In C programming, array of character are called strings. A string is terminated by null character /0.


Array is the collection of similar data under common name.


A common name is the name of string or array.For example:

 char stringname  = "Hello";
 Here stringname is common name .We will study String Decleration below Section.


 C  compiler represent above string as below image (string representation)


Declaration of String


It is declared as:


 char string_name[size];


E.g: char name[10];


Strings can also be declared using pointer.
char *p

Initialization of String


It can be initialized when they are declared


char c[]="abcd";
     OR,

char c[5]="abcd";

     OR,

char c[]={'a','b','c','d','\0'};

     OR;

char c[5]={'a','b','c','d','\0'}; .


String can also be initialized using pointers

char *c="abcd";


String Handling Function


 There are different libraries or built-in function in C for string manipulation.These function are defined in header file <string.h>

All string manipulation can be done manually by the programmer but, this makes programming complex and large.

To solve this, the C supports a large number of string handling functions.
    strlen()  -  Calculates the length of string

    strcpy() -   Copies a string to another string

    strcat()  -  Concatenates(joins) two strings

    strcmp() -   Compares two string

    strlwr()  -  Converts string to lowercase

    strupr()  -  Converts string to uppercase

  1.  strlen():


    This is used to return the length of string

    It returns the number of characters in the string without including null
    character

    Syntax:

 integer_variable = strlen(string);

  2  strcpy() :



    It copies one string to another

    Syntax:

 strcpy(destination_string , source_string);


    e.g strcpy(a,b) i.e the content of b is copied to a

  
  3. strcat():


    It joins  to strings together


    It stores the content of second string at the end of the first one.


    E.g: strcat (string1 , string2  ); i.e string1 = string1 + string2;

  
  4. strcmp():


    It  compares two string


    It takes two strings as parameters and returns the integer value.

      >> 0 if both string are same
      >> less than zero if  the first string is less than the second one.
       >>Greater than zero if the first string is more than the second

   5. strrev () :


    It is used to reverse all character of string except the null character

    Syntax:
    strrev ( string );


    E.g strrev(str) i.e it reverses the characters in string str and stores the reversed string in the str

SUMMERY

  1. Strings are actually one-dimensional array of characters terminated by a null character '\0'.
  2. Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null character at the end of string.
  3. Here "onlineclab" is a string of length 10 but  in c it's length is 11 because c add '\0' at the end.Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the '\0' at the end of the string when it initializes the array.
  4. The terminating null character  '\0' is important because it is only way the string handling function can know where the string ends
  5. Strings can also be declared using pointer.
  6. Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type. Array store all type of data but string store only character.
  7. string.h is collection of functions for string manipulation


  8. No standard operators for string assignment and comparisons!(remember: strings are arrays!)
  9. String can be passed to function in similar manner as arrays as, string is also an array
  10. Functions gets() and puts() are two string functions to take string input from user and display string respectively
  11. strlen()     Calculates the length of string
  12. strcpy()    Copies a string to another string
  13. strcat()    Concatenates(joins) two strings
  14. strcmp()   Compares two string
  15. strlwr()    Converts string to lowercase
  16. strupr()    Converts string to uppercase
  17. All string are array but all array are not string.   
Below video can be helpful for understanding string
Download above tutorial:

DO NOT MISS OTHER C PROGRAMMING TUTORIAL

* indicates required


You can Browse related article below for more information and program code related to string

Does above is helpful , Post you views in comment

Comments

Popular posts from this blog

C- Computer programming language-field of study

Print pattern in c - explore new perspectives in pattern printing

C++ Input/Output : Streams