Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

 STRING IS A SET OF CHARACTERS

GROUPED INTO SINGLE UNIT

 EX. “XYZ”
 EX. “123”
 EX.”XYZ123”
 SYNTAX:
 <DATATYPE>ARRAYNAME[SIZE];
 EX: char str[3];

 Another way of declaring string in C

 Syntax : char *stringname;


 Ex : char *studentname;
 STRING CAN BE INITIALIZED AS It’s a
delimiter
 EX : char str[3]={“xyz”}; x y z \0 called
NULL
character
Ex: char str[5]={‘h’,’e’,’l’,’l’,’o’}; h e l l o \0

Ex: char str[ ] ={“New York”}; N e w Y o r k \0


 strlen( ) string length
 strcpy( ) string copy
 strcmp( ) string comparison
 strcat( ) string concatenation
 strstr() substring
 This function is used to find the length of the
given string

 Syntax: <int> strlen(string);

 Ex: L =strlen(s);
Length L =3

a b c \0

s
 Ex: Write a C program to find the length of the
string.
 Void main( )
 {
 int l; char str[6];
 printf(“Enter string\n”);
 gets(str);
 l=strlen(str);
 Printf(“%d”,l);
 }
 This function is used to copy the content from
one string to another
 Syntax: strcpy(<destination string>,<source
string>);
 Ex: strcpy(str2,str1);
W E L C O M E \O

str1 str2

Strcpy(str2,str1)

W E L C O M E \0

str1
 Ex: Write a C program to copy one string to
another.
 Void main( )
 {
 char str1[8]; char str2[8];
 printf(“Enter string\n”);
 gets(str2);
 strcpy(str1,str2);
 puts(str1); // printf(“%s”,str1);
 }
 This function is used to compare between two
strings.
 It returns true value if the strings are similar.
 It returns false value if the strings are not
similar. Syntax: int strcmp(<string1>,<string2>);

W E L C O M E \0 S1
Strcmp(s1,s2)

W E L C O M E \0 S2 Strings are similar


A B C \0 S1

a b c \0 S2

Strings are not equal


 Void main( )
 {
 char str1[8]; char str2[8]; int x;
 printf(“Enter two string\sn”);
 gets(str1); gets(str2);
 x=strcmp(str1,str2);
 if(x==1)
 printf(“Strings are similar”);
 else
 printf(“Srtings are not similar”);

 }
 This function is used to combine one string to
another.
 syntax: strcat(<string1>,<string2>);

W E L \0 S1

C O M E \0
S2

Strcat(s1,s2);

W E L C O M E \0 S1
 void main( )
 {
 char s1[10],s2[10];
 printf(“enter strings”);
 gets(s1); gets(s2);
 strcat(s1,s2);
 puts(s1);
 }
 It finds the first occurrence of a substring or
given string in another string.
 It returns a pointer(address) to the position
from where the string starts.
 If the string is not found it returns null.\

 Syntax : char * strstr(char *string, char * string);


 #include<stdio.h>
 #include<string.h>
 void main()
 {
 char s1[20];
 char s2[5];
 clrscr();
 printf("Enter the string");
 gets(s1);
 printf("enter sub string ");
 gets(s2);
 printf("\n%s",strstr(s1,s2));
 getch();
 }
 Syntax: char <arrayname> [num][length];
 Eg:

0 R A M Y A \0
1 S U N N Y \0
2 U M A \0
3 M A D H U \0
 void main( )
 {
 int i;
 char name[4][10]={“RAMYA”,”SUNNY”,”UMA”,MADHU”};
 printf(“list of names are “);
 for(i=0;i<4;i++)
 {
 printf(“%s\n”,name[i]);
 }
 }

You might also like