String in C Language
String in C Language
Lecture on
String
in C
Prepared by:
AJAY KUMAR
Assistant Professor
Department of CSE
DIT University, Dehradun
Strings
a group of integers is an integer array
a group of characters is a character array , also
called strings.
Character arrays (or Strings) is used to
manipulate text such as words and sentences.
string constant is a one-dimensional array of
characters
E.g char mytext=a;
single char
char mystring[100]=DIT University
visit us: http://sites.google.com/site/kumarajay7th/
string
String definition
A string in C is defined as a sequence of characters
terminated by a special character \0.
The special character \0 is called NULL character.
NULL character is to indicate the end of string.
String representation
char mystring=firstTime;
char myarr[ ] = welcome;
char myarr[ ]= { w , e , l , c , o , m , e , \0 };
0th
1st
2nd
3rd
4th
5th
Indexing position
visit us: http://sites.google.com/site/kumarajay7th/
6th
7th
\0
String initialization
char str1[5]={ i , n , d , i , \0}; size five
char str2[5]= {indi}; size five
srting str2 will append \0 automatically at the end of the string.
void main( )
{
char name[25] ;
printf ( Enter your name: " ) ;
scanf ( "%s", name ) ;
Output:
Enter your name: Aatma
Hello Aatma!
void main( )
{
char name[ ] = pacman" ;
int i = 0 ;
while ( name[i] != `\0' )
{
printf ( "%c", name[i] ) ;
i++ ;
}
getch();
}
Truncated singh
void main( )
{
char name[25] ;
printf ( "enter your full name: " ) ;
gets ( name ) ;
puts ( "hello!" ) ;
puts ( name ) ;
getch();
}
Output:
Enter your full name:
soniya gandhi
Hello!
9
Soniya gandhi
10
text[i]='\0';
/* displaying the line of text using
putchar() */
puts("\n entered line of text is ");
for(i=0; text[i]!='\0';i++)
putchar(text[i]);
getch();
}
visit us: http://sites.google.com/site/kumarajay7th/
11
12
String Manipulations
Commonly used operations over strings:
Finding the length of string
Copy one string to another string
Concatenation of two strings
Comparing two strings
Reverse of a string
Searching substring in a string
visit us: http://sites.google.com/site/kumarajay7th/
13
Output:
Enter a string: Taj Mahal
Length of string= 9
14
Output:
enter a string:
wah taj!
15
Output:
source string= sayonara
target string= Sayanora
String copied in str1=japan
16
Output:
Enter a string:
Dehradun
Copied string is
Dehradun
visit us:
http://sites.google.com/site/kumarajay7th/
17
18
strcat() example
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main( )
{
char source[ ] = At" ;
char target[30] = Bhar" ;
strcat ( target, source ) ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
getch();
}
visit us:
http://sites.google.com/site/kumarajay7th/
output...
source string = At
target string = BharAt
19
source[i]='\0';
printf ( "\nconcatenated string\n" ) ;
puts(source);
getch();
return 1;
}
Output:
Concatenated string
CADDCentre
20
21
strcmp() example
void main( )
{
char string1[ ] = "jerry" ;
char string2[ ] = "ferry" ;
int i, j, k ;
i = strcmp ( string1, "jerry" ) ;
j = strcmp ( string1, string2 ) ;
k = strcmp ( string1, "jerry boy" ) ;
printf ( "\n%d %d %d", i, j, k ) ;
getch();
}
Output:
0 4 -32
jerry
: Ascii of \0= 0
jerry boy: Ascii of blank space = 32
visit us:
http://sites.google.com/site/kumarajay7th/
22
Output: 1 Output: 2
Enter a string;
REVINU
Enter a string;
ALFLA
Reverse string is
UNIVER
Reverse string is
ALFLA
Output: 3
Enter a string;
Reverse string is
23
2D array of string
Q. WACP to accept alphabetical
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main() {
char name[5][20];
int i;
puts("enter 5 name");
for(i=0;i<5;i++)
scanf("%s",name[i]);
/* printing the name */
puts("list of names is");
for(i=0;i<5;i++)
puts(name[i]);
getch();
return 0;
}
Output
Annu
Munnu
Chunnu
Tunnu
Sanu
List of name is
Annu
Munnu
Chunnu
Tunnu
Sanu
24
Buffer
clearance
fflush(stdin);
puts("enter a name to be searched");
gets(sname);
25
Output:
27