SlideShare a Scribd company logo
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

PPTX
Structure in C
Kamal Acharya
 
PPTX
HTML
chinesebilli
 
PPTX
2D Array
Ehatsham Riaz
 
PPTX
Presentation on Function in C Programming
Shuvongkor Barman
 
PPS
Projection of lines with problems
India
 
PPTX
Regression analysis
University of Jaffna
 
PPTX
Classification techniques in data mining
Kamal Acharya
 
PPTX
Electric and Hybrid vehicles
K C KARTHIK
 
Structure in C
Kamal Acharya
 
2D Array
Ehatsham Riaz
 
Presentation on Function in C Programming
Shuvongkor Barman
 
Projection of lines with problems
India
 
Regression analysis
University of Jaffna
 
Classification techniques in data mining
Kamal Acharya
 
Electric and Hybrid vehicles
K C KARTHIK
 

What's hot (20)

PPTX
Unit 6. Arrays
Ashim Lamichhane
 
PPTX
Strings in C
Kamal Acharya
 
PPTX
String in c programming
Devan Thakur
 
PPT
Strings
Mitali Chugh
 
PPTX
User defined functions in C
Harendra Singh
 
PPT
String c
thirumalaikumar3
 
PDF
Character Array and String
Tasnima Hamid
 
PDF
Strings IN C
yndaravind
 
PPTX
C functions
University of Potsdam
 
PPTX
Union in C programming
Kamal Acharya
 
PPT
structure and union
student
 
PPTX
Structures in c language
tanmaymodi4
 
PPT
Strings in c
vampugani
 
PDF
Arrays in Java
Naz Abdalla
 
PPTX
Pointers in c language
Tanmay Modi
 
PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PPTX
C string
University of Potsdam
 
PPTX
Array Of Pointers
Sharad Dubey
 
PPT
Formatted input and output
Online
 
Unit 6. Arrays
Ashim Lamichhane
 
Strings in C
Kamal Acharya
 
String in c programming
Devan Thakur
 
Strings
Mitali Chugh
 
User defined functions in C
Harendra Singh
 
Character Array and String
Tasnima Hamid
 
Strings IN C
yndaravind
 
Union in C programming
Kamal Acharya
 
structure and union
student
 
Structures in c language
tanmaymodi4
 
Strings in c
vampugani
 
Arrays in Java
Naz Abdalla
 
Pointers in c language
Tanmay Modi
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Array Of Pointers
Sharad Dubey
 
Formatted input and output
Online
 
Ad

Viewers also liked (20)

DOC
String in c
Suneel Dogra
 
PPSX
C programming string
argusacademy
 
PPT
Strings Functions in C Programming
DevoAjit Gupta
 
PPT
Strings
Nilesh Dalvi
 
PDF
Strings
Michael Gordon
 
PDF
Implementation of c string functions
mohamed sikander
 
PPT
Computer Programming- Lecture 5
Dr. Md. Shohel Sayeed
 
PPTX
Array in c language
home
 
PPTX
String in programming language in c or c++
Samsil Arefin
 
PPT
Structure in C
Fazle Rabbi Ador
 
PPTX
String
Samsil Arefin
 
PPTX
String functions
Kumar Krishnan
 
PPT
String functions and operations
Mudasir Syed
 
PPTX
Telephony application - voiceInn and architecture
Achyut Devkota
 
PPTX
AyurMed
Achyut Devkota
 
PPT
C++ Preprocessor Directives
Wasif Altaf
 
PPT
Web Project Presentation - JoinPakForces
Wasif Altaf
 
PDF
String image targets
Fabricio Begalli
 
PPTX
C programming - Pointer and DMA
Achyut Devkota
 
String in c
Suneel Dogra
 
C programming string
argusacademy
 
Strings Functions in C Programming
DevoAjit Gupta
 
Strings
Nilesh Dalvi
 
Implementation of c string functions
mohamed sikander
 
Computer Programming- Lecture 5
Dr. Md. Shohel Sayeed
 
Array in c language
home
 
String in programming language in c or c++
Samsil Arefin
 
Structure in C
Fazle Rabbi Ador
 
String functions
Kumar Krishnan
 
String functions and operations
Mudasir Syed
 
Telephony application - voiceInn and architecture
Achyut Devkota
 
C++ Preprocessor Directives
Wasif Altaf
 
Web Project Presentation - JoinPakForces
Wasif Altaf
 
String image targets
Fabricio Begalli
 
C programming - Pointer and DMA
Achyut Devkota
 
Ad

Similar to C programming - String (20)

PPTX
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
monimhossain14
 
PDF
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 6.pdf
amanpathak160605
 
PPT
14 strings
Rohit Shrivastava
 
PDF
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
PPTX
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PDF
0-Slot21-22-Strings.pdf
ssusere19c741
 
PPTX
unit-5 String Math Date Time AI presentation
MukeshTheLioner
 
DOCX
Type header file in c++ and its function
Frankie Jones
 
PPTX
String_C.pptx
HARSHITHA EBBALI
 
PDF
Matlab strings
pramodkumar1804
 
PPTX
Lecture_on_string_manipulation_functions.pptx
ABHISRIVASTAV9
 
PPT
Strings
Imad Ali
 
PPTX
Week6_P_String.pptx
OluwafolakeOjo
 
PPTX
Diploma ii cfpc u-4 function, storage class and array and strings
Rai University
 
PPTX
Btech i pic u-4 function, storage class and array and strings
Rai University
 
PDF
Functions torage class and array and strings-
aneebkmct
 
PPT
Savitch Ch 08
Terry Yoast
 
PDF
String notes
Prasadu Peddi
 
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
monimhossain14
 
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 6.pdf
amanpathak160605
 
14 strings
Rohit Shrivastava
 
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
0-Slot21-22-Strings.pdf
ssusere19c741
 
unit-5 String Math Date Time AI presentation
MukeshTheLioner
 
Type header file in c++ and its function
Frankie Jones
 
String_C.pptx
HARSHITHA EBBALI
 
Matlab strings
pramodkumar1804
 
Lecture_on_string_manipulation_functions.pptx
ABHISRIVASTAV9
 
Strings
Imad Ali
 
Week6_P_String.pptx
OluwafolakeOjo
 
Diploma ii cfpc u-4 function, storage class and array and strings
Rai University
 
Btech i pic u-4 function, storage class and array and strings
Rai University
 
Functions torage class and array and strings-
aneebkmct
 
Savitch Ch 08
Terry Yoast
 
String notes
Prasadu Peddi
 

Recently uploaded (20)

PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
Hyogeun Oh
 
PPTX
unit 3a.pptx material management. Chapter of operational management
atisht0104
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PDF
Software Testing Tools - names and explanation
shruti533256
 
PDF
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PDF
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Introduction to Data Science: data science process
ShivarkarSandip
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
Hyogeun Oh
 
unit 3a.pptx material management. Chapter of operational management
atisht0104
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
Software Testing Tools - names and explanation
shruti533256
 
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
Inventory management chapter in automation and robotics.
atisht0104
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Introduction to Data Science: data science process
ShivarkarSandip
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 

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