SlideShare a Scribd company logo
1 of 16
STRINGS
1
STRINGS
•1-d arrays of type char
•By convention, a string in C is terminated by the end-
of-string sentinel ‘0’ (null character)
•char s[21] - can have variable length string delimited
with 0
•Max length of the string that can be stored is 20 as the size
must include storage needed for the ‘0’
•String constants : “hello”, “abc”
•“abc” is a character array of size 4
2
CHARACTER ARRAYS AND STRINGS
char C[4] = { 's', 'k', 'u', 'p'};
• C[0] gets the value 'a', C[1] the value 'b', and so on. The last
(7th) location receives the null character ‘0’
• Null-terminated (last character is ‘0’) character arrays are
also called strings
• Strings can be initialized in an alternative way. The last
declaration is equivalent to:
char C[4] = "skup";
• The trailing null character is missing here. C automatically
puts it at the end if you define it like this
3
READING STRINGS: %S FORMAT
4
void main()
{
char name[25];
scanf("%s", name);
printf("Name = %s n", name);
}
%s reads a string into a character array
given the array name or start address.
It ends the string with ‘0’
AN EXAMPLE
5
void main()
{
#define SIZE 25
int i, count=0;
char name[SIZE];
scanf("%s", name);
printf("Name = %s n", name);
for (i=0; name[i]!='0'; i++)
if (name[i] == 'a') count++;
printf("Total a's = %dn", count);
}
Note that character strings read
in %s format end with ‘0’
DIFFERENCES : ARRAY & POINTERS
char *p = “abcde”;
The compiler allocates space for
p, puts the string constant
“abcde” in memory somewhere
else, initializes p with the base
address of the string constant
char s[ ] = “abcde”;
 char s[ ] =
{‘a’,’b’,’c’,’d’,’e’.’0’};
The compiler allocates 6 bytes of
memory for the array s which are
initialized with the 6 characters
6
a b c d e 0
a b c d e 0
p
S
STRING CONSTANT
•A string constant is treated as a pointer
•Its value is the base address of the string
char *p = “abc”;
printf (“%s %sn”,p,p+1); /* abc bc is printed */
7
a b c 0
p
LIBRARY FUNCTIONS FOR STRING
HANDLING
• You can write your own C code to do different operations on
strings like finding the length of a string, copying one string
to another, appending one string to the end of another etc.
• C library provides standard functions for these that you can
call, so no need to write your own code
• To use them, you must do
#include <string.h>
At the beginning of your program (after #include <stdio.h>)
8
STRING FUNCTIONS
• strlen : finds the length of a string
• strcat : concatenates one string at the end of another
• strcmp : compares two strings lexicographically
• strcpy : copies one string to another
9
strlen()
int strlen(const char *s)
• Takes a null-terminated strings
(we routinely refer to the char
pointer that points to a null-
terminated char array as a
string)
• Returns the length of the
string, not counting the null
(0) character
10
int strlen (const char *s) {
int n;
for (n=0; *s!=‘0’; ++s)
++n;
return n;
}
You cannot change contents
of s in the function
STRCAT()
• char *strcat (char *s1,
const char *s2);
• Takes 2 strings as
arguments, concatenates
them, and puts the result
in s1. Returns s1.
Programmer must ensure
that s1 points to enough
space to hold the result.
11
char *strcat(char *s1, const char *s2)
{
char *p = s1;
while (*p != ‘0’) /* go to end */
++p;
while(*s2 != ‘0’)
*p++ = *s2++; /* copy */
*p = ‘0’;
return s1;
}
You cannot change contents
of s2 in the function
STRCMP()
12
int strcmp (const char *s1,
const char *s2);
Two strings are passed as
arguments. An integer is
returned that is less
than, equal to, or
greater than 0,
depending on whether
s1 is lexicographically
less than, equal to, or
greater than s2.
STRCMP()
13
int strcmp (const char *s1,
const char *s2);
Two strings are passed as
arguments. An integer is
returned that is less
than, equal to, or
greater than 0,
depending on whether
s1 is lexicographically
less than, equal to, or
greater than s2.
int strcmp(char *s1, const char *s2)
{
for (;*s1!=‘0’&&*s2!=‘0’; s1++,s2++)
{
if (*s1>*s2) return 1;
if (*s2>*s1) return -1;
}
if (*s1 != ‘0’) return 1;
if (*s2 != ‘0’) return -1;
return 0;
}
char *strcpy (char *s1, char *s2);
The characters is the string s2 are copied into s1 until 0 is
moved. Whatever exists in s1 is overwritten. It is assumed
that s1 has enough space to hold the result. The pointer s1 is
returned.
14
STRCPY()
char *strcpy (char *s1, const char *s2);
The characters is the string s2 are copied into s1 until ‘0’ is
moved. Whatever exists in s1 is overwritten. It is assumed
that s1 has enough space to hold the result. The pointer s1 is
returned.
15
char * strcpy (char *s1, const char *s2)
{
char *p = s1;
while (*p++ = *s2++) ;
return s1;
}
STRCPY()
16
EXAMPLE: USING STRING FUNCTIONS
25
9
-1
big sky country
beautiful brown cows!
int main()
{
char s1[ ] = "beautiful big sky country",
s2[ ] = "how now brown cow";
printf("%dn",strlen (s1));
printf("%dn",strlen (s2+8));
printf("%dn", strcmp(s1,s2));
printf("%sn",s1+10);
strcpy(s1+10,s2+8);
strcat(s1,"s!");
printf("%sn", s1);
return 0;
}
Output

More Related Content

Similar to cprogramming strings.pptx

Similar to cprogramming strings.pptx (20)

introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
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 c++Strings in c++
Strings in c++
 
Strings
StringsStrings
Strings
 
String (Computer programming and utilization)
String (Computer programming and utilization)String (Computer programming and utilization)
String (Computer programming and utilization)
 
strings
stringsstrings
strings
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPT
 
BHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPTBHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPT
 
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
 
[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 CPU GTU
Strings CPU GTUStrings CPU GTU
Strings CPU GTU
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 
Strings
StringsStrings
Strings
 
c programming
c programmingc programming
c programming
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
Strings and pointers
Strings and pointersStrings and pointers
Strings and pointers
 

More from LECO9

C Programming Intro.ppt
C Programming Intro.pptC Programming Intro.ppt
C Programming Intro.pptLECO9
 
Embedded Systems.pptx
Embedded Systems.pptxEmbedded Systems.pptx
Embedded Systems.pptxLECO9
 
Basic Electronics.pptx
Basic Electronics.pptxBasic Electronics.pptx
Basic Electronics.pptxLECO9
 
Intro to Microcontroller.pptx
Intro to Microcontroller.pptxIntro to Microcontroller.pptx
Intro to Microcontroller.pptxLECO9
 
PIC_Intro.pptx
PIC_Intro.pptxPIC_Intro.pptx
PIC_Intro.pptxLECO9
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxDATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxLECO9
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxLECO9
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptxLECO9
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptxLECO9
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptxLECO9
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxLECO9
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxLECO9
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptxDESIGN PATTERN.pptx
DESIGN PATTERN.pptxLECO9
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxLECO9
 
cprogramming Structures.pptx
cprogramming Structures.pptxcprogramming Structures.pptx
cprogramming Structures.pptxLECO9
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptxLECO9
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxLECO9
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptxLECO9
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxLECO9
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptxLECO9
 

More from LECO9 (20)

C Programming Intro.ppt
C Programming Intro.pptC Programming Intro.ppt
C Programming Intro.ppt
 
Embedded Systems.pptx
Embedded Systems.pptxEmbedded Systems.pptx
Embedded Systems.pptx
 
Basic Electronics.pptx
Basic Electronics.pptxBasic Electronics.pptx
Basic Electronics.pptx
 
Intro to Microcontroller.pptx
Intro to Microcontroller.pptxIntro to Microcontroller.pptx
Intro to Microcontroller.pptx
 
PIC_Intro.pptx
PIC_Intro.pptxPIC_Intro.pptx
PIC_Intro.pptx
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxDATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptx
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptxDESIGN PATTERN.pptx
DESIGN PATTERN.pptx
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
 
cprogramming Structures.pptx
cprogramming Structures.pptxcprogramming Structures.pptx
cprogramming Structures.pptx
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptx
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptx
 

Recently uploaded

pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 

Recently uploaded (20)

pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 

cprogramming strings.pptx

  • 2. STRINGS •1-d arrays of type char •By convention, a string in C is terminated by the end- of-string sentinel ‘0’ (null character) •char s[21] - can have variable length string delimited with 0 •Max length of the string that can be stored is 20 as the size must include storage needed for the ‘0’ •String constants : “hello”, “abc” •“abc” is a character array of size 4 2
  • 3. CHARACTER ARRAYS AND STRINGS char C[4] = { 's', 'k', 'u', 'p'}; • C[0] gets the value 'a', C[1] the value 'b', and so on. The last (7th) location receives the null character ‘0’ • Null-terminated (last character is ‘0’) character arrays are also called strings • Strings can be initialized in an alternative way. The last declaration is equivalent to: char C[4] = "skup"; • The trailing null character is missing here. C automatically puts it at the end if you define it like this 3
  • 4. READING STRINGS: %S FORMAT 4 void main() { char name[25]; scanf("%s", name); printf("Name = %s n", name); } %s reads a string into a character array given the array name or start address. It ends the string with ‘0’
  • 5. AN EXAMPLE 5 void main() { #define SIZE 25 int i, count=0; char name[SIZE]; scanf("%s", name); printf("Name = %s n", name); for (i=0; name[i]!='0'; i++) if (name[i] == 'a') count++; printf("Total a's = %dn", count); } Note that character strings read in %s format end with ‘0’
  • 6. DIFFERENCES : ARRAY & POINTERS char *p = “abcde”; The compiler allocates space for p, puts the string constant “abcde” in memory somewhere else, initializes p with the base address of the string constant char s[ ] = “abcde”;  char s[ ] = {‘a’,’b’,’c’,’d’,’e’.’0’}; The compiler allocates 6 bytes of memory for the array s which are initialized with the 6 characters 6 a b c d e 0 a b c d e 0 p S
  • 7. STRING CONSTANT •A string constant is treated as a pointer •Its value is the base address of the string char *p = “abc”; printf (“%s %sn”,p,p+1); /* abc bc is printed */ 7 a b c 0 p
  • 8. LIBRARY FUNCTIONS FOR STRING HANDLING • You can write your own C code to do different operations on strings like finding the length of a string, copying one string to another, appending one string to the end of another etc. • C library provides standard functions for these that you can call, so no need to write your own code • To use them, you must do #include <string.h> At the beginning of your program (after #include <stdio.h>) 8
  • 9. STRING FUNCTIONS • strlen : finds the length of a string • strcat : concatenates one string at the end of another • strcmp : compares two strings lexicographically • strcpy : copies one string to another 9
  • 10. strlen() int strlen(const char *s) • Takes a null-terminated strings (we routinely refer to the char pointer that points to a null- terminated char array as a string) • Returns the length of the string, not counting the null (0) character 10 int strlen (const char *s) { int n; for (n=0; *s!=‘0’; ++s) ++n; return n; } You cannot change contents of s in the function
  • 11. STRCAT() • char *strcat (char *s1, const char *s2); • Takes 2 strings as arguments, concatenates them, and puts the result in s1. Returns s1. Programmer must ensure that s1 points to enough space to hold the result. 11 char *strcat(char *s1, const char *s2) { char *p = s1; while (*p != ‘0’) /* go to end */ ++p; while(*s2 != ‘0’) *p++ = *s2++; /* copy */ *p = ‘0’; return s1; } You cannot change contents of s2 in the function
  • 12. STRCMP() 12 int strcmp (const char *s1, const char *s2); Two strings are passed as arguments. An integer is returned that is less than, equal to, or greater than 0, depending on whether s1 is lexicographically less than, equal to, or greater than s2.
  • 13. STRCMP() 13 int strcmp (const char *s1, const char *s2); Two strings are passed as arguments. An integer is returned that is less than, equal to, or greater than 0, depending on whether s1 is lexicographically less than, equal to, or greater than s2. int strcmp(char *s1, const char *s2) { for (;*s1!=‘0’&&*s2!=‘0’; s1++,s2++) { if (*s1>*s2) return 1; if (*s2>*s1) return -1; } if (*s1 != ‘0’) return 1; if (*s2 != ‘0’) return -1; return 0; }
  • 14. char *strcpy (char *s1, char *s2); The characters is the string s2 are copied into s1 until 0 is moved. Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The pointer s1 is returned. 14 STRCPY()
  • 15. char *strcpy (char *s1, const char *s2); The characters is the string s2 are copied into s1 until ‘0’ is moved. Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The pointer s1 is returned. 15 char * strcpy (char *s1, const char *s2) { char *p = s1; while (*p++ = *s2++) ; return s1; } STRCPY()
  • 16. 16 EXAMPLE: USING STRING FUNCTIONS 25 9 -1 big sky country beautiful brown cows! int main() { char s1[ ] = "beautiful big sky country", s2[ ] = "how now brown cow"; printf("%dn",strlen (s1)); printf("%dn",strlen (s2+8)); printf("%dn", strcmp(s1,s2)); printf("%sn",s1+10); strcpy(s1+10,s2+8); strcat(s1,"s!"); printf("%sn", s1); return 0; } Output