SlideShare a Scribd company logo
1 of 13
Strings in c programming
• NAME:NARRA BHARGAVI
• PIN:22AJ1A0562
• CSE-A
Strings
2
• A string is nothing but the collection of the individual array elements or
characters.
• String is enclosed within Double quotes.
• “programming" is a example of String.
• Each Character Occupy 1 byte of Memory.
• Size of “programming“ = 11 bytes
• String is always Terminated with NULL Character (‘0′).
char word[20] = “‘p’ , ‘r’ , ‘o’ , ‘g’ , ‘r’ , ‘a’ , ‘m’ , ‘m’ , ‘I’ , ‘n’ , ‘g’ ,
‘0’”
NULL Character
3
• NULL Character is also known as string terminating character.
• It is represented by “0”.
• NULL Character is having ASCII value 0
• NULL terminates a string, but isn’t part of it
• important for strlen() – length doesn’t include the NULL
Declaration of a string
4
• Since we cannot declare string using String Data Type, instead of
which we use array of type “char” to create String.
• Syntax :
• char String_Variable_name [ SIZE ] ;
• Examples :
• char city[30];
• char name[20];
• char message[50];
Rules for declaring a string
5
• String / Character Array Variable name should be legal C Identifier.
• String Variable must have Size specified.
• char city[];
• Above Statement will cause compile time error.
• Do not use String as data type because String data type is included in
later languages such as C++ / Java. C does not support String data type
• When you are using string for other purpose than accepting and
printing data then you must include following header file in your code–
#include<string.h>
Initializing String (Character Array)
6
• Process of Assigning some legal default data to String is Called
Initialization of String.
• A string can be initialized in different ways. We will explain this
with the help of an example.
• Below is an example to declare a string with name as str and
initialize it with “GeeksforGeeks”.
1. char str[] = "GeeksforGeeks";
2. char str[50] = "GeeksforGeeks";
3. char str[] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','0'};
4. char str[14] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','0'};
STRING EXAMPLE
#include <stdio.h>
int main () {
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'};
printf("Greeting message: %sn", greeting );
return 0; }
When the above code is compiled and executed, it produces
the following result −
Greeting message: Hello
Functions of string.h
Function Purpose Example Output
Strcpy(); Makes a copy of a string strcpy(s1, “Hi”); Copies “Hi” to ‘s1’
variable
Strcat(); Appends a string to the
end of another string
strcat(“Work”, “Hard”); Prints “WorkHard”
Strcmp(); Compare two strings
alphabetically
strcmp(“hi”, “bye”); Returns -1.
Strlen(); Returns the number of
characters in a string
strlen(“Hi”); Returns 2.
Strrev(); reverses a given string Strrev(“Hello”); olleH
Strlwr(); Converts string to
lowercase
Strlwr(“HELLO”); hello
Strupr(); Converts string to
uppercase
Strupr(“hello”); HELLO
String Copy (strcpy)
• strcpy( ) function copies contents of one string into another string.
• Syntax : strcpy (destination_string , source_string );
• Example:-strcpy ( str1, str2) – It copies contents of str2 into str1.
strcpy ( str2, str1) – It copies contents of str1 into str2.
• If destination string length is less than source string, entire source string
value won’t be copied into destination string. For example, consider
destination string length is 20 and source string length is 30. Then, only 20
characters from source string will be copied into destination string and
remaining 10 characters won’t be copied and will be truncated.
String Concat (strcat)
10
• strncat( ) function in C language concatenates (appends) portion of one
string at the end of another string.
• Syntax : strncat ( destination_string , source_string, size);
• Example:-strncat ( str2, str1, 3 ); – First 3 characters of str1 is
concatenated at the end of str2.
• As you know, each string in C is ended up with null character (‘0’).
• In strncat( ) operation, null character of destination string is overwritten
by source string’s first character and null character is added at the end of
new destination string which is created after strncat( ) operation.
String Compare (strcmp)
11
 strcmp( ) function in C compares two given strings and returns zero if they
are same.
 If length of string1 < string2, it returns < 0 value that is -1.
 If length of string1 > string2, it returns > 0 value that is 1
 If length of string1 = string2 it returns 0.
Syntax : strcmp (str1 , str2 );strcmp( ) function is case sensitive. i.e, “A” and
“a” are treated as different characters.
String Length (strlen)
12
• strlen( ) function in C gives the length of the given
string.
• Syntax : strlen(str);
• strlen( ) function counts the number of characters in a given
string and returns the integer value.
• It stops counting the character when null character is found. Because,
null character indicates the end of the string in C.
THANK YOU
13

More Related Content

Similar to C strings guide - essential functions and concepts

Similar to C strings guide - essential functions and concepts (20)

strings
stringsstrings
strings
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Strings CPU GTU
Strings CPU GTUStrings CPU GTU
Strings CPU GTU
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
Strings
StringsStrings
Strings
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
String notes
String notesString notes
String notes
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
14 strings
14 strings14 strings
14 strings
 
Strings
StringsStrings
Strings
 
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
 
c programming
c programmingc programming
c programming
 
Team 1
Team 1Team 1
Team 1
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
[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++
 
string in C
string in Cstring in C
string in C
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation x
 

Recently uploaded

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
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
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
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
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 

Recently uploaded (20)

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 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
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
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
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 

C strings guide - essential functions and concepts

  • 1. Strings in c programming • NAME:NARRA BHARGAVI • PIN:22AJ1A0562 • CSE-A
  • 2. Strings 2 • A string is nothing but the collection of the individual array elements or characters. • String is enclosed within Double quotes. • “programming" is a example of String. • Each Character Occupy 1 byte of Memory. • Size of “programming“ = 11 bytes • String is always Terminated with NULL Character (‘0′). char word[20] = “‘p’ , ‘r’ , ‘o’ , ‘g’ , ‘r’ , ‘a’ , ‘m’ , ‘m’ , ‘I’ , ‘n’ , ‘g’ , ‘0’”
  • 3. NULL Character 3 • NULL Character is also known as string terminating character. • It is represented by “0”. • NULL Character is having ASCII value 0 • NULL terminates a string, but isn’t part of it • important for strlen() – length doesn’t include the NULL
  • 4. Declaration of a string 4 • Since we cannot declare string using String Data Type, instead of which we use array of type “char” to create String. • Syntax : • char String_Variable_name [ SIZE ] ; • Examples : • char city[30]; • char name[20]; • char message[50];
  • 5. Rules for declaring a string 5 • String / Character Array Variable name should be legal C Identifier. • String Variable must have Size specified. • char city[]; • Above Statement will cause compile time error. • Do not use String as data type because String data type is included in later languages such as C++ / Java. C does not support String data type • When you are using string for other purpose than accepting and printing data then you must include following header file in your code– #include<string.h>
  • 6. Initializing String (Character Array) 6 • Process of Assigning some legal default data to String is Called Initialization of String. • A string can be initialized in different ways. We will explain this with the help of an example. • Below is an example to declare a string with name as str and initialize it with “GeeksforGeeks”. 1. char str[] = "GeeksforGeeks"; 2. char str[50] = "GeeksforGeeks"; 3. char str[] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','0'}; 4. char str[14] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','0'};
  • 7. STRING EXAMPLE #include <stdio.h> int main () { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'}; printf("Greeting message: %sn", greeting ); return 0; } When the above code is compiled and executed, it produces the following result − Greeting message: Hello
  • 8. Functions of string.h Function Purpose Example Output Strcpy(); Makes a copy of a string strcpy(s1, “Hi”); Copies “Hi” to ‘s1’ variable Strcat(); Appends a string to the end of another string strcat(“Work”, “Hard”); Prints “WorkHard” Strcmp(); Compare two strings alphabetically strcmp(“hi”, “bye”); Returns -1. Strlen(); Returns the number of characters in a string strlen(“Hi”); Returns 2. Strrev(); reverses a given string Strrev(“Hello”); olleH Strlwr(); Converts string to lowercase Strlwr(“HELLO”); hello Strupr(); Converts string to uppercase Strupr(“hello”); HELLO
  • 9. String Copy (strcpy) • strcpy( ) function copies contents of one string into another string. • Syntax : strcpy (destination_string , source_string ); • Example:-strcpy ( str1, str2) – It copies contents of str2 into str1. strcpy ( str2, str1) – It copies contents of str1 into str2. • If destination string length is less than source string, entire source string value won’t be copied into destination string. For example, consider destination string length is 20 and source string length is 30. Then, only 20 characters from source string will be copied into destination string and remaining 10 characters won’t be copied and will be truncated.
  • 10. String Concat (strcat) 10 • strncat( ) function in C language concatenates (appends) portion of one string at the end of another string. • Syntax : strncat ( destination_string , source_string, size); • Example:-strncat ( str2, str1, 3 ); – First 3 characters of str1 is concatenated at the end of str2. • As you know, each string in C is ended up with null character (‘0’). • In strncat( ) operation, null character of destination string is overwritten by source string’s first character and null character is added at the end of new destination string which is created after strncat( ) operation.
  • 11. String Compare (strcmp) 11  strcmp( ) function in C compares two given strings and returns zero if they are same.  If length of string1 < string2, it returns < 0 value that is -1.  If length of string1 > string2, it returns > 0 value that is 1  If length of string1 = string2 it returns 0. Syntax : strcmp (str1 , str2 );strcmp( ) function is case sensitive. i.e, “A” and “a” are treated as different characters.
  • 12. String Length (strlen) 12 • strlen( ) function in C gives the length of the given string. • Syntax : strlen(str); • strlen( ) function counts the number of characters in a given string and returns the integer value. • It stops counting the character when null character is found. Because, null character indicates the end of the string in C.