SlideShare a Scribd company logo
1 of 15
Download to read offline
@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
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
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
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
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
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
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
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
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
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
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
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
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
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”);
}
}
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

More Related Content

What's hot (20)

C programming - String
C programming - StringC programming - String
C programming - String
 
C Pointers
C PointersC Pointers
C Pointers
 
C string
C stringC string
C string
 
Strings in c
Strings in cStrings in c
Strings in c
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
Array and string
Array and stringArray and string
Array and string
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Function in c
Function in cFunction in c
Function in c
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Enums in c
Enums in cEnums in c
Enums in c
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Pointers
PointersPointers
Pointers
 
File in c
File in cFile in c
File in c
 
Command line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialCommand line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorial
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 

Similar to Strings IN C

Similar to Strings IN C (20)

String notes
String notesString notes
String notes
 
string , pointer
string , pointerstring , pointer
string , pointer
 
Array &strings
Array &stringsArray &strings
Array &strings
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
Week6_P_String.pptx
Week6_P_String.pptxWeek6_P_String.pptx
Week6_P_String.pptx
 
14 strings
14 strings14 strings
14 strings
 
Unit 2
Unit 2Unit 2
Unit 2
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
String
StringString
String
 
String_C.pptx
String_C.pptxString_C.pptx
String_C.pptx
 
CP Handout#8
CP Handout#8CP Handout#8
CP Handout#8
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
Strings part2
Strings part2Strings part2
Strings part2
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
 
Unitii string
Unitii stringUnitii string
Unitii string
 

More from yndaravind

Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UMLyndaravind
 
Classes and Objects
Classes and Objects  Classes and Objects
Classes and Objects yndaravind
 
The Object Model
The Object Model  The Object Model
The Object Model yndaravind
 
Repetations in C
Repetations in CRepetations in C
Repetations in Cyndaravind
 
Selection & Making Decisions in c
Selection & Making Decisions in cSelection & Making Decisions in c
Selection & Making Decisions in cyndaravind
 
Bitwise Operators in C
Bitwise Operators in CBitwise Operators in C
Bitwise Operators in Cyndaravind
 
Structure In C
Structure In CStructure In C
Structure In Cyndaravind
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2yndaravind
 
Functions part1
Functions part1Functions part1
Functions part1yndaravind
 

More from yndaravind (13)

Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
 
Classes and Objects
Classes and Objects  Classes and Objects
Classes and Objects
 
The Object Model
The Object Model  The Object Model
The Object Model
 
OOAD
OOADOOAD
OOAD
 
OOAD
OOADOOAD
OOAD
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Repetations in C
Repetations in CRepetations in C
Repetations in C
 
Selection & Making Decisions in c
Selection & Making Decisions in cSelection & Making Decisions in c
Selection & Making Decisions in c
 
Bitwise Operators in C
Bitwise Operators in CBitwise Operators in C
Bitwise Operators in C
 
Structure In C
Structure In CStructure In C
Structure In C
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 
Functions part1
Functions part1Functions part1
Functions part1
 

Recently uploaded

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 

Recently uploaded (20)

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 

Strings IN C

  • 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