SlideShare a Scribd company logo
1 of 17
String Operations
1
Instructor
Dhiviya Rose J , AP-Sr. Scale | SoCSE
INFO 106 – Computer Programming
Definition
• A sequence of characters is often referred to as a
character “string”.
• No explicit type, instead strings are maintained as
arrays of characters
• Representing strings in C
▫ stored in arrays of characters
▫ array can be of any length
▫ end of string is indicated by a delimiter, the zero character ‘0’
Character Vs Strings
Will be
considered as
string
Will be
considered as
character array
char myarr[]={‘h’,’e’,’l’,’l’,’o’,’0’}; char myarr[]={‘h’,’e’,’l’,’l’,’o’};
INFO 106 – Computer Programming
String Declaration & Initialization
• A string constant is a sequence of characters
enclosed in double quotes.
char a[10]=“Hello”;
Or
char *colorPtr = "blue";
pointer
INFO 106 – Computer Programming
String Input
• Use %s field specification in scanf to read string
• Example:
char myarr[11];
scanf(“%s”,&myarr);
5
Only the
name of the
string
INFO 106 – Computer Programming
Example
#include <stdio.h>
void main()
{
char LName[10];
char FName[10];
printf("Enter your name : ");
scanf("%s %s",&LName,&FName);
printf("Nice to meet you %s %sn“,FName,LName);
}
6
INFO 106 – Computer Programming
String Functions
• string functions are used for performing
different string tasks
• Functions come from the utility library string.h
▫ #include <string.h>
• Examples
strlen(str) - calculate string length
strcpy(dst,src) - copy string at src to dst
strcmp(str1,str2) - compare str1 to str2
7
INFO 106 – Computer Programming
Standard Library
• String handling library has functions to
▫ Manipulate string data
▫ Search strings
▫ Tokenize strings
▫ Determine string length
8
INFO 106 – Computer Programming
String Length
Syntax: int strlen(char *str)
returns the length (integer) of the string argument
Example:
char str1 = “hello”;
int a;
a=strlen(str1);
10
Function prototype Function description
char *strcpy( char *s1,
const char *s2 )
Copies string s2 into array s1. The value of s1 is
returned.
char *strncpy( char *s1,
const char *s2, size_t n )
Copies at most n characters of string s2 into array s1.
The value of s1 is returned.
char *strcat( char *s1,
const char *s2 )
Appends string s2 to array s1. The first character of
s2 overwrites the terminating null character of s1.
The value of s1 is returned.
char *strncat( char *s1,
const char *s2, size_t n )
Appends at most n characters of string s2 to array s1.
The first character of s2 overwrites the terminating
null character of s1. The value of s1 is returned.
INFO 106 – Computer Programming
String Comparison
Syntax:
int strcmp(char *str1, char *str2)
compares str1 to str2, returns a value based on the first character
they differ at:
Answer < 0 if 2 string are less than or equal to
> 0 if 2 string are greater than
= 0 if the two strings are equal
INFO 106 – Computer Programming
String Comparison (cont)
strcmp examples:
strcmp(“hello”,”hello”) -- returns 0
strcmp(“yello”,”hello”) -- returns value > 0
strcmp(“Hello”,”hello”) -- returns value < 0
strcmp(“hello”,”hello there”) -- returns value < 0
strcmp(“some diff”,”some dift”) -- returns value < 0
expression for determining if two strings s1,s2
hold the same string value:
!strcmp(s1,s2)
INFO 106 – Computer Programming
String Comparison (ignoring case)
Syntax:
int strcasecmp(char *str1, char *str2)
similar to strcmp except that upper and lower case characters
(e.g., ‘a’ and ‘A’) are considered to be equal
int strncasecmp(char *str1, char *str2, int n)
version of strncmp that ignores case
INFO 106 – Computer Programming
Copying a String
WAP to create 3 string variable ,
String1 = Happy
String2=New Year
Op1: join s1+s2
Op2: Copy s1 to s3
151 /* Fig. 8.19: fig08_19.c
2 Using strcat and strncat */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8 char s1[ 20 ] = "Happy ";
9 char s2[] = "New Year ";
10 char s3[ 40 ] = "";
11
12 printf( "s1 = %sns2 = %sn", s1, s2 );
13 printf( "strcat( s1, s2 ) = %sn", strcat( s1, s2 ) );
14 printf( "strncat( s3, s1, 6 ) = %sn", strncat( s3, s1, 6 ) );
15 printf( "strcat( s3, s1 ) = %sn", strcat( s3, s1 ) );
16 return 0;
17 }
INFO 106 – Computer Programming
Searching for a Character/String
Syntax:
char *strchr(char *str, int ch)
returns a pointer (a char *) to the first occurrence of ch in str
returns NULL if ch does not occur in str
INFO 106 – Computer Programming
17

More Related Content

What's hot (20)

Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
Strings
StringsStrings
Strings
 
Strings
StringsStrings
Strings
 
MySQL 5.7 String Functions
MySQL 5.7 String FunctionsMySQL 5.7 String Functions
MySQL 5.7 String Functions
 
C++ string
C++ stringC++ string
C++ string
 
Strings
StringsStrings
Strings
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
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++
 
Strings in c
Strings in cStrings in c
Strings in c
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
 
String function in my sql
String function in my sqlString function in my sql
String function in my sql
 
The string class
The string classThe string class
The string class
 
Strings in C
Strings in CStrings in C
Strings in C
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Unitii string
Unitii stringUnitii string
Unitii string
 
strings
stringsstrings
strings
 
Strings in c++
Strings in c++Strings in c++
Strings in 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++
String in programming language in c or c++
 
string in C
string in Cstring in C
string in C
 

Similar to Strings

CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTSasideepa
 
BHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPTBHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPTSasideepa
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxAbhimanyuChaure
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programmingmikeymanjiro2090
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxJStalinAsstProfessor
 
String & its application
String & its applicationString & its application
String & its applicationTech_MX
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsJamesChristianGadian
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiSowmya Jyothi
 
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
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 

Similar to Strings (20)

CP-STRING (1).ppt
CP-STRING (1).pptCP-STRING (1).ppt
CP-STRING (1).ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 
CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPT
 
BHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPTBHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPT
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 
String & its application
String & its applicationString & its application
String & its application
 
Strings CPU GTU
Strings CPU GTUStrings CPU GTU
Strings CPU GTU
 
c programming
c programmingc programming
c programming
 
Strings part2
Strings part2Strings part2
Strings part2
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
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
 
String notes
String notesString notes
String notes
 
Team 1
Team 1Team 1
Team 1
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 

More from Dhiviya Rose

Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2Dhiviya Rose
 
Programming for Problem Solving Unit 1
Programming for Problem Solving Unit 1Programming for Problem Solving Unit 1
Programming for Problem Solving Unit 1Dhiviya Rose
 
Module 3 microsoft powerpoint
Module 3 microsoft powerpointModule 3 microsoft powerpoint
Module 3 microsoft powerpointDhiviya Rose
 
Module 3 business computing.pdf
Module 3 business computing.pdfModule 3 business computing.pdf
Module 3 business computing.pdfDhiviya Rose
 
Module 2 Digital Devices and its Applications
Module 2 Digital Devices and its ApplicationsModule 2 Digital Devices and its Applications
Module 2 Digital Devices and its ApplicationsDhiviya Rose
 
Unit 1 Business Computing
Unit 1 Business ComputingUnit 1 Business Computing
Unit 1 Business ComputingDhiviya Rose
 
Module 1 - Digital Devices and its Application
Module 1 - Digital Devices and its ApplicationModule 1 - Digital Devices and its Application
Module 1 - Digital Devices and its ApplicationDhiviya Rose
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsDhiviya Rose
 
CSEG1001 Unit 4 Functions and Pointers
CSEG1001 Unit 4 Functions and PointersCSEG1001 Unit 4 Functions and Pointers
CSEG1001 Unit 4 Functions and PointersDhiviya Rose
 
CSEG1001 Unit 5 Structure and Unions
CSEG1001 Unit 5 Structure and UnionsCSEG1001 Unit 5 Structure and Unions
CSEG1001 Unit 5 Structure and UnionsDhiviya Rose
 
CSEG1001Unit 2 C Programming Fundamentals
CSEG1001Unit 2 C Programming FundamentalsCSEG1001Unit 2 C Programming Fundamentals
CSEG1001Unit 2 C Programming FundamentalsDhiviya Rose
 
CSEG1001 Lecture 1 Introduction to Computers
CSEG1001 Lecture 1 Introduction to ComputersCSEG1001 Lecture 1 Introduction to Computers
CSEG1001 Lecture 1 Introduction to ComputersDhiviya Rose
 
Lecture 3 internet and web
Lecture 3 internet and webLecture 3 internet and web
Lecture 3 internet and webDhiviya Rose
 
Multidimentional array
Multidimentional arrayMultidimentional array
Multidimentional arrayDhiviya Rose
 
Searching in Arrays
Searching in ArraysSearching in Arrays
Searching in ArraysDhiviya Rose
 

More from Dhiviya Rose (16)

Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2
 
Programming for Problem Solving Unit 1
Programming for Problem Solving Unit 1Programming for Problem Solving Unit 1
Programming for Problem Solving Unit 1
 
Module 3 microsoft powerpoint
Module 3 microsoft powerpointModule 3 microsoft powerpoint
Module 3 microsoft powerpoint
 
Module 3 business computing.pdf
Module 3 business computing.pdfModule 3 business computing.pdf
Module 3 business computing.pdf
 
Module 2 Digital Devices and its Applications
Module 2 Digital Devices and its ApplicationsModule 2 Digital Devices and its Applications
Module 2 Digital Devices and its Applications
 
Software
SoftwareSoftware
Software
 
Unit 1 Business Computing
Unit 1 Business ComputingUnit 1 Business Computing
Unit 1 Business Computing
 
Module 1 - Digital Devices and its Application
Module 1 - Digital Devices and its ApplicationModule 1 - Digital Devices and its Application
Module 1 - Digital Devices and its Application
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and Strings
 
CSEG1001 Unit 4 Functions and Pointers
CSEG1001 Unit 4 Functions and PointersCSEG1001 Unit 4 Functions and Pointers
CSEG1001 Unit 4 Functions and Pointers
 
CSEG1001 Unit 5 Structure and Unions
CSEG1001 Unit 5 Structure and UnionsCSEG1001 Unit 5 Structure and Unions
CSEG1001 Unit 5 Structure and Unions
 
CSEG1001Unit 2 C Programming Fundamentals
CSEG1001Unit 2 C Programming FundamentalsCSEG1001Unit 2 C Programming Fundamentals
CSEG1001Unit 2 C Programming Fundamentals
 
CSEG1001 Lecture 1 Introduction to Computers
CSEG1001 Lecture 1 Introduction to ComputersCSEG1001 Lecture 1 Introduction to Computers
CSEG1001 Lecture 1 Introduction to Computers
 
Lecture 3 internet and web
Lecture 3 internet and webLecture 3 internet and web
Lecture 3 internet and web
 
Multidimentional array
Multidimentional arrayMultidimentional array
Multidimentional array
 
Searching in Arrays
Searching in ArraysSearching in Arrays
Searching in Arrays
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 

Recently uploaded (20)

TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 

Strings

  • 2. INFO 106 – Computer Programming Definition • A sequence of characters is often referred to as a character “string”. • No explicit type, instead strings are maintained as arrays of characters • Representing strings in C ▫ stored in arrays of characters ▫ array can be of any length ▫ end of string is indicated by a delimiter, the zero character ‘0’
  • 3. Character Vs Strings Will be considered as string Will be considered as character array char myarr[]={‘h’,’e’,’l’,’l’,’o’,’0’}; char myarr[]={‘h’,’e’,’l’,’l’,’o’};
  • 4. INFO 106 – Computer Programming String Declaration & Initialization • A string constant is a sequence of characters enclosed in double quotes. char a[10]=“Hello”; Or char *colorPtr = "blue"; pointer
  • 5. INFO 106 – Computer Programming String Input • Use %s field specification in scanf to read string • Example: char myarr[11]; scanf(“%s”,&myarr); 5 Only the name of the string
  • 6. INFO 106 – Computer Programming Example #include <stdio.h> void main() { char LName[10]; char FName[10]; printf("Enter your name : "); scanf("%s %s",&LName,&FName); printf("Nice to meet you %s %sn“,FName,LName); } 6
  • 7. INFO 106 – Computer Programming String Functions • string functions are used for performing different string tasks • Functions come from the utility library string.h ▫ #include <string.h> • Examples strlen(str) - calculate string length strcpy(dst,src) - copy string at src to dst strcmp(str1,str2) - compare str1 to str2 7
  • 8. INFO 106 – Computer Programming Standard Library • String handling library has functions to ▫ Manipulate string data ▫ Search strings ▫ Tokenize strings ▫ Determine string length 8
  • 9. INFO 106 – Computer Programming String Length Syntax: int strlen(char *str) returns the length (integer) of the string argument Example: char str1 = “hello”; int a; a=strlen(str1);
  • 10. 10 Function prototype Function description char *strcpy( char *s1, const char *s2 ) Copies string s2 into array s1. The value of s1 is returned. char *strncpy( char *s1, const char *s2, size_t n ) Copies at most n characters of string s2 into array s1. The value of s1 is returned. char *strcat( char *s1, const char *s2 ) Appends string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned. char *strncat( char *s1, const char *s2, size_t n ) Appends at most n characters of string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.
  • 11. INFO 106 – Computer Programming String Comparison Syntax: int strcmp(char *str1, char *str2) compares str1 to str2, returns a value based on the first character they differ at: Answer < 0 if 2 string are less than or equal to > 0 if 2 string are greater than = 0 if the two strings are equal
  • 12. INFO 106 – Computer Programming String Comparison (cont) strcmp examples: strcmp(“hello”,”hello”) -- returns 0 strcmp(“yello”,”hello”) -- returns value > 0 strcmp(“Hello”,”hello”) -- returns value < 0 strcmp(“hello”,”hello there”) -- returns value < 0 strcmp(“some diff”,”some dift”) -- returns value < 0 expression for determining if two strings s1,s2 hold the same string value: !strcmp(s1,s2)
  • 13. INFO 106 – Computer Programming String Comparison (ignoring case) Syntax: int strcasecmp(char *str1, char *str2) similar to strcmp except that upper and lower case characters (e.g., ‘a’ and ‘A’) are considered to be equal int strncasecmp(char *str1, char *str2, int n) version of strncmp that ignores case
  • 14. INFO 106 – Computer Programming Copying a String WAP to create 3 string variable , String1 = Happy String2=New Year Op1: join s1+s2 Op2: Copy s1 to s3
  • 15. 151 /* Fig. 8.19: fig08_19.c 2 Using strcat and strncat */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main() 7 { 8 char s1[ 20 ] = "Happy "; 9 char s2[] = "New Year "; 10 char s3[ 40 ] = ""; 11 12 printf( "s1 = %sns2 = %sn", s1, s2 ); 13 printf( "strcat( s1, s2 ) = %sn", strcat( s1, s2 ) ); 14 printf( "strncat( s3, s1, 6 ) = %sn", strncat( s3, s1, 6 ) ); 15 printf( "strcat( s3, s1 ) = %sn", strcat( s3, s1 ) ); 16 return 0; 17 }
  • 16. INFO 106 – Computer Programming Searching for a Character/String Syntax: char *strchr(char *str, int ch) returns a pointer (a char *) to the first occurrence of ch in str returns NULL if ch does not occur in str
  • 17. INFO 106 – Computer Programming 17