1. @2020 Presented By Y. N. D. Aravind 1
Presented By
Y. N. D. ARAVIND
M.Tech, Dept of CSE
Newton‟s Group Of Institutions, Macherla
2. Session Objectives
Explain C Strings
Explain String Input / Output Functions
Explain Arrays of Strings
Explain String Concepts
@2020 Presented By Y. N. D. Aravind
2
Explain Strings Manipulation Functions
2
3. Strings are array of characters, i.e., they are characters arranged one after
another in memory. To mark the end of the string, C uses a NULL is specified
as „0‟. A string is enclosed within double quotes.
char string_name[size];
1) A string variable is any valid C variable name and is always declared as an array.
2) The size determines the number of characters in the string name
char strname[ length ];
char name[10];
char name[5];
In the above example, single char variables are name[0], name[1], name[2], name[3],
name[4] and a string variable is name.
An array of char is also called a string variable, since it can store a string and it ermits us to
change its contents. In contrast a sequence of characters enclosed within a pair of double
quotes is called a string constant.
char name[20]=”Newtons”;
char name[ ]=”Macherla”;
@2020 Presented By Y. N. D. Aravind
3
3
4. Initialization of a String
The syntax of initializing a string variable has two variations :
Variation 1 :
char str1[8] = { „N‟,‟E‟,‟W‟,‟T‟,‟O‟,‟N‟,‟S‟,‟0‟};
@2020 Presented By Y. N. D. Aravind
4
Here,str1 is declared to be a string variable with size eight.
Variation 2 :
char str2[9] = {“Macherla”};
Here,str2 is declared to be a string variable with size nine, it can store
maximum nine characters including null character.
In either of these variations , the size of the character array can be skipped,
in which case the size and the number of characters in the initializer – list
would be automatically supplied by the compiler.
4
5. Initialization of a String
char ch[10]={„A‟,‟P‟,‟P‟,‟L‟,‟E‟,‟0‟};
(or)
Char ch[10]=”APPLE”;
@2020 Presented By Y. N. D. Aravind
5
In the above example, first character „A‟ stores in the ch[0] and second character „P‟ stores
in ch[1] and so on in the character array ch.
After storing all characters into the array, the compiler appends a null character to the array.
The null character is represented as „0‟.
A character array with null character(„0‟) is known as a string.
This null character specifies end of string.
Example :-
There is no separate data type given by „C‟ for declaring string variables.
They can be declared using char data type.
To print the strings on the monitor we can use printf(). The format specifier to print strings is
%s.
A P P L E 0
5
6. String Input /Output Functions
Scanf() and printf()
@2020 Presented By Y. N. D. Aravind
6
Scanf() is used to accept a string through standard input, keyboard. The syntax of its usage
is as follows
scanf(“%s”, str);
Accepts a string into str upto a white character(Blank space, New line character, Tab
space). The null character „0‟ will be automatically appended to the string.
Ex:- char str[6];
scanf(“%s”, str);
if the input is : hello world
Only “hello” would be taken by str.
Ex:- char str1[6], str2[6];
scanf(“%s %s”, str1,str2);
if the input is : hello world
“hello” would be taken by str1 and “world” would be taken by str2
Syntax of using printf() to display the string is :
printf(“%s”, str);
if str has string “ hello world”,
printf(“%s”,str) would display “hello world”.
6
7. String Input /Output Functions
getchar() and putchar()
@2020 Presented By Y. N. D. Aravind
7
getchar() is used to read a character from the terminal. It takes the following form :
ch = getchar();
Where ch is a variable of char type.
putchar() is a counterpart of getchar(). It is used to display a character on the monitor takes the following
form:
putchar(ch);
7
#include <stdio.h>
void main( )
{ char str[20],ch;
int i;
printf("Enter a line of characters n");
ch = getchar();
i=0;
while(ch != „n‟)
{
Str[i] = ch;
Ch= getchar();
i++;
}
Str[i]=„0‟;
printf(“The lines of characters entered is n");
for(i=0; str[i] != „0‟; i++)
putchar(str[i]);
getch();
}
Output
Enter a line of characters
My name is computer
The lines of characters entered is
My name is computer
8. String Input /Output Functions
gets() and puts()
@2020 Presented By Y. N. D. Aravind
8
The purpose of gets() is used to accept a string upto a new line character into a string
variable. It automatically appends the null character „0‟ to the end of the string. It takes the
following form :
gets(str);
The purpose of puts() is to display a string contained in a string variable . It also appends
the new-line character „n‟ to the string automatically, as a result of which the cusor is moved
down by one line after the string is displayed.
puts(str);
8
Write a program to read string from the keyboard.
#include <stdio.h>
main()
{
char ch[20];
printf(“n Enter string”);
gets(ch);
printf(“n The given string is %s”,ch);
}
OUTPUT:-
Enter string NGI COLLEGE
The given string is NGI COLLEGE
9. Write a program to print the length of given string.
@2020 Presented By Y. N. D. Aravind
9
#include <stdio.h>
Void main()
{
char ch[20];
int i, len=0;
printf(“n Enter string”);
gets(ch);
for(i=0;ch[i]!=‟0‟;i++)
{
len++;
}
printf(“nLength of the given string is %d”,len);
}
9
OUTPUT:-
Enter string COLLEGE
Length of the given string is 7
10. Write a program to print the reverse of given string.
@2020 Presented By Y. N. D. Aravind
10
#include <stdio.h>
void main()
{
char ch[20],rev[20];
int len=0,i,j;
printf(“n Enter string”);
gets(ch);
for(i=0;ch[i]!=‟0‟;i++)
{
len++;
}
for(j=0,i=len-1; i>=0; j++,i--)
{
rev[j]=ch[i];
}
rev[j]=‟0‟;
printf(“n Reverse of %s is %s”,ch,rev);
}
10
OUTPUT:-
Enter string COLLEGE
Reverse of COLLEGE is EGELLOC
11. Write a program to detect the occurance of a character in a given string.
@2020 Presented By Y. N. D. Aravind
11
#include <stdio.h>
void main()
{
char ch[20],s;
int len=0,count=0,i;
printf(“n Enter string”);
gets(ch);
printf(“n Enter character to be occured”);
s=getchar();
for(i=0;ch[i]!=‟0‟;i++)
{
len++;
}
for(i=0;i<len;i++)
{
if(ch[i]==s)
{
count++;
}
}
printf(“n %c occurs %d”,s,count);
}
11
OUTPUT:-
Enter string COLLEGE
Enter character to be occurred L
L occurs 2
12. Write a program to copy the string without using string function.
@2020 Presented By Y. N. D. Aravind
12
#include <stdio.h>
main()
{
char str1[20],str2[20];
int len=0,i;
printf(“n Enter string ”);
gets(str1);
for(i=0;str[i]!=‟0‟;i++)
{
len++;
}
for(i=0;i<len;i++)
{
str2[i]=str1[i];
}
puts(str2);
puts(str1);
}
12
OUTPUT:-
Enter string COLLEGE
COLLEGE
COLLEGE
13. Write a program to concatenate two strings without using string functions.
@2020 Presented By Y. N. D. Aravind
13
#include <stdio.h>
Void main()
{
char str1[20],str2[20];
int s1=0,s2=0,i,j=0;
printf(“n Enter string1”);
gets(str1);
printf(“n Enter string2”);
gets(str2);
for(i=0;str1[i]!=‟0‟;i++)
{
s1++;
}
for(i=0;str2[i]!=‟0‟;i++)
{
s2++;
}
for(i=s1;i<s1+s2;i++,j++)
{
str2[i]=str1[j];
}
str2[i] =„0‟;
puts(str2);
puts(str1);
}
13
OUTPUT:-
Enter string1 hard
Enter string2 work
workhard
hard
14. Write a program to compare the strings without using string function.
@2020 Presented By Y. N. D. Aravind
14
#include <stdio.h>
main()
{
char str1[20],str2[20];
int len1=0,len2=0,c=0;
printf(“n Enter string1”);
gets(str1);
printf(“n Enter string2”);
gets(str2);
for(i=0;str1[i]!=‟0‟;i++)
{
len1++;
}
for(i=0;str2[i]!=‟0‟;i++)
{
len2++;
}
14
OUTPUT:-
Enter string1 college
Enter string2 collage
Strings are not equal
if(len1==len2)
{
for(i=0;i<len1;i++)
{
if(str1[i]!=str2[i])
{
c=1;
}
}
if(c==1)
{
printf(“n Strings are not equal”)
}
else
{
printf(“n String are equal”);
}
}
else
{
printf(“n Strings are not equal”);
}
}
15. Thank You
@2020 Presented By Y. N. D. Aravind
15
Presented By
Y. N. D. ARAVIND
M.Tech, Dept of CSE
Newton‟s Group Of Institutions, Macherla