• Have Any Question?
  • contact@eitworld.com

C Strings





Strings In C is the sequence of characters is known as String used for manipulating any text such as words normally strings are used. Every C Strings is terminated with ‘’ (NULL) character but it is optional. Each character of string occupies one byte in memory. The characters of array are stored in contiguous memory locations. Here is an example which shows the memory map of a string:

c_strings

At eitworld.com you can learn C Programming Strings in simple and easy way with lots of examples.

    • Array of characters is known as String. To manipulate any text such as words normally strings are used.
    • Every string is terminated with ‘’ (NULL) character.
    • Here is an example of string:
char name[] = {‘M’,’A’,’N’,’O’,’J’,’’};
    • Each character of string occupies one byte in memory.
    • It is not compulsory to write ‘’ in string, compiler itself put the ‘’ character at the end of the string.
    • The characters of array are stored in contiguous memory locations. Here is an example which shows the memory map of a string:

c_strings

    • A String can also be initialized as follows:
char name[]=”MANOJ”;
  • ‘%s’ format string is used to display the string with the help of printf() function. Here is an simple example of string:
Example

#include<stdio.h>

#include<conio.h>

int main()

{

char name[7]="MANOJ";

printf("nYour name is:%s",name);

getch();

return 0;

}

The output of the above program is as follows:

Output
Your name is: MANOJ

C -String Standard Functions

    • C language supports several standard functions for manipulating the string.
    • Following table contains the various String function with their description:

c_strings

Following example shows the use of some of the string functions listed above in the table:

Example

#include<stdio.h>

#include<string.h>

#include<conio.h>

int main()

{

char s1[10],s2[10],s3[10];

printf("nEnter string1:");

scanf("%s",&s1);

printf("nEnter string2:");

scanf("%s",&s2);

printf("nlength of first string:%d",strlen(s1));

printf("nlength of second string:%d",strlen(s2));

printf("ncopy first string into third string:");

strcpy(s3,s1);

printf("%s",s3);

printf("nconcat first and second string:");

strcat(s1,s2);

printf("%s",s1);

printf("nConvert first string in lower case letters:");

strlwr(s1);

printf("%s",s1);

printf("nConvert second string in upper case letters:");

strupr(s2);

printf("%s",s2);

getch();

return 0;

}

The output of the above program is as follows:

Output
Enter string1: RaDhA

Enter string2:ShArmA

length of first string:5

length of second string:6

copy first string into third string:RaDhA

concat first and second string:RaDhAShArmA

Convert first string in lower case letters:radhasharma

Convert second string in upper case letters:SHARMA



Real Time Web Analytics