Strings
Strings
H e l l o \0
char s[ ] = “Hello”;
H e l l o \0
When the compiler encounters a sequence of characters enclosed in the double
quotation marks, it appends a null character ‘\0’ at the end by default.
char s[]={‘H’, ’e’, ’l’, ‘l’, ‘o’};
H e l l o \0
int a[]={1,2,3,4,5};
char name[10]=“Nirmala”;
N i r m a l a \0
int a[10],n
Reading and writing strings
scanf()
and printf() with the conversion
character %s
Read
scanf(“%s”,string_name);
printff(“%s”,string_name);
Read a string using gets()
gets(str_name);
Print a string using puts()
puts(str_name);
char a[20] =“Nirmala”,b[20];
b=“MCA”
B=a
A>b
A==b
Operations on string(String handling
functions)
strlen()
strcpy()
strcmp()
strcat()
strrev()
Allthe string handling functions are in
string.h
strlen()
N i r m a l a \0 s1
C o l l e g e \0
s2
N i r m a l a C o l l e g e \0
s1
void stringcat(char dest[],char
source[])
{
int i;
for(i=0;dest[i]!=‘\0’;i++);
dest[[i++]=‘ ‘;
for(j=0;source[j]!=‘\0’;j++)
des[i++]=source[j];
dest[i]=‘\0’;
}
strrev
To reverse a string
strrev(string);
A N N M A R Y \0
E L S A \0
S A N G E E T H A \0
S U C H I T H R A \0