SlideShare a Scribd company logo
C Programming Strings
In C programming, a string is a sequence of characters terminated with a
null character 0. For example:
char c[] = "c string";
When the compiler encounters a sequence of characters enclosed in the
double quotation marks, it appends a null character 0 at the end by
default.
Memory Diagram
How to declare a string?
Here's how you can declare strings:
char s[5];
String Declaration in C
How to initialize strings?
You can initialize strings in a number of ways.
char c[] = "abcd";
char c[50] = "abcd";
char c[] = {'a', 'b', 'c', 'd', '0'};
char c[5] = {'a', 'b', 'c', 'd', '0'};
String Initialization in C
Let's take another example:
char c[5] = "abcde";
Here, we are trying to assign 6 characters (the last character is '0') to
a char array having 5 characters. This is bad and you should never do this.
Assigning Values to Strings
Arrays and strings are second-class citizens in C; they do not support the
assignment operator once it is declared. For example,
char c[100];
c = "C programming"; // Error! array type is not assignable.
Note: Use the strcpy() function to copy the string instead.
Read String from the user
You can use the scanf() function to read a string.
The scanf() function reads the sequence of characters until it
encounters whitespace (space, newline, tab, etc.).
Example 1: scanf() to read a string
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Output
Enter name: Dennis Ritchie
Your name is Dennis.
Even though Dennis Ritchie was entered in the above program,
only "Dennis" was stored in the name string. It's because there was a space
after Dennis.
Also notice that we have used the code name instead of &name with scanf().
scanf("%s", name);
This is because name is a char array, and we know that array names decay
to pointers in C.
Thus, the name in scanf() already points to the address of the first element in
the string, which is why we don't need to use &.
How to read a line of text?
You can use the fgets() function to read a line of string. And, you can
use puts() to display the string.
Example 2: fgets() and puts()
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin); // read string
printf("Name: ");
puts(name); // display string
return 0;
}
Output
Enter name: Tom Hanks
Name: Tom Hanks
Here, we have used fgets() function to read a string from the user.
Standard C Library – String.h Functions
The C language comes bundled with <string.h> which contains some useful
string-handling functions. Some of them are as follows:
Function Name Description
strlen(string_name) Returns the length of string name.
strcpy(s1, s2) Copies the contents of string s2 to string s1.
strcmp(str1, str2)
Compares the first string with the second string. If strings are the
same it returns 0.
strcat(s1, s2)
Concat s1 string with s2 string and the result is stored in the first
string.
strlwr() Converts string to lowercase.
strupr() Converts string to uppercase.
Function Name Description
strstr(s1, s2) Find the first occurrence of s2 in s1.
Example of String By Using Above-mentioned Functions:
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = “John”;
char str2[12] = “Potter”;
char str3[12];
int len ;
/* copy str1 into str3 */
strcpy(str3, str1);
printf(“strcpy( str3, str1) : %sn”, str3 );
/* concatenates str1 and str2 */
strcat( str1, str2);
printf(“strcat( str1, str2): %sn”, str1 );
/* total length of str1 after concatenation */
len = strlen(str1);
printf(“strlen(str1) : %dn”, len );
return 0;
}
Output:
strcpy( str3, str1) : John
strcat( str1, str2): JohnPotter
strlen(str1) : 10
Traverse String
There are two ways to traverse a string
 By using the length of string
 By using the null character.
 Example of using the length of string
#include<stdio.h>
void main ()
{
char s[11] = “byjuslearning”;
int i = 0;
int count = 0;
while(i<11)
{
if(s[i]==’a’ || s[i] == ‘e’ || s[i] == ‘i’ || s[i] == ‘u’ || s[i] == ‘o’)
{
count ++;
}
i++;
}
printf(“The number of vowels %d”,count);
}
Output: The number of vowels : 3
 Second Example: Using Null Character
#include<stdio.h>
void main ()
{
char s[11] = “byjuslearning”;
int i = 0;
int count = 0;
while(s[i] != NULL)
{
if(s[i]==’a’ || s[i] == ‘e’ || s[i] == ‘i’ || s[i] == ‘u’ || s[i] == ‘o’)
{
count ++;
}
i++;
}
printf(“The number of vowels %d”,count);
}
Output: The number of vowels: 3
Ad

More Related Content

Similar to C Programming Strings.docx (20)

C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
Rumman Ansari
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
mikeymanjiro2090
 
Unit 2
Unit 2Unit 2
Unit 2
TPLatchoumi
 
Strings
StringsStrings
Strings
Mitali Chugh
 
string function with example...................
string function with example...................string function with example...................
string function with example...................
NishantsrivastavaV
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
JamesChristianGadian
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
yndaravind
 
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++
Azeemaj101
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
Vikram Nandini
 
Module-2_Strings concepts in c programming
Module-2_Strings concepts in c programmingModule-2_Strings concepts in c programming
Module-2_Strings concepts in c programming
CHAITRAB29
 
Arrays
ArraysArrays
Arrays
AnaraAlam
 
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledgeUNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
2024163103shubham
 
c programming
c programmingc programming
c programming
Arun Umrao
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
karmuhtam
 
Lecture 2. mte 407
Lecture 2. mte 407Lecture 2. mte 407
Lecture 2. mte 407
rumanatasnim415
 
STRING FUNCTION - Programming in C.pptx
STRING FUNCTION -  Programming in C.pptxSTRING FUNCTION -  Programming in C.pptx
STRING FUNCTION - Programming in C.pptx
Indhu Periys
 
pps unit 3.pptx
pps unit 3.pptxpps unit 3.pptx
pps unit 3.pptx
mathesh0303
 
Array &strings
Array &stringsArray &strings
Array &strings
UMA PARAMESWARI
 
String_C.pptx
String_C.pptxString_C.pptx
String_C.pptx
HARSHITHA EBBALI
 
ARRAY's in C Programming Language PPTX.
ARRAY's in C  Programming Language PPTX.ARRAY's in C  Programming Language PPTX.
ARRAY's in C Programming Language PPTX.
MSridhar18
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
Rumman Ansari
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
mikeymanjiro2090
 
string function with example...................
string function with example...................string function with example...................
string function with example...................
NishantsrivastavaV
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
JamesChristianGadian
 
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++
Azeemaj101
 
Module-2_Strings concepts in c programming
Module-2_Strings concepts in c programmingModule-2_Strings concepts in c programming
Module-2_Strings concepts in c programming
CHAITRAB29
 
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledgeUNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
2024163103shubham
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
karmuhtam
 
STRING FUNCTION - Programming in C.pptx
STRING FUNCTION -  Programming in C.pptxSTRING FUNCTION -  Programming in C.pptx
STRING FUNCTION - Programming in C.pptx
Indhu Periys
 
ARRAY's in C Programming Language PPTX.
ARRAY's in C  Programming Language PPTX.ARRAY's in C  Programming Language PPTX.
ARRAY's in C Programming Language PPTX.
MSridhar18
 

More from 8759000398 (9)

COMPUTER WINDOWS AND ITS LAB ASSESSMENT.docx
COMPUTER WINDOWS AND ITS LAB ASSESSMENT.docxCOMPUTER WINDOWS AND ITS LAB ASSESSMENT.docx
COMPUTER WINDOWS AND ITS LAB ASSESSMENT.docx
8759000398
 
Testing in Software Engineering.docx
Testing in Software Engineering.docxTesting in Software Engineering.docx
Testing in Software Engineering.docx
8759000398
 
SAD_UnitII.docx
SAD_UnitII.docxSAD_UnitII.docx
SAD_UnitII.docx
8759000398
 
JavaScript Date Objects.docx
JavaScript Date Objects.docxJavaScript Date Objects.docx
JavaScript Date Objects.docx
8759000398
 
Nursing Infromatics.pptx
Nursing Infromatics.pptxNursing Infromatics.pptx
Nursing Infromatics.pptx
8759000398
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
8759000398
 
newone2.pdf
newone2.pdfnewone2.pdf
newone2.pdf
8759000398
 
College app for android device
College app for android deviceCollege app for android device
College app for android device
8759000398
 
Android
AndroidAndroid
Android
8759000398
 
COMPUTER WINDOWS AND ITS LAB ASSESSMENT.docx
COMPUTER WINDOWS AND ITS LAB ASSESSMENT.docxCOMPUTER WINDOWS AND ITS LAB ASSESSMENT.docx
COMPUTER WINDOWS AND ITS LAB ASSESSMENT.docx
8759000398
 
Testing in Software Engineering.docx
Testing in Software Engineering.docxTesting in Software Engineering.docx
Testing in Software Engineering.docx
8759000398
 
SAD_UnitII.docx
SAD_UnitII.docxSAD_UnitII.docx
SAD_UnitII.docx
8759000398
 
JavaScript Date Objects.docx
JavaScript Date Objects.docxJavaScript Date Objects.docx
JavaScript Date Objects.docx
8759000398
 
Nursing Infromatics.pptx
Nursing Infromatics.pptxNursing Infromatics.pptx
Nursing Infromatics.pptx
8759000398
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
8759000398
 
College app for android device
College app for android deviceCollege app for android device
College app for android device
8759000398
 
Ad

Recently uploaded (20)

History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Ad

C Programming Strings.docx

  • 1. C Programming Strings In C programming, a string is a sequence of characters terminated with a null character 0. For example: char c[] = "c string"; When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character 0 at the end by default. Memory Diagram How to declare a string? Here's how you can declare strings: char s[5]; String Declaration in C How to initialize strings? You can initialize strings in a number of ways. char c[] = "abcd"; char c[50] = "abcd"; char c[] = {'a', 'b', 'c', 'd', '0'};
  • 2. char c[5] = {'a', 'b', 'c', 'd', '0'}; String Initialization in C Let's take another example: char c[5] = "abcde"; Here, we are trying to assign 6 characters (the last character is '0') to a char array having 5 characters. This is bad and you should never do this. Assigning Values to Strings Arrays and strings are second-class citizens in C; they do not support the assignment operator once it is declared. For example, char c[100]; c = "C programming"; // Error! array type is not assignable. Note: Use the strcpy() function to copy the string instead. Read String from the user You can use the scanf() function to read a string. The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.). Example 1: scanf() to read a string #include <stdio.h> int main()
  • 3. { char name[20]; printf("Enter name: "); scanf("%s", name); printf("Your name is %s.", name); return 0; } Output Enter name: Dennis Ritchie Your name is Dennis. Even though Dennis Ritchie was entered in the above program, only "Dennis" was stored in the name string. It's because there was a space after Dennis. Also notice that we have used the code name instead of &name with scanf(). scanf("%s", name); This is because name is a char array, and we know that array names decay to pointers in C. Thus, the name in scanf() already points to the address of the first element in the string, which is why we don't need to use &. How to read a line of text? You can use the fgets() function to read a line of string. And, you can use puts() to display the string. Example 2: fgets() and puts() #include <stdio.h> int main() { char name[30];
  • 4. printf("Enter name: "); fgets(name, sizeof(name), stdin); // read string printf("Name: "); puts(name); // display string return 0; } Output Enter name: Tom Hanks Name: Tom Hanks Here, we have used fgets() function to read a string from the user. Standard C Library – String.h Functions The C language comes bundled with <string.h> which contains some useful string-handling functions. Some of them are as follows: Function Name Description strlen(string_name) Returns the length of string name. strcpy(s1, s2) Copies the contents of string s2 to string s1. strcmp(str1, str2) Compares the first string with the second string. If strings are the same it returns 0. strcat(s1, s2) Concat s1 string with s2 string and the result is stored in the first string. strlwr() Converts string to lowercase. strupr() Converts string to uppercase.
  • 5. Function Name Description strstr(s1, s2) Find the first occurrence of s2 in s1. Example of String By Using Above-mentioned Functions: #include <stdio.h> #include <string.h> int main () { char str1[12] = “John”; char str2[12] = “Potter”; char str3[12]; int len ; /* copy str1 into str3 */ strcpy(str3, str1); printf(“strcpy( str3, str1) : %sn”, str3 ); /* concatenates str1 and str2 */ strcat( str1, str2); printf(“strcat( str1, str2): %sn”, str1 ); /* total length of str1 after concatenation */ len = strlen(str1); printf(“strlen(str1) : %dn”, len ); return 0; }
  • 6. Output: strcpy( str3, str1) : John strcat( str1, str2): JohnPotter strlen(str1) : 10 Traverse String There are two ways to traverse a string  By using the length of string  By using the null character.  Example of using the length of string #include<stdio.h> void main () { char s[11] = “byjuslearning”; int i = 0; int count = 0; while(i<11) { if(s[i]==’a’ || s[i] == ‘e’ || s[i] == ‘i’ || s[i] == ‘u’ || s[i] == ‘o’) { count ++; } i++; } printf(“The number of vowels %d”,count);
  • 7. } Output: The number of vowels : 3  Second Example: Using Null Character #include<stdio.h> void main () { char s[11] = “byjuslearning”; int i = 0; int count = 0; while(s[i] != NULL) { if(s[i]==’a’ || s[i] == ‘e’ || s[i] == ‘i’ || s[i] == ‘u’ || s[i] == ‘o’) { count ++; } i++; } printf(“The number of vowels %d”,count); } Output: The number of vowels: 3
  翻译: