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

More Related Content

What's hot

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
 
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
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
 
C string
C stringC string
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
 

What's hot (20)

Strinng Classes in c++
Strinng Classes in c++Strinng Classes in c++
Strinng Classes in c++
 
String notes
String notesString notes
String notes
 
strings
stringsstrings
strings
 
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++
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Strings
StringsStrings
Strings
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
 
String in c programming
String in c programmingString in c programming
String in c programming
 
String in c
String in cString in c
String in c
 
String.ppt
String.pptString.ppt
String.ppt
 
C++ string
C++ stringC++ string
C++ string
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
String c
String cString c
String c
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
String functions in C
String functions in CString functions in C
String functions in C
 
C string
C stringC string
C string
 
Strings
StringsStrings
Strings
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
Strings in C
Strings in CStrings in C
Strings in C
 
Strings
StringsStrings
Strings
 

Similar to Strings part2

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
 
C q 3
C q 3C q 3
manipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptxmanipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptx
ShowribabuKanta
 
String handling
String handlingString handling
String handling
IyeTech - Pakistan
 
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
 
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 bufferkamal kotecha
 
Python data handling
Python data handlingPython data handling
Python data handling
Prof. Dr. K. Adisesha
 
c programming
c programmingc programming
c programming
Arun Umrao
 
Week6_P_String.pptx
Week6_P_String.pptxWeek6_P_String.pptx
Week6_P_String.pptx
OluwafolakeOjo
 
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++
Azeemaj101
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
8759000398
 

Similar to Strings part2 (20)

Chap 8(strings)
Chap 8(strings)Chap 8(strings)
Chap 8(strings)
 
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
 
C q 3
C q 3C q 3
C q 3
 
manipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptxmanipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptx
 
String handling
String handlingString handling
String handling
 
Team 1
Team 1Team 1
Team 1
 
Strings
StringsStrings
Strings
 
Computer programming 2 Lesson 12
Computer programming 2  Lesson 12Computer programming 2  Lesson 12
Computer programming 2 Lesson 12
 
[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
StringsStrings
Strings
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.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
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Strings
StringsStrings
Strings
 
Python data handling
Python data handlingPython data handling
Python data handling
 
c programming
c programmingc programming
c programming
 
Week6_P_String.pptx
Week6_P_String.pptxWeek6_P_String.pptx
Week6_P_String.pptx
 
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++
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 

More from 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
 
OOAD
OOADOOAD
OOAD
OOADOOAD
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
 

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

Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 

Recently uploaded (20)

Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 

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