String A string is an array of char values terminated by a special null character value '\0'. A null terminated string contains th...
String
A string is an array of char values terminated by a special null character value '\0'. A null terminated string contains the characters that comprise the string followed by null. Thus during execution of program the declaration must be one more than the no of elements because null character \0 is placed at the last address.
Array of string as two dimensional array of character shows size and length of string. The first dimension or size tells how many string are in array and the second dimension tells the maximum length of each string.
String Handling Function
The library or built-in functions strlen(), strcpy(), strcat(), strcmp(), strrev(), strlwr(), strupr() etc are used for string manipulation. All the string function is defined on the header file string.h.
strlen()
This function return an integer which denotes the length of string passed. The length of string is the number of characters present in it, excluding the terminating null character.
syntax
strlen(string constant);
or
strlen(variable);
Example
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[40];
int l;
printf("Enter your name : \t");
gets(name);
l = strlen(name);
printf("\nThe no of character of your name is %d", l);
getch();
}
strcpy()
The strcpy() function copies one string to another. The function accepts two string as parameters and copies the second string character by character into the first one upto including the null character of the second string.
syntax
strcpy(destination, source);
Example
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[] = "Ram", s[5];
strcpy(s,name);
printf("%s",s);
getch();
}
strcat()
This function concatenates two string i.e. it appends one string at the end of another. This function accepts two strings as the paramenters and stores the contents of the second string at the end of first.
syntax
strcat(string1, string2);
Example
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20] = "Shyam", b[5] = "Hari";
strcat(a,b);
printf("\nThe output is %s", a);
getch();
}
strcmp()
This function compares two strings to find out whether they are same or different. This function is useful for constructing and searching string as arranged into a dictionary. This function accepts two string as parameters and returns as integer whose value may be zero, negative or positive. Two strings are comparing character by character until there is mismatch or end of one string and output is given as ASCII value.
syntax
strcmp(string1, string2);
Example
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[] = "Ram", b[] = "Rest";
int i;
i = strcmp(a,b);
printf("%d",i);
getch();
}
strrev()
This function is used to reverse all characters in a string except null character at the end of string.
syntax
strrev(string);
Example
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[15] = "Xenium";
char b[15];
strcpy(b,a);
strrev(b);
printf("Real name %s and reversed name is %s",a,b);
getch();
}
A string is an array of char values terminated by a special null character value '\0'. A null terminated string contains the characters that comprise the string followed by null. Thus during execution of program the declaration must be one more than the no of elements because null character \0 is placed at the last address.
Array of string as two dimensional array of character shows size and length of string. The first dimension or size tells how many string are in array and the second dimension tells the maximum length of each string.
String Handling Function
The library or built-in functions strlen(), strcpy(), strcat(), strcmp(), strrev(), strlwr(), strupr() etc are used for string manipulation. All the string function is defined on the header file string.h.
strlen()
This function return an integer which denotes the length of string passed. The length of string is the number of characters present in it, excluding the terminating null character.
syntax
strlen(string constant);
or
strlen(variable);
Example
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[40];
int l;
printf("Enter your name : \t");
gets(name);
l = strlen(name);
printf("\nThe no of character of your name is %d", l);
getch();
}
strcpy()
The strcpy() function copies one string to another. The function accepts two string as parameters and copies the second string character by character into the first one upto including the null character of the second string.
syntax
strcpy(destination, source);
Example
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[] = "Ram", s[5];
strcpy(s,name);
printf("%s",s);
getch();
}
strcat()
This function concatenates two string i.e. it appends one string at the end of another. This function accepts two strings as the paramenters and stores the contents of the second string at the end of first.
syntax
strcat(string1, string2);
Example
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20] = "Shyam", b[5] = "Hari";
strcat(a,b);
printf("\nThe output is %s", a);
getch();
}
strcmp()
This function compares two strings to find out whether they are same or different. This function is useful for constructing and searching string as arranged into a dictionary. This function accepts two string as parameters and returns as integer whose value may be zero, negative or positive. Two strings are comparing character by character until there is mismatch or end of one string and output is given as ASCII value.
syntax
strcmp(string1, string2);
Example
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[] = "Ram", b[] = "Rest";
int i;
i = strcmp(a,b);
printf("%d",i);
getch();
}
strrev()
This function is used to reverse all characters in a string except null character at the end of string.
syntax
strrev(string);
Example
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[15] = "Xenium";
char b[15];
strcpy(b,a);
strrev(b);
printf("Real name %s and reversed name is %s",a,b);
getch();
}
COMMENTS