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

9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
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
 

Recently uploaded (20)

9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
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
 

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