Handbook of Computer Science(cs) and IT

Strings

In C language, strings are stored in an array of character (char) type along with the null terminating character “\O” at the end.

char name [ ] = {‘K’, ‘R’ , ’I’, ‘S’, ‘H ‘ , ‘A’, ‘\O’};

‘\O’= Null character whose ASCII value is 0.

‘ 0’ = ASCII value is 48.

In the above declaration ‘\0’ is not necessary. C inserts the null character automatically.

# include <stdio.h>

void main ( )

{

char name ] = “RAM”;

printf (“%s”, name);

}

% s = It is used in printf ( ) as a format specification for printing out a string.

All the following notations refer to the same element

name [i] *(name+i)

* (i + name)

q [name]

# include <stdio.h>

void main ( )

{

char name [ ]= “shyam”;

char * ptr;

ptr = name; / *store base address of string while (*ptr ! = \O’ )

{

printf (“%c”, *prt);

ptr + +;

}

}

Note The above program is used to print all the characters of an string using pointer.

 

Length of String

Use strlen ( ) function to get the length of a string minus the null terminating character.

Syntax int strlen (string);

Concatenation of String

The strcat( ) function appends one string to another.

Syntax char * strcat (string 1, string 2);

Copy String

To copy one string to another string  variable,  we use strcpy( ) function.

Syntax strcpy (string 1, string 2);

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

Leave a Reply

Your email address will not be published. Required fields are marked *