Vous êtes sur la page 1sur 31

STRING AND STRING HANDLING IN C

A string in C language is a variable length array of characters that is delimited by null character(\0)

Storing Strings
A string is stored in an array of characters. It is terminated by the null character(\0).
H E L

End of string

L O \0

Introduction
Group of characters ,digits, and symbols enclosed within quotation marks are called strings. Character arrays are called strings. Every string is terminated by \0(null character). Eg : char name[]={i , n , d , i , a ,\0};

Important Points To Note Each character of the string occupies one byte of memory. Last character I always \0. It is not compulsory to write \0 at the end of the string. Compiler automatically puts \0 at the end of the character array

Declaration and initialization of string


char name[]=INDIA ; The c compiler inserts the NULL (\0) character automatically at the end of the string. So initialization of null character is not essential.

Char arrays can be intialized as follows:a) char name[5]={i , n, d , i, a}; b) char name[6]={i , n, d , i, a}; In case(a) the output will not be India but it contains some garbage value. Arguments in this example are initialized with [6],which is exactly equal no of characters inside the braces. The Null character must be included and hence, the argument must be [6] instead of [5].

WAP to display the output when the account of null character is not considered.
#include<stdio.h> void main() { char name[5]={i , n, d , I, a}; clrscr(); printf(Name1= %s,name); }
Output

output would be india followed by garbage value. To get the correct result the argument must be [6]

To display a successive string in case 1st string is not terminated


#include<stdio.h> void main() { char name1[5]={i , n, d , I, a}; char name2[6]={i , n, d , I, a}; clrscr(); printf(Name1= %s,name1); printf(Name1= %s,name2); }
Output:

indiaindia

The compiler reads the second string immediately followed by the first string because the end of first string is not indentified. Because of this second string is printed followed by the first string.

String Formats with different precision


main() { char text[15]=prabhakar; printf(%s\n,text); printf(%.5s\n,text); printf(%.8s\n,text); printf(%.15s\n,text); printf(%-10.4s\n,text); printf(%11s\n,text); }

WHAT IS A STRING? A string is combination of characters. Any set or sequence of characters defined within double quotation symbols is a constant string. In c it is required to do some meaningful operations on the strings

Initializing Strings The initialization of a string must the following form which is simpler to the one dimension array char month1[ ]={j,a,n,u,a,r,y};

The following example shows the use of string: #include < stdio.h > main() { char month[15]; printf (Enter the string); gets (month); printf (The string entered is %s, month); } Note Character string always terminated by a null character . A string variable is always declared as an array & is any valid C variable name. Gets-complete input line is read and stored in memory Reading Strings from the terminal The function scanf with %s format specification is needed to read the character string from the terminal itself. The following example shows how to read strings from the terminals: char address[15]; scanf(%s,address);

String operations (string.h)


language recognizes that strings are terminated by null character and is a different class of array by letting us input and output the array as a unit. To array out many of the string manipulations library supports a large number of string handling functions that can be used such as: 1. Length (number of characters in the string). 2. Concatenation (adding two are more strings) 3. Comparing two strings. 4. Substring (Extract substring from a given string) 5. Copy(copies one string over another)

What is the string handling functions present in header file <string.h>?

There are number of string handling functions present in string.h. In other words if a programmer uses any of the function present in string.h then they must include the header file as

#include <string.h>

strlen() This function is used to length of the string in other words the number of characters present in string . strcat() This function is used to concatenate two strings. strcmp() This function is used to compare two strings. strcpy() This function is used to copy the second string given as second parameter to this function into first string. strstr() This function is to obtain the first occurrence of substring in a string .

strlen() function This function counts and returns the number of characters in a particular string. The length always does not include a null character. The syntax of strlen() is as follows: n=strlen(string); Where n is the integer variable which receives the value of length of the string.

The following program shows to find the length of the string using strlen() function /*write a c program to find the length of the string using strlen() function*/ #include < stdio.h > include < string.h > void main() { char name[100]; int length; printf(Enter the string); gets(name); length=strlen(name); printf(\nNumber of characters in the string is=%d,length); }

strcat() function when you combine two strings, you add the characters of one string to the end of the other string. This process is called as concatenation. The strcat() function is used to joins 2 strings together. It takes the following form: strcat(string1,string2) string1 & string2 are the character arrays. When the function strcat is executed string2 is appended to the string1. the string at string2 always remains unchanged.

/to concatenate two string #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[40]; char str1[40]; printf("Enter string1 and string2"); gets(str);//sanam gets(str1); //kadge puts(str);//sanam strcat(str,str1); sanam kadge puts(str); sanamkadge getch(); }

strcmp ()function
In c , you cannot directly compare the value of 2 strings in a condition like if(string1==string2) Most libraries however contain the function called strcmp(),which returns a zero if 2 strings are equal, or a non zero number if the strings are not the same. The syntax of strcmp() is given below: strcmp(string1,string2)

strcmpi() function
This function is same as strcmp() which compares 2 strings but not case sensitive. strcmpi(string1,string2)

strcpy() function
To assign the characters to a string,C does not allow you directly as in the statement name=Robert; Instead use the strcpy() function found in most compilers the syntax of the function is illustrated below. strcpy(string1,string2);

Library: string.h Prototype: strcpy(char s1[], char s2[]); Syntax: char string2[20]="red dwarf"; char string1[20]=""; strcpy(string1, string2);

strlwr () function This function converts all characters in a string from uppercase to lowercase The syntax of the function strlwr is illustrated below strlwr(string);

#include<stdio.h> #include<conio.h> #include<string.h> Int main() { char str[20]; clrscr(); gets(str); puts(str); strlwr(str); puts(str); Return 0; }

strrev() function This function reverses the characters in a particular string. The syntax of the function strrev is illustrated below strrev(string);

#include<stdio.h> #include<conio.h> #include<string.h> Int main() { char str[20]; clrscr(); gets(str); puts(str); strrev(str); puts(str); Return 0; }

FIND THE LENGTH WITOUT STRLEN()

TO FIND REV WITOUT STRING.H

Write a program to accept a string, copy it into another string and display this new string. Write a program to accept two strings, join them and display the result. Write a program to accept a string and find its length without using the string header file. Write a program to reverse a user entered string. Write a program to check whether the entered string is palindrome or not (Do not use the string header file).

Vous aimerez peut-être aussi