SARANYA K
AP/CSE
SRIT
 The string is actually a one-dimensional array of characters
which is terminated by a null character '0'.
 All the string handling functions are prototyped in: string.h
header file. So while using any string related function, don't
forget to include string.h.
 String constants have double quote marks around them.
 String constants can be assigned to a char array either with
no size specified, or the size can also be specified, but don't
forget to leave a space for the null character.
 Strings are often needed to be manipulated by the programmer
according to the need of a problem. Hence, C provides a
variety of string handling functions.
 String handling functions refers to a group of functions
implementing various operations on strings.
 Some of the operations performed by the string handling
functions includes:
› Length (number of characters in the string).
› Concatenation (adding two are more strings)
› Comparing two strings.
› Substring (Extract substring from a given string)
› Copy(copies one string over another)
 The various string handling functions supported by C are as
follows:
 strlen()
It is used to find the length of the string.
syntax:
strlen(string)
 strcpy()
It is used to copy one string to another.
syntax:
strcpy(string1,string2)
 strcat()
It is used to combine two strings.
syntax:
strcat(string1,string2)
 strcmp()
It is used to compare two strings.
syntax:
strcmp(string1,string2)
 Returns 0 if two strings are equal.
 Return value <0 if s1 is less than s2.
 Return value >0 if s1 is greater than s2.
 strrev()
It used to reverse a string.
syntax:
strrev(string)
 strlwr(), strupr()
It used to change the case of a string.
syntax:
strlwr(string)
strupr(string)
 strncpy()
It used to copy ‘n’ characters of one string to another.
 strstr()
It is used to determine the first occurrence of a
given string in another string.
 strncat()
It appends source string to destination string up to
specified length.
 strspn()
It is used to find up to what length two strings are
identical.
 strncmp()
It is used to compare ‘n’ character of two strings.
 strcmpi()
It is used to compare two strings without regarding the case.
 strnicmp()
It is used to compare first ‘n’ characters of two strings
without regarding the case.
 strchr()
It is used to determine the first occurrence of a given
character in a string.
 strrchr()
It is used to determine the last occurrence of a given
character in a string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[]="college";
int b;
clrscr();
b=strlen(a);
printf("nThe length of the string is %d",b);
getch();
}
Output:
The length of the string is 7
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[ ]="IT";
char b[ ]="Dept";
clrscr();
strcpy(a,b);
printf("nThe string is %s",a);
getch();
}
Output:
The string is Dept
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[ ]="IT";
char b[ ]="Dept";
clrscr();
strcat(a,b);
printf("nThe string is %s",a);
getch();
}
Output:
The string is ITDept
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[ ]="itdept";
char b[ ]="it";
int i;
clrscr();
i=strcmp(a,b);
if(i==0)
printf("nstrings are equal:%d",i);
else if(i<0)
printf("nstring1 is less than string2:%d",i);
else
printf("nstring1 is greater than string2:%d",i);
getch();
}
Output:
string1 is greater than string2:100
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[ ]="itdept";
clrscr();
printf("nThe string is :%s",a);
strupr(a);
printf("nThe string after conversion to uppercase :%s",a);
strlwr(a);
printf("nThe string after conversion to lowercase :%s",a);
getch();
}
The string is : itdept
The string after conversion to uppercase :ITDEPT
The string after conversion to lowercase : itdept
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[ ]="Dept";
clrscr();
printf("nThe string is %s",strrev(a));
getch();
}
Output:
The string is tpeD
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[ ]="itdept";
char b[15];
int i=0;
clrscr();
strncpy(b,a,2);
b[2]='0';
printf("nThe string is :%s",b);
getch();
}
Output:
The string is :it
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int len,i,j;
char str[15];
clrscr();
printf("n Enter the string:");
scanf("%s",str);
len=strlen(str);
for(i=0,j=len-1;i<len/2;i++,j--)
{
if(str[i]!=str[j])
{
printf("n The String is not a palindrome");
getch();
exit(0);
}
}
printf("n The String is a palindrome");
getch();
}
Output:
Enter the string: abcba
The String is a palindrome

Strings

  • 1.
  • 2.
     The stringis actually a one-dimensional array of characters which is terminated by a null character '0'.  All the string handling functions are prototyped in: string.h header file. So while using any string related function, don't forget to include string.h.  String constants have double quote marks around them.  String constants can be assigned to a char array either with no size specified, or the size can also be specified, but don't forget to leave a space for the null character.
  • 3.
     Strings areoften needed to be manipulated by the programmer according to the need of a problem. Hence, C provides a variety of string handling functions.  String handling functions refers to a group of functions implementing various operations on strings.  Some of the operations performed by the string handling functions includes: › Length (number of characters in the string). › Concatenation (adding two are more strings) › Comparing two strings. › Substring (Extract substring from a given string) › Copy(copies one string over another)
  • 4.
     The variousstring handling functions supported by C are as follows:  strlen() It is used to find the length of the string. syntax: strlen(string)  strcpy() It is used to copy one string to another. syntax: strcpy(string1,string2)  strcat() It is used to combine two strings. syntax: strcat(string1,string2)
  • 5.
     strcmp() It isused to compare two strings. syntax: strcmp(string1,string2)  Returns 0 if two strings are equal.  Return value <0 if s1 is less than s2.  Return value >0 if s1 is greater than s2.  strrev() It used to reverse a string. syntax: strrev(string)  strlwr(), strupr() It used to change the case of a string. syntax: strlwr(string) strupr(string)
  • 6.
     strncpy() It usedto copy ‘n’ characters of one string to another.  strstr() It is used to determine the first occurrence of a given string in another string.  strncat() It appends source string to destination string up to specified length.  strspn() It is used to find up to what length two strings are identical.
  • 7.
     strncmp() It isused to compare ‘n’ character of two strings.  strcmpi() It is used to compare two strings without regarding the case.  strnicmp() It is used to compare first ‘n’ characters of two strings without regarding the case.  strchr() It is used to determine the first occurrence of a given character in a string.  strrchr() It is used to determine the last occurrence of a given character in a string.
  • 8.
    #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="college"; intb; clrscr(); b=strlen(a); printf("nThe length of the string is %d",b); getch(); } Output: The length of the string is 7
  • 9.
    #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="IT"; char b[ ]="Dept"; clrscr(); strcpy(a,b); printf("nThe string is %s",a); getch(); } Output: The string is Dept
  • 10.
    #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="IT"; char b[ ]="Dept"; clrscr(); strcat(a,b); printf("nThe string is %s",a); getch(); } Output: The string is ITDept
  • 11.
    #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="itdept"; char b[ ]="it"; int i; clrscr(); i=strcmp(a,b); if(i==0) printf("nstrings are equal:%d",i); else if(i<0) printf("nstring1 is less than string2:%d",i);
  • 12.
    else printf("nstring1 is greaterthan string2:%d",i); getch(); } Output: string1 is greater than string2:100
  • 13.
    #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="itdept"; clrscr(); printf("nThe string is :%s",a); strupr(a); printf("nThe string after conversion to uppercase :%s",a); strlwr(a); printf("nThe string after conversion to lowercase :%s",a); getch(); }
  • 14.
    The string is: itdept The string after conversion to uppercase :ITDEPT The string after conversion to lowercase : itdept
  • 15.
    #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="Dept"; clrscr(); printf("nThe string is %s",strrev(a)); getch(); } Output: The string is tpeD
  • 16.
    #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="itdept"; char b[15]; int i=0; clrscr(); strncpy(b,a,2); b[2]='0'; printf("nThe string is :%s",b); getch(); } Output: The string is :it
  • 17.
    #include<stdio.h> #include<conio.h> #include<string.h> void main() { int len,i,j; charstr[15]; clrscr(); printf("n Enter the string:"); scanf("%s",str); len=strlen(str);
  • 18.
    for(i=0,j=len-1;i<len/2;i++,j--) { if(str[i]!=str[j]) { printf("n The Stringis not a palindrome"); getch(); exit(0); } } printf("n The String is a palindrome"); getch(); } Output: Enter the string: abcba The String is a palindrome