Monday 9 April 2018

String Operations (string.h)

C language recognizes that string is a different class of array by letting us input and output the array as a unit and are terminated by null character. C library supports a large number of string handling functions that can be used to carry out many of the string manipulations such as :-

 * Length (Number of character in the string).
 * Concatenation (Adding two or more strings).
 * Comparing two strings.
 * Substring (Extract substring from a given string).
 * Copy (Copies one string over another).

To do all the operation described here it is essential to include string.h library header file in the program.

strlen( ) Function
  This function counts and returns the number of character in a string. The length does not include a null character.

 Syntax
 n=strlen(string);
                     Where n is integer variable which receives the value of length of the string.

Example :
  length = strlen("Irawen");
The function will assign number of character 9 in the string to a integer variable length.


\*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("\n Number of character in the string is=%d",length);
}

strcat( ) Function
 When you combine two strings , you add the character of one string to the end of other string. The process is called concatenation. The strcat( ) function joins two strings together. It takes the following form

Syntax :
  strcat(string1, string2)
   String1 and string2 are character arrays. When the function strcat is executed string2 is appended to string1. The string2 remains unchanged.

Example :
 strcpy(string1, "sri");
 strcpy(string2,"Bhagavan");
 printf("%s",strcat(string1,string2);
  
From the above program segment the value  of string1 becomes sribhagavan. The string at str2 remains unchanged as bhagavan.

strcmp( ) Function
  In C language we cannot directly compare the value of 2 strings in a condition like if(string1==string2).
  
Most libraries however contain the strcmp( ) function, 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)

 String1 and string2 may be string variable or string constants. String1 and string2 may be string variable or string constant some computers return a negative if the string1 is alphabetically less than the second and a positive number if the string is greater than the second.

Example :
strcmp("Newyork","Newyork")  will return zero because 2 strings are equal.

strcmp("their","there") will return a 9 which is the numeric difference between ASCII 'i' and ASCII 'r'.

strcmp("The","the") will return 32 which is the numeric difference between ASCII "T" & "t".


strcmpi( ) Function 
  This function is same as strcmp( ) which compares 2 string but not case sensitive.

Example :
  strcmpi("THE","the");  will return 0;

strcpy( ) Function
  C language not allow you to assign the characters to a string 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);

Strcpy function assign the contents of string2 to string1. String2 may be a character array variable or a string constant.

strcpy(Name,"Robert");
In the above example Robert is assigned to the string called name.

strlwr( ) Funtion
 This function converts all characters in a string from uppercase to lower case.

Syntax:
 strlwr(string);

For example :
strlwr("IRAWEN") converts to Irawen.


strrev( ) Function
  This function reverses the character in a string.

Syntax
  strrev(string);

For example :
strrev("program") reverses the character in a string into "margrop".

strupr( ) Function
  This function converts all character in a string from lower case to uppercase.

Syntax
 strupr(string);

For example :
 strupr("pirawen") will convert the string to PIRAWEN.

/* Example program to use string functions */

#include<stdio.h>
#include<string.h>
void main( )
{
char s1[20], s2[20], s3[20];
int x;
printf("Enter the strings");
scanf("%s%s",s1,s2);
x=strcmp(s1,s2);
if(x!=0)
{
printf("\nStrings are not equal\n");
strcat(s1,s2);
}
else
printf("\nStrings are equal");
strcpy(s3,s1);
l1=strlen(s1);
l2=strlen(s2);
l3=strlen(s3);
printf("\n s1=%s\t length=%d characters\n",s1,l1);
printf("\n s2=%s\t length=%d characters\n",s2,l2);
printf("\n s3=%s\t length=%d characters\n",s3,l3);
}

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (113) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (726) Python Coding Challenge (169) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses