SlideShare a Scribd company logo
1 of 31
String
5/21/2015 3:26 PM1 Prepared by Achyut Devkota
Achyut Devkota
Kathford International College
String
5/21/2015 3:26 PMPrepared by Achyut Devkota2
 A group of characters
 A string constant is a one-dimensional array
of characters terminated by a null ( ‘0’ ).
declaration of Character:
char mychar;
declaration of String:
char myString[10];
The characters after the null character are
ignored.
Initialization String
5/21/2015 3:26 PMPrepared by Achyut Devkota3
Initialization Syntax:
char myString [] = { 'H','A','E','S', 'L', 'E', 'R', '0' } ;
char myString[13] = “Initial value”
char myString[] = “Initial value”;
0 = null character
Note: that ‘0’ and ‘0’ are not same.
 When declaring a string don’t forget to leave a
space for the null character which is also
known as the string terminator character
Compilation time
initialization
n i t i a l v a l u e ? ? …I 0
5/21/2015 3:26 PMPrepared by Achyut Devkota4
Why Null
char ?
only way the functions that work with
a string can know where the string
ends.
n i t i a l v a l u e ? ? …I 0
char myString[100] = “Initial value”
Initialization String
5/21/2015 3:26 PMPrepared by Achyut Devkota5
Run time
Initialization
Character array :
Using Input/ output function :
Scanf() gets() getchar()
Common Error
5/21/2015 3:26 PMPrepared by Achyut Devkota6
The following results in an error:
1. char str1 [5]=“Hello”;
2. char str1[6];
ctr1=“Hello”;
3. char str1[6] = “Hello”;
char str2[6];
str2 = str1;
//Results in Error
Input Function
5/21/2015 3:26 PMPrepared by Achyut Devkota7
 The scanf() Function
 header file stdio.h
 Syntax:
char mystring[100];
scanf(“%s”, mystring);
 The name of a string is a pointer constant to the
first character in the character array.
 Problem:
terminates its input on the first white space it
finds.
white space includes blanks, tabs, carriage
returns(CR), form feeds & new line.
Example:
5/21/2015 3:26 PMPrepared by Achyut Devkota8
Output:
Input Function
5/21/2015 3:26 PMPrepared by Achyut Devkota9
 The gets() Function
 Header file stdio.h
 takes a string from standard input and assigns
it to a character array.
 It replaces the n with 0.
 Syntax:
char mystring[100];
gets(myString);
fgets() it keeps the n and includes it as part of the string.
Example:
5/21/2015 3:26 PMPrepared by Achyut Devkota10
Output:
Input Function
5/21/2015 3:26 PMPrepared by Achyut Devkota11
 The getchar() Function
 Takes single character at a time.
 Syntax:
char mychar;
mychar=getchar();
It can use to read each character of an string.
int i;
char mystring[100];
printf("Enter String:n");
for(i=0;i<10;i++)
mystring[i]=getchar();
mystring[9]='0';
printf("nn%s",mystring);
Example
:
Example:
5/21/2015 3:26 PMPrepared by Achyut Devkota12
What should be the output if entered string
is:
Hello boys
Output function
5/21/2015 3:26 PMPrepared by Achyut Devkota13
 The printf () function
 header file stdio.h
(self study)
 The puts() function
 header file stdio.h
Output function
5/21/2015 3:26 PMPrepared by Achyut Devkota14
 The putchar() Function
 output single character at a time.
 Syntax:
char mychar;
mychar=getchar();
It can use to read each character of an string.
Example int i;
char mystring[100];
printf("Enter String:n");
gets(mystring);
for(i=0;i<20;i++)
putchar(mystring[i]);
Example:
5/21/2015 3:26 PMPrepared by Achyut Devkota15
Rarely use in array
manipulation .
String operation – string.h
5/21/2015 3:26 PMPrepared by Achyut Devkota16
 Four main library function which is define in
string.h header file
strcpy() - copy one string into another
strcat() - append one string onto the right side of
the other
strcmp() – compare alphabetic order of two
strings
strlen() – return the length of a string
strcpy()
5/21/2015 3:26 PMPrepared by Achyut Devkota17
 strcpy(destinationstring, sourcestring)
 Copies sourcestring into destinationstring
 For example
Output
strcat()
5/21/2015 3:26 PMPrepared by Achyut Devkota18
 strcat() function to combine two strings into a new
string.
 strcat(destinationstring, sourcestring)
 appends sourcestring to right hand side of
destinationstring
 We need to be certain that the array to which we
assign the resulting string is large enough to hold all
the characters from the two contributing strings.
 Syntax:
strcat(str1, str2);
Example: strcat()
5/21/2015 3:26 PMPrepared by Achyut Devkota19
Output
strcmp()
5/21/2015 3:26 PMPrepared by Achyut Devkota20
 Compares str1 and str2 alphabetically
strcmp(str1, str2)
 If the two strings are equal, strcmp() returns 0.
 If str1 is greater than str2, strcmp() returns a
positive number.
 If str1 is less than str2, strcmp() returns a
negative number.
strcmp()
5/21/2015 3:26 PMPrepared by Achyut Devkota21
Example:
5/21/2015 3:26 PMPrepared by Achyut Devkota22
Output
strlen()
5/21/2015 3:26 PMPrepared by Achyut Devkota23
 Strlen() function to determine the length of
a string.
 It counts the number of characters in a string,
excluding the null character.
 Syntax:
strcount = strlen(myString);
Example:
5/21/2015 3:26 PMPrepared by Achyut Devkota24
Output:
More …
5/21/2015 3:26 PMPrepared by Achyut Devkota25
 strlwr() : converts a string to lowercase
 Strupr() : converts a string to uppercase
 Strncat() : Appends first n characters of a string
at the end of another
 Strncmp() :Compares first n characters of two
strings
 Strcmpi():Compares two strings without regard
to case ("i" denotes that this function ignores
case)
 Strrev() :Reverses string
 Strncpy() : copy first n character of one string to
another.
Example:
5/21/2015 3:26 PMPrepared by Achyut Devkota26
Output:
Problem:
5/21/2015 3:26 PMPrepared by Achyut Devkota27
 Write a program to input a string and
rearrange the string in alphabetical order. For
example, the word NEPAL should be written
as AELNP
What is the key difference between
‘A’ and “A” ?
5/21/2015 3:26 PMPrepared by Achyut Devkota28
 The representation of a char (e.g., ‘A’) and a string
(e.g., “A”) is essentially different.
 A string is an array of characters ended with the null
character.
A
Character ‘Q’
A 0
String “Q”
Two dimensional- string array
5/21/2015 3:26 PMPrepared by Achyut Devkota29
 An array of strings is a two-dimensional array
of characters in which each row is one string.
char names[std_number][Name_Lth];
char month[5][10] = {“January”,
“February”, “March”, “April”,
“May”};
Example:
5/21/2015 3:26 PMPrepared by Achyut Devkota30
Output
5/21/2015 3:26 PMPrepared by Achyut Devkota31

More Related Content

What's hot (20)

Structure & union
Structure & unionStructure & union
Structure & union
 
C string
C stringC string
C string
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
String in c programming
String in c programmingString in c programming
String in c programming
 
2D Array
2D Array 2D Array
2D Array
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
C tokens
C tokensC tokens
C tokens
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
 
String functions in C
String functions in CString functions in C
String functions in C
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Character set of c
Character set of cCharacter set of c
Character set of c
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Function in C
Function in CFunction in C
Function in C
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
 

Viewers also liked

Viewers also liked (20)

C programming string
C  programming stringC  programming string
C programming string
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
Strings
StringsStrings
Strings
 
Strings
StringsStrings
Strings
 
Implementation of c string functions
Implementation of c string functionsImplementation of c string functions
Implementation of c string functions
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
 
Array in c language
Array in c languageArray in c language
Array in c language
 
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++
 
Structure in C
Structure in CStructure in C
Structure in C
 
Structure in c
Structure in cStructure in c
Structure in c
 
structure and union
structure and unionstructure and union
structure and union
 
String
StringString
String
 
String functions
String functionsString functions
String functions
 
String functions and operations
String functions and operations String functions and operations
String functions and operations
 
Telephony application - voiceInn and architecture
Telephony application - voiceInn and architectureTelephony application - voiceInn and architecture
Telephony application - voiceInn and architecture
 
AyurMed
AyurMedAyurMed
AyurMed
 
C++ Preprocessor Directives
C++ Preprocessor DirectivesC++ Preprocessor Directives
C++ Preprocessor Directives
 
Web Project Presentation - JoinPakForces
Web Project Presentation - JoinPakForcesWeb Project Presentation - JoinPakForces
Web Project Presentation - JoinPakForces
 
String image targets
String image targetsString image targets
String image targets
 
C programming - Pointer and DMA
C programming - Pointer and DMAC programming - Pointer and DMA
C programming - Pointer and DMA
 

Similar to C programming - String

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.pdfSowmyaJyothi3
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdfssusere19c741
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its functionFrankie Jones
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and stringsRai University
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsRai University
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-aneebkmct
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsRai University
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and stringsRai University
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsRai University
 

Similar to C programming - String (20)

14 strings
14 strings14 strings
14 strings
 
[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
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
Strings
StringsStrings
Strings
 
String_C.pptx
String_C.pptxString_C.pptx
String_C.pptx
 
Matlab strings
Matlab stringsMatlab strings
Matlab strings
 
Strings
StringsStrings
Strings
 
Week6_P_String.pptx
Week6_P_String.pptxWeek6_P_String.pptx
Week6_P_String.pptx
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
 
U4.ppt
U4.pptU4.ppt
U4.ppt
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
 
Savitch Ch 08
Savitch Ch 08Savitch Ch 08
Savitch Ch 08
 
String notes
String notesString notes
String notes
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
 
pps unit 3.pptx
pps unit 3.pptxpps unit 3.pptx
pps unit 3.pptx
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
 

Recently uploaded

Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 

Recently uploaded (20)

(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 

C programming - String

  • 1. String 5/21/2015 3:26 PM1 Prepared by Achyut Devkota Achyut Devkota Kathford International College
  • 2. String 5/21/2015 3:26 PMPrepared by Achyut Devkota2  A group of characters  A string constant is a one-dimensional array of characters terminated by a null ( ‘0’ ). declaration of Character: char mychar; declaration of String: char myString[10]; The characters after the null character are ignored.
  • 3. Initialization String 5/21/2015 3:26 PMPrepared by Achyut Devkota3 Initialization Syntax: char myString [] = { 'H','A','E','S', 'L', 'E', 'R', '0' } ; char myString[13] = “Initial value” char myString[] = “Initial value”; 0 = null character Note: that ‘0’ and ‘0’ are not same.  When declaring a string don’t forget to leave a space for the null character which is also known as the string terminator character Compilation time initialization n i t i a l v a l u e ? ? …I 0
  • 4. 5/21/2015 3:26 PMPrepared by Achyut Devkota4 Why Null char ? only way the functions that work with a string can know where the string ends. n i t i a l v a l u e ? ? …I 0 char myString[100] = “Initial value”
  • 5. Initialization String 5/21/2015 3:26 PMPrepared by Achyut Devkota5 Run time Initialization Character array : Using Input/ output function : Scanf() gets() getchar()
  • 6. Common Error 5/21/2015 3:26 PMPrepared by Achyut Devkota6 The following results in an error: 1. char str1 [5]=“Hello”; 2. char str1[6]; ctr1=“Hello”; 3. char str1[6] = “Hello”; char str2[6]; str2 = str1; //Results in Error
  • 7. Input Function 5/21/2015 3:26 PMPrepared by Achyut Devkota7  The scanf() Function  header file stdio.h  Syntax: char mystring[100]; scanf(“%s”, mystring);  The name of a string is a pointer constant to the first character in the character array.  Problem: terminates its input on the first white space it finds. white space includes blanks, tabs, carriage returns(CR), form feeds & new line.
  • 8. Example: 5/21/2015 3:26 PMPrepared by Achyut Devkota8 Output:
  • 9. Input Function 5/21/2015 3:26 PMPrepared by Achyut Devkota9  The gets() Function  Header file stdio.h  takes a string from standard input and assigns it to a character array.  It replaces the n with 0.  Syntax: char mystring[100]; gets(myString); fgets() it keeps the n and includes it as part of the string.
  • 10. Example: 5/21/2015 3:26 PMPrepared by Achyut Devkota10 Output:
  • 11. Input Function 5/21/2015 3:26 PMPrepared by Achyut Devkota11  The getchar() Function  Takes single character at a time.  Syntax: char mychar; mychar=getchar(); It can use to read each character of an string. int i; char mystring[100]; printf("Enter String:n"); for(i=0;i<10;i++) mystring[i]=getchar(); mystring[9]='0'; printf("nn%s",mystring); Example :
  • 12. Example: 5/21/2015 3:26 PMPrepared by Achyut Devkota12 What should be the output if entered string is: Hello boys
  • 13. Output function 5/21/2015 3:26 PMPrepared by Achyut Devkota13  The printf () function  header file stdio.h (self study)  The puts() function  header file stdio.h
  • 14. Output function 5/21/2015 3:26 PMPrepared by Achyut Devkota14  The putchar() Function  output single character at a time.  Syntax: char mychar; mychar=getchar(); It can use to read each character of an string. Example int i; char mystring[100]; printf("Enter String:n"); gets(mystring); for(i=0;i<20;i++) putchar(mystring[i]);
  • 15. Example: 5/21/2015 3:26 PMPrepared by Achyut Devkota15 Rarely use in array manipulation .
  • 16. String operation – string.h 5/21/2015 3:26 PMPrepared by Achyut Devkota16  Four main library function which is define in string.h header file strcpy() - copy one string into another strcat() - append one string onto the right side of the other strcmp() – compare alphabetic order of two strings strlen() – return the length of a string
  • 17. strcpy() 5/21/2015 3:26 PMPrepared by Achyut Devkota17  strcpy(destinationstring, sourcestring)  Copies sourcestring into destinationstring  For example Output
  • 18. strcat() 5/21/2015 3:26 PMPrepared by Achyut Devkota18  strcat() function to combine two strings into a new string.  strcat(destinationstring, sourcestring)  appends sourcestring to right hand side of destinationstring  We need to be certain that the array to which we assign the resulting string is large enough to hold all the characters from the two contributing strings.  Syntax: strcat(str1, str2);
  • 19. Example: strcat() 5/21/2015 3:26 PMPrepared by Achyut Devkota19 Output
  • 20. strcmp() 5/21/2015 3:26 PMPrepared by Achyut Devkota20  Compares str1 and str2 alphabetically strcmp(str1, str2)  If the two strings are equal, strcmp() returns 0.  If str1 is greater than str2, strcmp() returns a positive number.  If str1 is less than str2, strcmp() returns a negative number.
  • 21. strcmp() 5/21/2015 3:26 PMPrepared by Achyut Devkota21
  • 22. Example: 5/21/2015 3:26 PMPrepared by Achyut Devkota22 Output
  • 23. strlen() 5/21/2015 3:26 PMPrepared by Achyut Devkota23  Strlen() function to determine the length of a string.  It counts the number of characters in a string, excluding the null character.  Syntax: strcount = strlen(myString);
  • 24. Example: 5/21/2015 3:26 PMPrepared by Achyut Devkota24 Output:
  • 25. More … 5/21/2015 3:26 PMPrepared by Achyut Devkota25  strlwr() : converts a string to lowercase  Strupr() : converts a string to uppercase  Strncat() : Appends first n characters of a string at the end of another  Strncmp() :Compares first n characters of two strings  Strcmpi():Compares two strings without regard to case ("i" denotes that this function ignores case)  Strrev() :Reverses string  Strncpy() : copy first n character of one string to another.
  • 26. Example: 5/21/2015 3:26 PMPrepared by Achyut Devkota26 Output:
  • 27. Problem: 5/21/2015 3:26 PMPrepared by Achyut Devkota27  Write a program to input a string and rearrange the string in alphabetical order. For example, the word NEPAL should be written as AELNP
  • 28. What is the key difference between ‘A’ and “A” ? 5/21/2015 3:26 PMPrepared by Achyut Devkota28  The representation of a char (e.g., ‘A’) and a string (e.g., “A”) is essentially different.  A string is an array of characters ended with the null character. A Character ‘Q’ A 0 String “Q”
  • 29. Two dimensional- string array 5/21/2015 3:26 PMPrepared by Achyut Devkota29  An array of strings is a two-dimensional array of characters in which each row is one string. char names[std_number][Name_Lth]; char month[5][10] = {“January”, “February”, “March”, “April”, “May”};
  • 30. Example: 5/21/2015 3:26 PMPrepared by Achyut Devkota30 Output
  • 31. 5/21/2015 3:26 PMPrepared by Achyut Devkota31