SlideShare a Scribd company logo
@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
Arrays of Strings
To create an array of strings, you use a two dimensional character array, in
which the size of the left index determines the number of strings and the
size of the right index specify the maximum length of each string.
Syntax : char str[4][20];
In the above example an array of 4 strings, with each string having a
maximum length of 20 characters, those are str[0], str[1], str[2],str[3].
@2020 Presented By Y. N. D. Aravind
3
#include <stdio.h>
void main()
{
char names[10][20];
int n,i;
printf(“n How many names ”);
scanf(“%d”,&n);
flushall();
for(i=0;i<n;i++)
{
printf(“n Enter name %d ”,i);
gets(names[i]);
}
3
printf(“n The names are n”);
for(i=0;i<n;i++)
{
puts(names[i]);
}
}
Output
How many names 3
Enter name 1 NGI
Enter name 2 NIST
Enter name 3 NIE
The names are
NGI
NIST
NIE
String Functions
C does not provide any operator to deal with strings. However, C does
have a large set of useful string handling library functions. The
corresponding header file is string.h
@2020 Presented By Y. N. D. Aravind
1. strcpy( destination string, source string ): To copy source string to destination string.
2. strcat(string1, string2 ): To append string2 at end of string1(include null char )
3. strlen( string ): Returns length of the strings( no of characters )
4. strcmp( string1, string2 ): To compare two strings and returns 0(zero) if they are equal
otherwise returns a non-zero number.
5. strchr(string,char): Locate the first occurrence of a char in a string.
6. strrev( string ): Reverse of the string.
7. strncpy( string1,string2,n): To copy n chars from string2 to string1. After copy we must place a null
char at end of string1.
8. strncat( string1, string2, n): To append n chars of string2 at end of string1. After append we must
place a null char at end of string1.
9. strcmpi(string1,string2) : Same as strcmp( ), but at the time of compare it will ignore the case
sensitive.
10. strncmp( string1, string2, n): Same as strcmp( ), but it will compare first n specified chars only.
11. strncmpi( string1, string2, n): Same as strcmpi( ), but it will compare first n specified chars only.
12. strset( s1,s2): Returns the string s1 at the first occurrence of a sub string s2.
4
strcpy( )
This function is used to copy one string to the other. Its syntax is as follows:
strcpy(string1,string2);
where string1 and string2 are one-dimensional character arrays.
This function copies the content of string2 to string1.
@2020 Presented By Y. N. D. Aravind
#include<stdio.h>
#include<conio.h>
Void main ()
{
char string1[30],string2[30];
printf(“n Enter first string : ”);
gets(string1);
printf(“n Enter second string:”);
gets(string2);
strcpy(string1,string2);
printf(“n First string = %s”,string1);
printf(“n Second string =
%s”,string2);
}
5
OUTPUT
Enter first string : master
Enter second string : madam
First string = madam
Second string = madam
strcat ( )
This function is used to concatenate two strings. i.e., it appends one string at the end of the
specified string. Its syntax as follows :
strcat(string1,string2);
where string1 and string2 are one-dimensional character arrays.
This function joins two strings together. In other words, it adds the string2 to string1 and the
string1 contains the final concatenated string. E.g., string1 contains prog and string2 contains ram,
then string1 holds program after execution of the strcat() function.
@2020 Presented By Y. N. D. Aravind
#include<stdio.h>
#include<conio.h>
Void main()
{
char string1[30],string2[30];
printf(“n Enter first string : ”);
gets(string1);
printf(“n Enter second string:”);
gets(string2);
strcat(string1,string2);
printf(“n First string = %s”,string1);
printf(“n Second string = %s”,string2);
}
6
OUTPUT
Enter first string : prog
Enter second string : ram
First string = program
Second string = ram
strncat( )
In the previous slide we discussed strcat() function, which is used for concatenation of one string
to another string. In this guide, we will see a similar function strncat(), which is same as strcat()
except that strncat() appends only the specified number of characters to the destination string.
Syntax:- char *strncat(char *str1, const char *str2, size_t n);
str1 – Destination string.
str2 – Source string which is appended at the end of destination string str1.
n – number of characters of source string str2 that needs to be appended.
@2020 Presented By Y. N. D. Aravind 7
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[50], str2[50];
//destination string strcpy(str1, "This is my initial string");
//source string strcpy(str2, ", add this");
//displaying destination string printf("String after concatenation: %sn", strncat(str1, str2, 5));
// this should be same as return value of strncat() printf("Destination String str1: %s", str1);
return 0;
} OUTPUT
String after concatenation: This is my initial string, add
Destination String str1: This is my initial string, add
strlen( )
This function is used to find the length of the string excluding the NULL character. In other words,
this function is used to count the number of characters in a string. Its syntax is as follows:
int strlen(string);
Example: char str1[ ] = “WELCOME”;
int n;
n = strlen(str1);
@2020 Presented By Y. N. D. Aravind
#include<stdio.h>
#include<conio.h>
Void main()
{
char string1[50];
int length;
printf(“n Enter any string : ”);
gets(string1);
length=strlen(string1);
printf(“n The length of string = %d”,length);
}
8
OUTPUT
Enter any string : WELCOME
The length of string = 7
strcmp ( )
This function compares two strings character by character (ASCII comparison) and returns one of
three values {-1,0,1}. The numeric difference is „0‟ if strings are equal .If it is negative string1 is
alphabetically above string2 .If it is positive string2 is alphabetically above string1.
Its syntax is as follows : int strcmp(string1,string2);
Example : char str1[ ] = “ROM”;
char str2[ ] =”RAM”;
strcmp(str1,str2); (or) strcmp(“ROM”,”RAM”);
@2020 Presented By Y. N. D. Aravind
#include<stdio.h>
#include<conio.h>
Void main()
{
char string1[30],string2[15];
int x;
printf(“n Enter first string:”);
gets(string1);
printf(“n Enter second string:”);
gets(string2);
x=strcmp(string1,string2);
if(x==0)
printf(“n Both strings are equal”);
else if(x>0)
printf(“n First string is bigger”);
else
printf(“n Second string is bigger”);
}
9
OUTPUT
Enter first string : ROM
Enter second string : RAM
First string is bigger
strncmp ( )
In the last tutorial we discussed strcmp() function which is used for comparing
two strings. In this guide, we will discuss strncmp() function which is same as
strcmp(), except that strncmp() comparison is limited to the number of
characters specified during the function call. For example strncmp(str1, str2, 4)
would compare only the first four characters of strings str1 and str2.
Syntax:-
int strncmp(const char *str1, const char *str2, size_t n)
str1 – First String
str2 – Second String
n – number of characters that needs to be compared.
Return value of strncmp()
This function compares only the first n (specified number of) characters of
strings and returns following value based on the comparison.
0, if both the strings str1 and str2 are equal
>0, if the ASCII value of first unmatched character of str1 is greater than str2
<0, if the ASCII value of first unmatched character of str1 is less than str2
@2020 Presented By Y. N. D. Aravind 10
strrev ( )
The function can be used to reverse a string.
Syntax:- strrev(str);
@2020 Presented By Y. N. D. Aravind
#include<stdio.h>
#include<conio.h>
Void main
{
char str1[10];
printf(“n enter string”);
gets(str);
strrev(str);
puts(“The reverse string of a given string is ”);
puts(str);
}
11
OUTPUT
Enter string : COLLEGE
The reverse string of a given string is EGELLOC
strchar( )
The function strchr() searches the occurrence of a specified character in the given string and
returns the pointer to it.
Syntax :-
char *strchr(const char *str, int ch)str – The string in which the character is searched.
ch – The character that is searched in the string str.
Return Value of strchr()
It returns the pointer to the first occurrence of the character in the given string, which means that if
we display the string value of the pointer then it should display the part of the input string starting
from the first occurrence of the specified character.
@2020 Presented By Y. N. D. Aravind
#include <stdio.h>
#include <string.h>
int main ()
{
const char str[] = "This is just a String";
const char ch = 'u';
char *p;
p = strchr(str, ch);
printf("String starting from %c is: %s", ch, p);
return 0;
}
12
Output
String starting from u is: ust a String
Write a program to print whether the string is palindrome or not.
@2020 Presented By Y. N. D. Aravind
#include <stdio.h>
#include <string.h>
int main ()
{
char str[10],str1[10];
int x;
printf(“n Enter string ”);
gets(str);
strcpy(str1,str)
strrev(str);
x=strcmp(str,str1);
if(x==0)
{
printf(“n %s is palindrome”,str);
}
else
{
printf(“n %s is not palindrome”,str);
}
}
13
Output -1
Enter string liril
liril is palindrome
Output -2
Enter string newton
newton is not palindrome
Thank You
@2020 Presented By Y. N. D. Aravind
Presented By
Y. N. D. ARAVIND
M.Tech, Dept of CSE
Newton’s Group Of Institutions, Macherla
14
Ad

More Related Content

What's hot (20)

Strinng Classes in c++
Strinng Classes in c++Strinng Classes in c++
Strinng Classes in c++
Vikash Dhal
 
String notes
String notesString notes
String notes
Prasadu Peddi
 
strings
stringsstrings
strings
teach4uin
 
String in programming language in c or c++
 String in programming language  in c or c++  String in programming language  in c or c++
String in programming language in c or c++
Samsil Arefin
 
C programming - String
C programming - StringC programming - String
C programming - String
Achyut Devkota
 
Strings
StringsStrings
Strings
Mitali Chugh
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
Fazila Sadia
 
String in c programming
String in c programmingString in c programming
String in c programming
Devan Thakur
 
String in c
String in cString in c
String in c
Suneel Dogra
 
String.ppt
String.pptString.ppt
String.ppt
ajeela mushtaq
 
C++ string
C++ stringC++ string
C++ string
Dheenadayalan18
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
Neeru Mittal
 
String c
String cString c
String c
thirumalaikumar3
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
Appili Vamsi Krishna
 
String functions in C
String functions in CString functions in C
String functions in C
baabtra.com - No. 1 supplier of quality freshers
 
C string
C stringC string
C string
University of Potsdam
 
Strings
StringsStrings
Strings
Nilesh Dalvi
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
Aliul Kadir Akib
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Strings
StringsStrings
Strings
Michael Gordon
 

Similar to Strings part2 (20)

Chap 8(strings)
Chap 8(strings)Chap 8(strings)
Chap 8(strings)
Bangabandhu Sheikh Mujibur Rahman Science and Technology University
 
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
SowmyaJyothi3
 
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledgeUNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
2024163103shubham
 
C q 3
C q 3C q 3
C q 3
Rahul Vishwakarma
 
manipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptxmanipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptx
ShowribabuKanta
 
Principals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdfPrincipals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdf
anilcsbs
 
String handling
String handlingString handling
String handling
IyeTech - Pakistan
 
Team 1
Team 1Team 1
Team 1
Sathasivam Rangasamy
 
Strings
StringsStrings
Strings
Saranya saran
 
Computer programming 2 Lesson 12
Computer programming 2  Lesson 12Computer programming 2  Lesson 12
Computer programming 2 Lesson 12
MLG College of Learning, Inc
 
[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++
Muhammad Hammad Waseem
 
Strings
StringsStrings
Strings
Dhiviya Rose
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
yndaravind
 
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjkPOEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
Sowmya Jyothi
 
Module-2_Strings concepts in c programming
Module-2_Strings concepts in c programmingModule-2_Strings concepts in c programming
Module-2_Strings concepts in c programming
CHAITRAB29
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
PROBLEM SOLVING USING A PPSC- UNIT -3.pdf
PROBLEM SOLVING USING A PPSC- UNIT -3.pdfPROBLEM SOLVING USING A PPSC- UNIT -3.pdf
PROBLEM SOLVING USING A PPSC- UNIT -3.pdf
JNTUK KAKINADA
 
Strings
StringsStrings
Strings
naslin prestilda
 
Python data handling
Python data handlingPython data handling
Python data handling
Prof. Dr. K. Adisesha
 
Ad

More from yndaravind (13)

Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
yndaravind
 
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
 
OOAD
OOADOOAD
OOAD
yndaravind
 
OOAD
OOADOOAD
OOAD
yndaravind
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
yndaravind
 
Repetations in C
Repetations in CRepetations in C
Repetations in C
yndaravind
 
Selection & Making Decisions in c
Selection & Making Decisions in cSelection & Making Decisions in c
Selection & Making Decisions in c
yndaravind
 
Bitwise Operators in C
Bitwise Operators in CBitwise Operators in C
Bitwise Operators in C
yndaravind
 
Structure In C
Structure In CStructure In C
Structure In C
yndaravind
 
Arrays In C
Arrays In CArrays In C
Arrays In C
yndaravind
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
yndaravind
 
Functions part1
Functions part1Functions part1
Functions part1
yndaravind
 
Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
yndaravind
 
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 C
yndaravind
 
Selection & Making Decisions in c
Selection & Making Decisions in cSelection & Making Decisions in c
Selection & Making Decisions in c
yndaravind
 
Bitwise Operators in C
Bitwise Operators in CBitwise Operators in C
Bitwise Operators in C
yndaravind
 
Structure In C
Structure In CStructure In C
Structure In C
yndaravind
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
yndaravind
 
Functions part1
Functions part1Functions part1
Functions part1
yndaravind
 
Ad

Recently uploaded (20)

World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 

Strings part2

  • 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. Arrays of Strings To create an array of strings, you use a two dimensional character array, in which the size of the left index determines the number of strings and the size of the right index specify the maximum length of each string. Syntax : char str[4][20]; In the above example an array of 4 strings, with each string having a maximum length of 20 characters, those are str[0], str[1], str[2],str[3]. @2020 Presented By Y. N. D. Aravind 3 #include <stdio.h> void main() { char names[10][20]; int n,i; printf(“n How many names ”); scanf(“%d”,&n); flushall(); for(i=0;i<n;i++) { printf(“n Enter name %d ”,i); gets(names[i]); } 3 printf(“n The names are n”); for(i=0;i<n;i++) { puts(names[i]); } } Output How many names 3 Enter name 1 NGI Enter name 2 NIST Enter name 3 NIE The names are NGI NIST NIE
  • 4. String Functions C does not provide any operator to deal with strings. However, C does have a large set of useful string handling library functions. The corresponding header file is string.h @2020 Presented By Y. N. D. Aravind 1. strcpy( destination string, source string ): To copy source string to destination string. 2. strcat(string1, string2 ): To append string2 at end of string1(include null char ) 3. strlen( string ): Returns length of the strings( no of characters ) 4. strcmp( string1, string2 ): To compare two strings and returns 0(zero) if they are equal otherwise returns a non-zero number. 5. strchr(string,char): Locate the first occurrence of a char in a string. 6. strrev( string ): Reverse of the string. 7. strncpy( string1,string2,n): To copy n chars from string2 to string1. After copy we must place a null char at end of string1. 8. strncat( string1, string2, n): To append n chars of string2 at end of string1. After append we must place a null char at end of string1. 9. strcmpi(string1,string2) : Same as strcmp( ), but at the time of compare it will ignore the case sensitive. 10. strncmp( string1, string2, n): Same as strcmp( ), but it will compare first n specified chars only. 11. strncmpi( string1, string2, n): Same as strcmpi( ), but it will compare first n specified chars only. 12. strset( s1,s2): Returns the string s1 at the first occurrence of a sub string s2. 4
  • 5. strcpy( ) This function is used to copy one string to the other. Its syntax is as follows: strcpy(string1,string2); where string1 and string2 are one-dimensional character arrays. This function copies the content of string2 to string1. @2020 Presented By Y. N. D. Aravind #include<stdio.h> #include<conio.h> Void main () { char string1[30],string2[30]; printf(“n Enter first string : ”); gets(string1); printf(“n Enter second string:”); gets(string2); strcpy(string1,string2); printf(“n First string = %s”,string1); printf(“n Second string = %s”,string2); } 5 OUTPUT Enter first string : master Enter second string : madam First string = madam Second string = madam
  • 6. strcat ( ) This function is used to concatenate two strings. i.e., it appends one string at the end of the specified string. Its syntax as follows : strcat(string1,string2); where string1 and string2 are one-dimensional character arrays. This function joins two strings together. In other words, it adds the string2 to string1 and the string1 contains the final concatenated string. E.g., string1 contains prog and string2 contains ram, then string1 holds program after execution of the strcat() function. @2020 Presented By Y. N. D. Aravind #include<stdio.h> #include<conio.h> Void main() { char string1[30],string2[30]; printf(“n Enter first string : ”); gets(string1); printf(“n Enter second string:”); gets(string2); strcat(string1,string2); printf(“n First string = %s”,string1); printf(“n Second string = %s”,string2); } 6 OUTPUT Enter first string : prog Enter second string : ram First string = program Second string = ram
  • 7. strncat( ) In the previous slide we discussed strcat() function, which is used for concatenation of one string to another string. In this guide, we will see a similar function strncat(), which is same as strcat() except that strncat() appends only the specified number of characters to the destination string. Syntax:- char *strncat(char *str1, const char *str2, size_t n); str1 – Destination string. str2 – Source string which is appended at the end of destination string str1. n – number of characters of source string str2 that needs to be appended. @2020 Presented By Y. N. D. Aravind 7 #include <stdio.h> #include <string.h> int main () { char str1[50], str2[50]; //destination string strcpy(str1, "This is my initial string"); //source string strcpy(str2, ", add this"); //displaying destination string printf("String after concatenation: %sn", strncat(str1, str2, 5)); // this should be same as return value of strncat() printf("Destination String str1: %s", str1); return 0; } OUTPUT String after concatenation: This is my initial string, add Destination String str1: This is my initial string, add
  • 8. strlen( ) This function is used to find the length of the string excluding the NULL character. In other words, this function is used to count the number of characters in a string. Its syntax is as follows: int strlen(string); Example: char str1[ ] = “WELCOME”; int n; n = strlen(str1); @2020 Presented By Y. N. D. Aravind #include<stdio.h> #include<conio.h> Void main() { char string1[50]; int length; printf(“n Enter any string : ”); gets(string1); length=strlen(string1); printf(“n The length of string = %d”,length); } 8 OUTPUT Enter any string : WELCOME The length of string = 7
  • 9. strcmp ( ) This function compares two strings character by character (ASCII comparison) and returns one of three values {-1,0,1}. The numeric difference is „0‟ if strings are equal .If it is negative string1 is alphabetically above string2 .If it is positive string2 is alphabetically above string1. Its syntax is as follows : int strcmp(string1,string2); Example : char str1[ ] = “ROM”; char str2[ ] =”RAM”; strcmp(str1,str2); (or) strcmp(“ROM”,”RAM”); @2020 Presented By Y. N. D. Aravind #include<stdio.h> #include<conio.h> Void main() { char string1[30],string2[15]; int x; printf(“n Enter first string:”); gets(string1); printf(“n Enter second string:”); gets(string2); x=strcmp(string1,string2); if(x==0) printf(“n Both strings are equal”); else if(x>0) printf(“n First string is bigger”); else printf(“n Second string is bigger”); } 9 OUTPUT Enter first string : ROM Enter second string : RAM First string is bigger
  • 10. strncmp ( ) In the last tutorial we discussed strcmp() function which is used for comparing two strings. In this guide, we will discuss strncmp() function which is same as strcmp(), except that strncmp() comparison is limited to the number of characters specified during the function call. For example strncmp(str1, str2, 4) would compare only the first four characters of strings str1 and str2. Syntax:- int strncmp(const char *str1, const char *str2, size_t n) str1 – First String str2 – Second String n – number of characters that needs to be compared. Return value of strncmp() This function compares only the first n (specified number of) characters of strings and returns following value based on the comparison. 0, if both the strings str1 and str2 are equal >0, if the ASCII value of first unmatched character of str1 is greater than str2 <0, if the ASCII value of first unmatched character of str1 is less than str2 @2020 Presented By Y. N. D. Aravind 10
  • 11. strrev ( ) The function can be used to reverse a string. Syntax:- strrev(str); @2020 Presented By Y. N. D. Aravind #include<stdio.h> #include<conio.h> Void main { char str1[10]; printf(“n enter string”); gets(str); strrev(str); puts(“The reverse string of a given string is ”); puts(str); } 11 OUTPUT Enter string : COLLEGE The reverse string of a given string is EGELLOC
  • 12. strchar( ) The function strchr() searches the occurrence of a specified character in the given string and returns the pointer to it. Syntax :- char *strchr(const char *str, int ch)str – The string in which the character is searched. ch – The character that is searched in the string str. Return Value of strchr() It returns the pointer to the first occurrence of the character in the given string, which means that if we display the string value of the pointer then it should display the part of the input string starting from the first occurrence of the specified character. @2020 Presented By Y. N. D. Aravind #include <stdio.h> #include <string.h> int main () { const char str[] = "This is just a String"; const char ch = 'u'; char *p; p = strchr(str, ch); printf("String starting from %c is: %s", ch, p); return 0; } 12 Output String starting from u is: ust a String
  • 13. Write a program to print whether the string is palindrome or not. @2020 Presented By Y. N. D. Aravind #include <stdio.h> #include <string.h> int main () { char str[10],str1[10]; int x; printf(“n Enter string ”); gets(str); strcpy(str1,str) strrev(str); x=strcmp(str,str1); if(x==0) { printf(“n %s is palindrome”,str); } else { printf(“n %s is not palindrome”,str); } } 13 Output -1 Enter string liril liril is palindrome Output -2 Enter string newton newton is not palindrome
  • 14. Thank You @2020 Presented By Y. N. D. Aravind Presented By Y. N. D. ARAVIND M.Tech, Dept of CSE Newton’s Group Of Institutions, Macherla 14