Wednesday, October 13, 2010

String In C Language

,


The string in C programming language are represented by arrays of characters. The end of the string is marked with a special character, the null character, which is simply the character with the value 0. Whenever we write a string, enclosed in double quotes, C automatically creates an array of characters for us, containing that string, terminated by the \0 character.

For example, we can declare and define an array of characters, and initialize it with a string constant:

char string[ ] = "Hellopg";



Print With Example :

#include <stdio.h>
int main ()
{
   char Value[8] = {'H', 'e', 'l', 'l', 'o','p','g', '\0'};
   printf("Greeting message: %s\n", Value );
   return 0;
}

 
 Function  Purpose
 strcpy(s1, s2); Copies string s2 into string s1.
 strcat(s1, s2); Concatenates string s2 onto the end of string s1.
 strlen(s1); Returns the length of string s1.
 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
 strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1.
 strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.

Copy Function :

#include <string.h>

char string1[ ] = "Hello, world!";
char string2[20];
strcpy(string2, string1);

Try all function and observe result.

0 comments to “String In C Language”

Post a Comment