SlideShare a Scribd company logo
1 of 24
Unit III -3.2
String-
String Operations-
String Arrays
String
• The C String is stored as an array of characters.
• A String in C programming is a sequence of
characters terminated with a null character
‘0’.
• The difference between a character array and
a C string is the string is terminated with a
unique character ‘0’.
String Declaration
• A string in C is declared as a one-dimensional
array.
– Syntax
• char string_name[size];
– In the above syntax string name is any name given to the string variable and size is used to define the
length of the string, i.e the number of characters strings will store.
Char str[ ];
Memory Representation of String
Char str[ ]=“Sethu”
Index
Varaiable
Address
There is an extra terminating character which is the Null character (‘0’) used to
indicate the termination of a string that differs strings from normal character
arrays.
S e t h u
0 1 2 3 4 5
0
0x 1x 2x 3x 4x 5x
We do not place the null
characters at the end of the
string Constant.
The C Compiler automatically
places the’0’ at the end of the
string when it initializes the
array.
C String Initialization
4 different ways
Method1
1. Assigning a string literal without size
char str[] = “SethuInstituteofTechnology";
String literals can be assigned without size.
Here, the name of the string str acts as a pointer
because it is an array.
A literal is a token that denotes a fixed value, which may be an integer , a floating-point
number, a character, or a string.
C String Initialization
Method-2
• 2. Assigning a string literal with a predefined size
char str[50] = “InformationTechnology";
String literals can be assigned with a predefined size.
One extra space which will be assigned to the null character.
If you want to store a string of size n then youshould always
declare a string with a size equal to or greater than n+1.
C String Initialization
Method-3
• 3. Assigning character by character with size
Assign a string character by character.
char str[15] = {
‘S','e',‘t',‘h',‘u',‘I',‘n',‘s',‘t',‘i',‘t',‘u','t',‘e','0'};
But you should remember to set the end
character as ‘0’ which is a null character.
C String Initialization
Method-4
• Assigning character by character without size
• Assign character by character without size
with the NULL character at the end.
• char str[] =
{‘S','e',‘t',‘h',‘u',‘I',‘n',‘s',‘t',‘i',‘t',‘u','t',‘e','0'};
• The size of the string is determined by the
compiler automatically.
Note
• When a Sequence of characters enclosed in
the double quotation marks is encountered by
the compiler, a null character ‘0’ is appended
at the end of the string by default.
String Function
• String Header file
• #include <string.h>
String Operations
• String Length
• Concatenate Strings
• Copy Strings
• Compare Strings
• String Reverse
String Function
• String Length
To get the length of a string,
strlen()
Ex
char alphabet[]
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet));
Output
26
String Function
• sizeof to get the size of a string/array
• char alphabet[]
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet)); // 26
printf("%d", sizeof(alphabet)); // 27
• sizeof will always return the memory size
(in bytes)
• sizeof also includes the 0 character when
counting
String Function
• Concatenate Strings
– To concatenate (combine) two strings,
strcat()
Ex
char str1[20] = "Hello ";
char str2[] = "World!";
// Concatenate str2 to str1 (result is stored in str1)
strcat(str1, str2);
// Print str1
printf("%s", str1);
Output
• Hello World!
String Function
• Copy Strings
– To copy the value of one string to another,
• strcpy()
• Ex
• char str1[20] = "Hello World!";
char str2[20];
// Copy str1 to str2
strcpy(str2, str1);
// Print str2
printf("%s", str2);
• Output
• Hello World!
Compare Strings
• To compare two strings, you can use the strcmp() function.
• It returns 0 if the two strings are equal, otherwise a value that is not 0:
• Ex
• char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "Hi";
// Compare str1 and str2, and print the result
printf("%dn", strcmp(str1, str2)); // Returns 0 (the strings are equal)
// Compare str1 and str3, and print the result
printf("%dn", strcmp(str1, str3)); // Returns -4 (the strings are not equal)
• Output
• 0
-4
Program-1 Find the Length of the
String using Strlen() Function
// C program to illustrate strings
#include <stdio.h>
#include <string.h>
int main()
{
// declare and initialize string
char str[] = “Priya";
// print string
printf("%sn", str);
int length = 0;
length = strlen(str);
// displaying the length of string
printf("Length of string str is %d", length);
return 0;
}
Program -2 Read String from the user
and print the Same Using %s Access
Specifier
// C program to read string from user
#include<stdio.h>
int main()
{
// declaring string
char str[50];
// reading string
scanf("%s",str);
// print string
printf("%s",str);
return 0;
}
String Array
• In C programming String is a 1-D array of
characters and is defined as an array of
characters.
• But an array of strings in C is a two-
dimensional array of character types.
• Each String is terminated with a null character
(0). It is an application of a 2d array.
Syntax:
char variable_name[r] = {list of string};
• Here,
• var_name is the name of the variable in C.
• r is the maximum number of string values that
can be stored in a string array.
• c is the maximum number of character values
that can be stored in each string array.
// C Program to print Array of strings
#include <stdio.h>
int main()
{
char arr[3][13] = {"C Program",
"Python", "Java Program"};
printf("String array Elements are:n");
for (int i = 0; i < 3; i++)
{
printf("%sn", arr[i]);
}
return 0;
}
Output
String array Elements are:
C Program
Python
Java Program
Memory Representation
C P r o g r a m 0
P y t h o n 0
J a v a p r o g r a m 0
0 1 2 3 4 5 6 7 8 9 10 11 12
Index
Array [0]
Array [1]
Array [2]
the size of the array of strings the space consumption is high
Lab Experiments #1
• Write a C program to print reverse of the given
string
Program
#include <stdio.h>
#include <string.h>
// function definition of the revstr()
void revstr(char *str1)
{
// declare variable
int i, len, temp;
len = strlen(str1); // use strlen() to get the
length of str string
// use for loop to iterate the string
for (i = 0; i < len/2; i++)
{
// temp variable use to temporary hold
the string
temp = str1[i];
str1[i] = str1[len - i - 1];
str1[len - i - 1] = temp;
}
}
int main()
{
char str[50]; // size of char string
printf (" Enter the string: ");
gets(str); // use gets() function to take string
printf (" n Before reversing the string: %s
n", str);
// call revstr() function
revstr(str);
printf (" After reversing the string: %s", str);
}
Output
Enter the string: sethu institute
Before reversing the string: sethu institute
After reversing the string: etutitsni uhtes

More Related Content

Similar to fundamentals of c programming_String.pptx

Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programmingAppili Vamsi Krishna
 
String in c in erode SENGUNTHAR engineering .pptx
String in c in erode SENGUNTHAR engineering .pptxString in c in erode SENGUNTHAR engineering .pptx
String in c in erode SENGUNTHAR engineering .pptxmani123123r
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxrohinitalekar1
 
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.pptxJawadTanvir
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdfssusere19c741
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...AntareepMajumder
 
String in c programming
String in c programmingString in c programming
String in c programmingDevan Thakur
 
Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01Md. Ashikur Rahman
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiSowmya Jyothi
 
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
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentationAliul Kadir Akib
 

Similar to fundamentals of c programming_String.pptx (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
 
String in c in erode SENGUNTHAR engineering .pptx
String in c in erode SENGUNTHAR engineering .pptxString in c in erode SENGUNTHAR engineering .pptx
String in c in erode SENGUNTHAR engineering .pptx
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
Strings
StringsStrings
Strings
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
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
 
Strings
StringsStrings
Strings
 
14 strings
14 strings14 strings
14 strings
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
 
String in c programming
String in c programmingString in c programming
String in c programming
 
Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
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
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Strings and pointers
Strings and pointersStrings and pointers
Strings and pointers
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
C string
C stringC string
C string
 

Recently uploaded

Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
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
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 

Recently uploaded (20)

Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
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
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 

fundamentals of c programming_String.pptx

  • 1. Unit III -3.2 String- String Operations- String Arrays
  • 2. String • The C String is stored as an array of characters. • A String in C programming is a sequence of characters terminated with a null character ‘0’. • The difference between a character array and a C string is the string is terminated with a unique character ‘0’.
  • 3. String Declaration • A string in C is declared as a one-dimensional array. – Syntax • char string_name[size]; – In the above syntax string name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store. Char str[ ];
  • 4. Memory Representation of String Char str[ ]=“Sethu” Index Varaiable Address There is an extra terminating character which is the Null character (‘0’) used to indicate the termination of a string that differs strings from normal character arrays. S e t h u 0 1 2 3 4 5 0 0x 1x 2x 3x 4x 5x We do not place the null characters at the end of the string Constant. The C Compiler automatically places the’0’ at the end of the string when it initializes the array.
  • 5. C String Initialization 4 different ways Method1 1. Assigning a string literal without size char str[] = “SethuInstituteofTechnology"; String literals can be assigned without size. Here, the name of the string str acts as a pointer because it is an array. A literal is a token that denotes a fixed value, which may be an integer , a floating-point number, a character, or a string.
  • 6. C String Initialization Method-2 • 2. Assigning a string literal with a predefined size char str[50] = “InformationTechnology"; String literals can be assigned with a predefined size. One extra space which will be assigned to the null character. If you want to store a string of size n then youshould always declare a string with a size equal to or greater than n+1.
  • 7. C String Initialization Method-3 • 3. Assigning character by character with size Assign a string character by character. char str[15] = { ‘S','e',‘t',‘h',‘u',‘I',‘n',‘s',‘t',‘i',‘t',‘u','t',‘e','0'}; But you should remember to set the end character as ‘0’ which is a null character.
  • 8. C String Initialization Method-4 • Assigning character by character without size • Assign character by character without size with the NULL character at the end. • char str[] = {‘S','e',‘t',‘h',‘u',‘I',‘n',‘s',‘t',‘i',‘t',‘u','t',‘e','0'}; • The size of the string is determined by the compiler automatically.
  • 9. Note • When a Sequence of characters enclosed in the double quotation marks is encountered by the compiler, a null character ‘0’ is appended at the end of the string by default.
  • 10. String Function • String Header file • #include <string.h>
  • 11. String Operations • String Length • Concatenate Strings • Copy Strings • Compare Strings • String Reverse
  • 12. String Function • String Length To get the length of a string, strlen() Ex char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; printf("%d", strlen(alphabet)); Output 26
  • 13. String Function • sizeof to get the size of a string/array • char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; printf("%d", strlen(alphabet)); // 26 printf("%d", sizeof(alphabet)); // 27 • sizeof will always return the memory size (in bytes) • sizeof also includes the 0 character when counting
  • 14. String Function • Concatenate Strings – To concatenate (combine) two strings, strcat() Ex char str1[20] = "Hello "; char str2[] = "World!"; // Concatenate str2 to str1 (result is stored in str1) strcat(str1, str2); // Print str1 printf("%s", str1); Output • Hello World!
  • 15. String Function • Copy Strings – To copy the value of one string to another, • strcpy() • Ex • char str1[20] = "Hello World!"; char str2[20]; // Copy str1 to str2 strcpy(str2, str1); // Print str2 printf("%s", str2); • Output • Hello World!
  • 16. Compare Strings • To compare two strings, you can use the strcmp() function. • It returns 0 if the two strings are equal, otherwise a value that is not 0: • Ex • char str1[] = "Hello"; char str2[] = "Hello"; char str3[] = "Hi"; // Compare str1 and str2, and print the result printf("%dn", strcmp(str1, str2)); // Returns 0 (the strings are equal) // Compare str1 and str3, and print the result printf("%dn", strcmp(str1, str3)); // Returns -4 (the strings are not equal) • Output • 0 -4
  • 17. Program-1 Find the Length of the String using Strlen() Function // C program to illustrate strings #include <stdio.h> #include <string.h> int main() { // declare and initialize string char str[] = “Priya"; // print string printf("%sn", str); int length = 0; length = strlen(str); // displaying the length of string printf("Length of string str is %d", length); return 0; }
  • 18. Program -2 Read String from the user and print the Same Using %s Access Specifier // C program to read string from user #include<stdio.h> int main() { // declaring string char str[50]; // reading string scanf("%s",str); // print string printf("%s",str); return 0; }
  • 19. String Array • In C programming String is a 1-D array of characters and is defined as an array of characters. • But an array of strings in C is a two- dimensional array of character types. • Each String is terminated with a null character (0). It is an application of a 2d array.
  • 20. Syntax: char variable_name[r] = {list of string}; • Here, • var_name is the name of the variable in C. • r is the maximum number of string values that can be stored in a string array. • c is the maximum number of character values that can be stored in each string array.
  • 21. // C Program to print Array of strings #include <stdio.h> int main() { char arr[3][13] = {"C Program", "Python", "Java Program"}; printf("String array Elements are:n"); for (int i = 0; i < 3; i++) { printf("%sn", arr[i]); } return 0; } Output String array Elements are: C Program Python Java Program
  • 22. Memory Representation C P r o g r a m 0 P y t h o n 0 J a v a p r o g r a m 0 0 1 2 3 4 5 6 7 8 9 10 11 12 Index Array [0] Array [1] Array [2] the size of the array of strings the space consumption is high
  • 23. Lab Experiments #1 • Write a C program to print reverse of the given string
  • 24. Program #include <stdio.h> #include <string.h> // function definition of the revstr() void revstr(char *str1) { // declare variable int i, len, temp; len = strlen(str1); // use strlen() to get the length of str string // use for loop to iterate the string for (i = 0; i < len/2; i++) { // temp variable use to temporary hold the string temp = str1[i]; str1[i] = str1[len - i - 1]; str1[len - i - 1] = temp; } } int main() { char str[50]; // size of char string printf (" Enter the string: "); gets(str); // use gets() function to take string printf (" n Before reversing the string: %s n", str); // call revstr() function revstr(str); printf (" After reversing the string: %s", str); } Output Enter the string: sethu institute Before reversing the string: sethu institute After reversing the string: etutitsni uhtes