String in C Language

String Program in C 


In C, array of characters is called a string. A string is terminated with a null character \0. 
For example: " c string tutorial"

Here, C string tutorial is a string. when compiler encounter strings, it appends a null character \0 at the end of the string.


String Declaration 


Before we can actually work with strings, we need to declare them first.

String are declared in a similar wayas arrays. Only difference is that, strings are of char type.


Using arrays 


char s[5];


Initialization of Strings


In C, string can be initialized in a number of different ways.

For convenience and ease, both initialization and declaration are done in the same step.

Using arrays 


Char c[] = "abcd";
or
char c[50] = "abcd";
or
char c[] = {'a', 'b', 'c', 'd', '\0'};
or
char c[5] = {'a', 'b', 'c', 'd', '\0'};


The given string is initialized and stored in the form of arrays as above.

Reading Strings from user 


You can use the scanf() function to read a string like any other data types.

However, the scanf() function only takes the first entered word. The function terminates when it encounters a white space (or just space), Enter and tab.

Example -- 


char c[20];
scanf("%s", c);

Reading a line of text 


An approach to reading a full line of text is to read and store each character one by one.

For example : getchar() function is used to read a line of text.


#include <stdio.h>
int main ()
{
char name[30], ch;
int i = 0;
printf("enter name: ");
while(ch != '\n')
{
ch =getchar();
name[i] = ch;
i++;
}
name[i] ='\0';
printf("name: %s", name);
return 0;
}

In the above program, ch gets a single character from the user each time. This process is repeated until the user enters return (enter key). Finally, the null character is inserted at the end to make it a string. This process to take string is tedious.

Standard library function to read a line of text.


To make life easier, there are predefined functions gets() and puts in C language to read and display string respectively. 

#include <stdio.h>
int main()
{
char name[30];
printf(" enter name: ");
gets(name);
printf(" name: ");
puts(name);
return 0;
}

Output 


enter name: tom hanks
name : tom hanks


String manipulate 


You need to often manipulate strings according to the need of a problem. Most, if not all, of the time string manipulation can be done manually but, this makes programming complex and large.

To solve this, C supports a large number of string handling functions in the standard library "string.h"

Few commonly used string handling functions are discussed below:




Here some uses of string functions and Strings handling functions are defined under "string.h" header file.

#include <string.h>

MULTIPLE STRING HANDLING :--


Here chars[3][10] means 3 row and 10 column. 



EXAMPLE OF MULTIPLE STRING --

string function in c,string program in c
string function in c


Comments

Popular posts from this blog

Important Announcements(download all my book free)

O LEVEL- INTERNET TECHNOLOGY & WEB DESIGN

Call by Reference with Pointer in C Language part-4-a