SlideShare a Scribd company logo
1 of 14
Strings
Contents
• 3.1 Declaration and initialization, string
input/output, format specifiers
• 3.2 Standard library functions
• 3.3 Strings and pointers
• 3.4 Array of strings
• 3.5 Command Line Arguments
What is String?
• A string is a sequence of characters
terminated with null character ‘0’.
• In C string is defined using array of characters.
• Example char name [] = “COMPUTER”;
C O M P U T E R ‘0’
String declaration
• Syntax : char string_name [no of characters];
example char s1[10];
char faculty[30];
String initialization
• Syntax : char string_name [] = “”;
example char s1[] = “c language”;
char faculty[] = “Arts”;
String input output
Input function : scanf / gets
Example
scanf(“%s”, s1);
gets(s1);
Output function: printf/puts
printf(“n s1= %s”, s1);
puts(s1);
Format Specifier
%s
Standard Library Functions
<string.h>
• strlen : returns length of the string
int strlen (const char *)
• strcpy : copy one string into another
void strcpy (char *, const char *)
strcpy(s1,s2);
• strcat : concantenates (combines) two strings
void strcat (char *, const char *)
strcat(s1,s2);
• strcmp : compare two strings
int strcmp (const char *, const char *)
res = strcmp (s1,s2);
// convert string in Upper case
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char s[] = "computer";
printf("n string in upper case n");
for(int i; i< strlen(s);i++)
{
printf("%c",toupper(s[i]));
}
return 0;
}
// calculate length of the string without using std
function strlen
#include <stdio.h>
#include <string.h>
int main()
{
char s[30];
int i=0;
printf("n enter any string : ");
scanf("%s", s);
while(s[i] != '0')
i++;
printf("n length of string %s is %d",s,i);
return 0;
}
Pointers and Strings
Pointer to character array
char name[30];
char *p;
p = name;
void
Use pointers to store strings and
display strings
#include <stdio.h>
#include <string.h>
int main()
{
char *name[4] = { "vinita", "vinay", "seema", "arya“ };
int i,j;
for(i=0; i<4;i++)
{ j=0;
while(*(name[i] +j) != '0')
{
printf(“%c",*(name[i] +j));
j++;
}
printf("n");
}
return 0;
}
Array of Strings
A string is a 1-D array of characters, so an array of strings is a 2-D array of characters.
• char names[3][6] = {
{‘A', ‘r', ‘y', ‘a’},
{'t', 'o', 'm},
{'j', 'e', 'r', 'r', 'y'}
};
• char names[3][6] = {
“Arya”,
“tom”,
“jerry”
};
0 1 2 3 4 5
0 ‘A’ ‘r’ ‘y’ ‘a’ ‘0’
1 ‘t’ ‘o’ ‘m’ ‘0’
2 ‘j’ ‘e’ ‘r’ ‘r’ ‘y’ ‘0’
//Array of Strings
// Display data from array of strings
#include <stdio.h>
int main()
{
char names[3][20] = { "Rutuja",
"Mansi",
"Sushmita"
};
for(int i=0;i<3;i++)
printf("%sn",names +i);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char names[4][20];
char s[20];
int i;
printf("n enter any four names of students :");
for(i=0;i<4;i++)
{
scanf("%s",names +i);
}
printf("n The student's names aren");
for(i=0;i<4;i++)
printf("%sn",names +i);
printf("n enter the name to search in array :- ");
scanf("%s",s);
for(i=0;i<4;i++)
{
if(strcmp(s,names+i) == 0)
{
printf("n successful search...");
}
}
if(i >=4)
printf("n Unsuccessful search");
return 0;
}
Strings in C language

More Related Content

What's hot (20)

String C Programming
String C ProgrammingString C Programming
String C Programming
 
Array and string
Array and stringArray and string
Array and string
 
Strings in c
Strings in cStrings in c
Strings in c
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
Data types in C
Data types in CData types in C
Data types in C
 
Function in C program
Function in C programFunction in C program
Function in C program
 
String functions in C
String functions in CString functions in C
String functions in C
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Array in C
Array in CArray in C
Array in C
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Array in c
Array in cArray in c
Array in c
 
2D Array
2D Array 2D Array
2D Array
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Strings
StringsStrings
Strings
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 

Similar to Strings in C language

Similar to Strings in C language (20)

introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
c ppt.pdf
c ppt.pdfc ppt.pdf
c ppt.pdf
 
Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_
 
SPL 13 | Character Array(String) in C
SPL 13 | Character Array(String) in CSPL 13 | Character Array(String) in C
SPL 13 | Character Array(String) in C
 
String
StringString
String
 
Unitii string
Unitii stringUnitii string
Unitii string
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
Lecture 2. mte 407
Lecture 2. mte 407Lecture 2. mte 407
Lecture 2. mte 407
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Week6_P_String.pptx
Week6_P_String.pptxWeek6_P_String.pptx
Week6_P_String.pptx
 
14 strings
14 strings14 strings
14 strings
 
Team 1
Team 1Team 1
Team 1
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
Arrays
ArraysArrays
Arrays
 
week-7x
week-7xweek-7x
week-7x
 
String notes
String notesString notes
String notes
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 

Recently uploaded

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 

Strings in C language

  • 2. Contents • 3.1 Declaration and initialization, string input/output, format specifiers • 3.2 Standard library functions • 3.3 Strings and pointers • 3.4 Array of strings • 3.5 Command Line Arguments
  • 3. What is String? • A string is a sequence of characters terminated with null character ‘0’. • In C string is defined using array of characters. • Example char name [] = “COMPUTER”; C O M P U T E R ‘0’
  • 4. String declaration • Syntax : char string_name [no of characters]; example char s1[10]; char faculty[30]; String initialization • Syntax : char string_name [] = “”; example char s1[] = “c language”; char faculty[] = “Arts”;
  • 5. String input output Input function : scanf / gets Example scanf(“%s”, s1); gets(s1); Output function: printf/puts printf(“n s1= %s”, s1); puts(s1); Format Specifier %s
  • 6. Standard Library Functions <string.h> • strlen : returns length of the string int strlen (const char *) • strcpy : copy one string into another void strcpy (char *, const char *) strcpy(s1,s2); • strcat : concantenates (combines) two strings void strcat (char *, const char *) strcat(s1,s2); • strcmp : compare two strings int strcmp (const char *, const char *) res = strcmp (s1,s2);
  • 7. // convert string in Upper case #include <stdio.h> #include <string.h> #include <ctype.h> int main() { char s[] = "computer"; printf("n string in upper case n"); for(int i; i< strlen(s);i++) { printf("%c",toupper(s[i])); } return 0; }
  • 8. // calculate length of the string without using std function strlen #include <stdio.h> #include <string.h> int main() { char s[30]; int i=0; printf("n enter any string : "); scanf("%s", s); while(s[i] != '0') i++; printf("n length of string %s is %d",s,i); return 0; }
  • 9. Pointers and Strings Pointer to character array char name[30]; char *p; p = name; void
  • 10. Use pointers to store strings and display strings #include <stdio.h> #include <string.h> int main() { char *name[4] = { "vinita", "vinay", "seema", "arya“ }; int i,j; for(i=0; i<4;i++) { j=0; while(*(name[i] +j) != '0') { printf(“%c",*(name[i] +j)); j++; } printf("n"); } return 0; }
  • 11. Array of Strings A string is a 1-D array of characters, so an array of strings is a 2-D array of characters. • char names[3][6] = { {‘A', ‘r', ‘y', ‘a’}, {'t', 'o', 'm}, {'j', 'e', 'r', 'r', 'y'} }; • char names[3][6] = { “Arya”, “tom”, “jerry” }; 0 1 2 3 4 5 0 ‘A’ ‘r’ ‘y’ ‘a’ ‘0’ 1 ‘t’ ‘o’ ‘m’ ‘0’ 2 ‘j’ ‘e’ ‘r’ ‘r’ ‘y’ ‘0’
  • 12. //Array of Strings // Display data from array of strings #include <stdio.h> int main() { char names[3][20] = { "Rutuja", "Mansi", "Sushmita" }; for(int i=0;i<3;i++) printf("%sn",names +i); return 0; }
  • 13. #include <stdio.h> #include <string.h> int main() { char names[4][20]; char s[20]; int i; printf("n enter any four names of students :"); for(i=0;i<4;i++) { scanf("%s",names +i); } printf("n The student's names aren"); for(i=0;i<4;i++) printf("%sn",names +i); printf("n enter the name to search in array :- "); scanf("%s",s); for(i=0;i<4;i++) { if(strcmp(s,names+i) == 0) { printf("n successful search..."); } } if(i >=4) printf("n Unsuccessful search"); return 0; }