A
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.
visit us: http://sites.google.com/site/kumarajay7th/
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
Tells the end of string
4
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.
char s1[ ] = { bharat};
size of s1 is seven. 6 from bharat + 1 from NULL char (\0) = 7
char s2[ ] = { p , a , k , \0, };
size of s2 is four. 3 from PAK + 1 from NUL char (\0) = 4
visit us: http://sites.google.com/site/kumarajay7th/
String first program
void main( )
{
Char name[ ] = kalia" ;
printf ( "%s", name ) ;
getch();
}
void main( )
{
char name[25] ;
printf ( Enter your name: " ) ;
scanf ( "%s", name ) ;
printf ( "hello %s!", name ) ;
getch();
Format specification %s for string }
No &name in scanf();
visit us: http://sites.google.com/site/kumarajay7th/
Output:
Enter your name: Aatma
Hello Aatma!
String: First program
void main( )
{
char name[ ] = pacman" ;
int i = 0 ;
while ( i <= 7 )
{
printf ( "%c", name[i] ) ;
i++ ;
}
getch();
}
visit us: http://sites.google.com/site/kumarajay7th/
void main( )
{
char name[ ] = pacman" ;
int i = 0 ;
while ( name[i] != `\0' )
{
printf ( "%c", name[i] ) ;
i++ ;
}
getch();
}
String i/o: gets() and puts()
gets():
Purpose of gets() is to accept a string upto a new line
character into a string variable.
It automatically appends the NULL character \0 at the end of
the string.
e.g. char str[40];
gets(str);
puts():
To display a string contained in a string variable. It also adds
the new-line character \n to the string automatically.
e.g. puts(str);
visit us: http://sites.google.com/site/kumarajay7th/
String i/o: gets() and puts() example
void main( )
{
char name[25] ;
printf ( "enter your full name:\n " ) ;
scanf (%s, name ) ;
printf(\n\n %s, name);
getch();
}
Output:
Enter your full name:
Manmohan singh
Manmohan
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
Strings i/o: getchar() and putchar()
getchar():
getchar() is used to read a character through standard
input, keyboard.
e.g. char c;
c= getchar();
putchar():
putchar() is a counterpart of getchar() and used to
display a character on standard output, screen.
e.g. putchar(c);
visit us:
http://sites.google.com/site/kumarajay7th/
10
String i/o: getchar() and putchar() example
#include<stdio.h>
#include<conio.h>
void main() {
char c, text[100];
int i=0;
puts("\n enter a line of text:");
c=getchar();
while(c!='\n') {
text[i]=c;
c=getchar();
i++;
}
Output:
Enter a line of text:
I am hindustani
Entered line of text is:
I am hindustani
/* line of text is now available in text
after appending NULL character*/
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
Program to find the occurrence of a
character in a string
#include<stdio.h>
/* finding the occurance of char */
#include<conio.h>
count=0;
int main()
for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
char str[20],ch;
count++;
int i,count;
printf("\n enter a character:\n"); printf("\n occurance of %c in
scanf ("%c", &ch);
string = %d times",ch,count);
fflush(stdin);
getch();
printf("\n enter a long string\n"); return 0;
gets(str);
}
visit us: http://sites.google.com/site/kumarajay7th/
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
Length of string: strlen()
Strlen() is a system defined function used to find the
length of string excluding NULL character \0.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main() {
int i,length;
char str[20];
puts("enter a string\n");
gets(str);
/* using strlen() */
length=strlen(str);
printf("\n length of string=%d",length);
getch();
return 1;
}
Output:
Enter a string: Taj Mahal
Length of string= 9
visit us: http://sites.google.com/site/kumarajay7th/
14
String length without using strlen()
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main() {
int i,length;
char str[20];
puts("enter a string\n");
gets(str);
Output:
enter a string:
wah taj!
without using strlen()
string length= 8
/* without using strlen() */
length=0;
for(i=0;str[i]!='\0';i++)
length++;
printf(\n without using strlen()\n);
printf("\n\n string length =%d",length);
getch();
return 1;
visit us:
}
http://sites.google.com/site/kumarajay7th/
15
Copy one string to another string: strcpy()
strcpy() copies the contents of one string into another.
Similar to assignment operator (=)
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( ) {
char source[ ] = "sayonara" ;
char target[20] , str1[20];
strcpy ( target, source ) ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
strcpy(str1, japan);
printf(\n string copied in str1=%s,str1);
getch();
}
Output:
source string= sayonara
target string= Sayanora
String copied in str1=japan
visit us: http://sites.google.com/site/kumarajay7th/
16
Without using strcpy()
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main() {
int i;
char source[20];
char target[20];
puts("enter a string: ");
gets(source);
for(i=0;source[i]!='\0';i++)
target[i]=source[i];
target[i]='\0';
puts("copied string is " );
puts( target);
getch();
return 1;
}
Output:
Enter a string:
Dehradun
Copied string is
Dehradun
visit us:
http://sites.google.com/site/kumarajay7th/
17
Concatenation of 2 strings: strcat()
The process of appending one string to the
end of another string is called concatenation.
e.g str1[ ]=Sita
str2[ ]=Ram
Concatenated string str1 & str2 becomes
SitaRam
visit us:
http://sites.google.com/site/kumarajay7th/
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
Without strcat():String concatenation
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main( )
{
int i,j=0;
char source[ ] = CADD" ;
char target[30] = Centre" ;
i=0;
while (source[i]!='\0')
i++;
while(target[j]!='\0')
{
source[i]=target[j];
j++;
i++;
}
source[i]='\0';
printf ( "\nconcatenated string\n" ) ;
puts(source);
getch();
return 1;
}
Output:
Concatenated string
CADDCentre
visit us: http://sites.google.com/site/kumarajay7th/
20
Comparing 2 string: strcmp()
Checks whether two strings are same or different
Compare character by character until there is a
mismatch
Similar to relational operator (==)
Used to search a string in a list of strings
Returns a integral value(ASCII) on comparing strings
When str1 > str2 alphabetically, returns +ve value
When str1<str2 alphabetically, return ve value
When str1 = str2 alphabetically, returns 0 (zero) value
visit us:
http://sites.google.com/site/kumarajay7th/
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
Reverse of string: strrev()
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i;
char str[20];
puts("enter a string: ");
gets(str);
puts("reverse string is " );
puts( strrev(str));
getch();
return 1;
}
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
visit us: http://sites.google.com/site/kumarajay7th/
23
2D array of string
Q. WACP to accept alphabetical
name and print them without sorting.
#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]);
visit us: http://sites.google.com/site/kumarajay7th/
getch();
return 0;
}
Output
Annu
Munnu
Chunnu
Tunnu
Sanu
List of name is
Annu
Munnu
Chunnu
Tunnu
Sanu
24
Search a name in a list of names
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char name[5][20], sname[20];
int i,flag=0;
puts("enter 5 name");
for(i=0;i<5;i++)
scanf("%s",name[i]);
Buffer
clearance
fflush(stdin);
puts("enter a name to be searched");
gets(sname);
/* searching a name in a list of names */
for(i=0;i<5;i++) {
if(strcmp(name[i],sname)==0)
{
flag=1;
break;
}
}
if(flag)
printf("name found");
else
puts("not found..try again");
getch();
return 0;
}
visit us: http://sites.google.com/site/kumarajay7th/
25
Sort names alphabetically
printf("\n\n------name in ascending order--------");
#include<stdio.h>
for(i=0;i<5;i++)
#include<conio.h>
printf("\n %s ",name[i]);
#include<string.h>
getch();
int main() {
return 0;
char name[5][20], tname[20];
}
int i,j;
puts("enter 5 name");
for(i=0;i<5;i++)
scanf("%s",name[i]);
/* sorting names */
Enter 5 name
for(i=0;i<4;i++)
---name in sorted order ----Annu
for (j=i+1;j<5;j++)
Annu
Munnu
Chunnu
if(strcmp(name[i],name[j])> 0)
Chunnu
Munnu
{
sanu
Ranu
strcpy(tname,name[i]);
Ranu
Sanu
strcpy(name[i],name[j]);
strcpy(name[j],tname);
}
visit us: http://sites.google.com/site/kumarajay7th/
26
Output:
Assignment (Exercise on String)
1. WAP to check whether a string is palindrome or not.
2. WAP to convert all lowercase characters in a given string
into their UPPERCASE character.
3. WAP to convert string into numerical value using atoi()
function Such as 124 will become 124.
4. WAP to count the number of occcurance of a charcter in
a string
5. Illustrate the following function:
a. strcmp() b. strcmpi() c. stricmp()
visit us:
http://sites.google.com/site/kumarajay7th/
27