SlideShare a Scribd company logo
1 of 10
STRINGS IN C
Session 11
OBJECTIVES
• What is String
• Declaration of String
• Initialization of strings
• Reading Strings from user
• Reading a line of text
• Passing Strings to Functions
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
2
STRING
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
3
• In C programming, array of
characters is called a string.
• A string is terminated by a null
character /0. For example:
• "c string tutorial"
DECLARATION
• Before we actually work with strings, we need to declare them first.
• Strings are declared in a similar manner as arrays. Only difference is that, strings are
of char type.
• char s[5];
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
4
INITIALIZATION OF STRINGS
• In C, string can be initialized in a number of different ways.
• For convenience and ease, both initialization and declaration are done in the same
step.
• char c[] = "abcd";
• OR,
• char c[50] = "abcd";
• String can also be initialized using pointers as:
• char *c = "abcd";
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
5
READING STRINGS FROM USER
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
6
• You can use the scanf() function to read a string like any other data types.
• However, the scanf() function only takes the first entered word.
• The function terminates when it encounters a white space (or just space).
• char c[20];
• scanf("%s", c);
•
EXAMPLE #1
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
7
EXAMPLE #2
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
8
• #include <stdio.h>
• int main()
• {
• char name[30], ch;
• int i = 0;
• printf("Enter name: ");
• while(ch != 'n') // terminates if user hit enter
• {
• ch = getchar();
• name[i] = ch;
• i++;
• }
• name[i] = '0'; // inserting null character at end
• printf("Name: %s", name);
• return 0;
• }
EXAMPLE #3
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
9
• #include <stdio.h>
• int main()
• {
• char name[30];
• printf("Enter name: ");
• gets(name); //Function to read string from user.
• printf("Name: ");
• puts(name); //Function to display string.
• return 0;
• }
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
10

More Related Content

What's hot

Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programmingChitrank Dixit
 
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programmingNico Ludwig
 
Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data typesPratik Devmurari
 
Arraysincv109102017 180831194256
Arraysincv109102017 180831194256Arraysincv109102017 180831194256
Arraysincv109102017 180831194256ABHAY9616302301
 
primitive data types
 primitive data types primitive data types
primitive data typesJadavsejal
 
45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambala45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambalajatin batra
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C ProgrammingQazi Shahzad Ali
 
Learn C LANGUAGE at ASIT
Learn C LANGUAGE at ASITLearn C LANGUAGE at ASIT
Learn C LANGUAGE at ASITASIT
 
Variables and data types in C++
Variables and data types in C++Variables and data types in C++
Variables and data types in C++Ameer Khan
 
Numeric Data Types & Strings
Numeric Data Types & StringsNumeric Data Types & Strings
Numeric Data Types & StringsAbhinav Porwal
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variableseShikshak
 
C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3Rumman Ansari
 
Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++Ankur Pandey
 

What's hot (20)

Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programming
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programming
 
Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data types
 
Assignment
AssignmentAssignment
Assignment
 
C++ Tokens
C++ TokensC++ Tokens
C++ Tokens
 
Arraysincv109102017 180831194256
Arraysincv109102017 180831194256Arraysincv109102017 180831194256
Arraysincv109102017 180831194256
 
C language
C languageC language
C language
 
primitive data types
 primitive data types primitive data types
primitive data types
 
45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambala45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambala
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Learn C LANGUAGE at ASIT
Learn C LANGUAGE at ASITLearn C LANGUAGE at ASIT
Learn C LANGUAGE at ASIT
 
Data type
Data typeData type
Data type
 
Variables and data types in C++
Variables and data types in C++Variables and data types in C++
Variables and data types in C++
 
Numeric Data Types & Strings
Numeric Data Types & StringsNumeric Data Types & Strings
Numeric Data Types & Strings
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variables
 
C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3
 
Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++
 
Character set of c
Character set of cCharacter set of c
Character set of c
 

Similar to C programming Tutorial Session 4

Strings in C language
Strings in C languageStrings in C language
Strings in C languageP M Patil
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20myrajendra
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulationShahjahan Samoon
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation xShahjahan Samoon
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdfssusere19c741
 
16 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp0216 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp02Abdul Samee
 
C programming day#3.
C programming day#3.C programming day#3.
C programming day#3.Mohamed Fawzy
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programmingmikeymanjiro2090
 

Similar to C programming Tutorial Session 4 (20)

String C Programming
String C ProgrammingString C Programming
String C Programming
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation x
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
16 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp0216 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp02
 
C programming language
C programming languageC programming language
C programming language
 
Java string handling
Java string handlingJava string handling
Java string handling
 
CP Handout#8
CP Handout#8CP Handout#8
CP Handout#8
 
C programming day#3.
C programming day#3.C programming day#3.
C programming day#3.
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
CP-STRING (1).ppt
CP-STRING (1).pptCP-STRING (1).ppt
CP-STRING (1).ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
String.pptx
String.pptxString.pptx
String.pptx
 
14 strings
14 strings14 strings
14 strings
 
C language
C languageC language
C language
 

More from Muhammad Ehtisham Siddiqui

J Query (Complete Course) by Muhammad Ehtisham Siddiqui
J Query (Complete Course) by Muhammad Ehtisham SiddiquiJ Query (Complete Course) by Muhammad Ehtisham Siddiqui
J Query (Complete Course) by Muhammad Ehtisham SiddiquiMuhammad Ehtisham Siddiqui
 
Buiding Next Generation Websites Session 8 by Muhammad Ehtisham Siddiqui
Buiding Next Generation Websites Session 8 by Muhammad Ehtisham SiddiquiBuiding Next Generation Websites Session 8 by Muhammad Ehtisham Siddiqui
Buiding Next Generation Websites Session 8 by Muhammad Ehtisham SiddiquiMuhammad Ehtisham Siddiqui
 
Building Next Generation Websites Session 7 by Muhammad Ehtisham Siddiqui
Building Next Generation  Websites Session 7 by Muhammad Ehtisham SiddiquiBuilding Next Generation  Websites Session 7 by Muhammad Ehtisham Siddiqui
Building Next Generation Websites Session 7 by Muhammad Ehtisham SiddiquiMuhammad Ehtisham Siddiqui
 
Building Next Generation Websites Session 6 by Muhammad Ehtisham Siddiqui
Building Next Generation Websites Session 6 by Muhammad Ehtisham SiddiquiBuilding Next Generation Websites Session 6 by Muhammad Ehtisham Siddiqui
Building Next Generation Websites Session 6 by Muhammad Ehtisham SiddiquiMuhammad Ehtisham Siddiqui
 
Building Next Generation Websites by Muhammad Ehtisham Siddiqui
Building Next Generation Websites by Muhammad Ehtisham SiddiquiBuilding Next Generation Websites by Muhammad Ehtisham Siddiqui
Building Next Generation Websites by Muhammad Ehtisham SiddiquiMuhammad Ehtisham Siddiqui
 

More from Muhammad Ehtisham Siddiqui (20)

C programming Tutorial Session 4
C programming Tutorial Session 4C programming Tutorial Session 4
C programming Tutorial Session 4
 
HTML5 Web storage
HTML5 Web storageHTML5 Web storage
HTML5 Web storage
 
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
J Query (Complete Course) by Muhammad Ehtisham SiddiquiJ Query (Complete Course) by Muhammad Ehtisham Siddiqui
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
 
JavaScript Session 2
JavaScript Session 2JavaScript Session 2
JavaScript Session 2
 
JavaScript Session 3
JavaScript Session 3JavaScript Session 3
JavaScript Session 3
 
Javascript session 1
Javascript session 1Javascript session 1
Javascript session 1
 
Html audio video
Html audio videoHtml audio video
Html audio video
 
Html 5 geolocation api
Html 5 geolocation api Html 5 geolocation api
Html 5 geolocation api
 
Buiding Next Generation Websites Session 8 by Muhammad Ehtisham Siddiqui
Buiding Next Generation Websites Session 8 by Muhammad Ehtisham SiddiquiBuiding Next Generation Websites Session 8 by Muhammad Ehtisham Siddiqui
Buiding Next Generation Websites Session 8 by Muhammad Ehtisham Siddiqui
 
Building Next Generation Websites Session 7 by Muhammad Ehtisham Siddiqui
Building Next Generation  Websites Session 7 by Muhammad Ehtisham SiddiquiBuilding Next Generation  Websites Session 7 by Muhammad Ehtisham Siddiqui
Building Next Generation Websites Session 7 by Muhammad Ehtisham Siddiqui
 
Building Next Generation Websites Session 6 by Muhammad Ehtisham Siddiqui
Building Next Generation Websites Session 6 by Muhammad Ehtisham SiddiquiBuilding Next Generation Websites Session 6 by Muhammad Ehtisham Siddiqui
Building Next Generation Websites Session 6 by Muhammad Ehtisham Siddiqui
 
Building Next Generation Websites by Muhammad Ehtisham Siddiqui
Building Next Generation Websites by Muhammad Ehtisham SiddiquiBuilding Next Generation Websites by Muhammad Ehtisham Siddiqui
Building Next Generation Websites by Muhammad Ehtisham Siddiqui
 
Building Next Generation Websites Session6
Building Next Generation Websites Session6Building Next Generation Websites Session6
Building Next Generation Websites Session6
 
Building Next Generation Websites Session5
Building Next Generation Websites Session5Building Next Generation Websites Session5
Building Next Generation Websites Session5
 
Building Next Generation Websites Session4
Building Next Generation Websites Session4Building Next Generation Websites Session4
Building Next Generation Websites Session4
 
Session4
Session4Session4
Session4
 
Office session14
Office session14Office session14
Office session14
 
Office session13
Office session13Office session13
Office session13
 
Office session12
Office session12Office session12
Office session12
 
Office session11
Office session11Office session11
Office session11
 

Recently uploaded

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 

Recently uploaded (20)

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 

C programming Tutorial Session 4

  • 2. OBJECTIVES • What is String • Declaration of String • Initialization of strings • Reading Strings from user • Reading a line of text • Passing Strings to Functions Presented by: Muhammad Ehtisham Siddiqui (BSCS) 2
  • 3. STRING Presented by: Muhammad Ehtisham Siddiqui (BSCS) 3 • In C programming, array of characters is called a string. • A string is terminated by a null character /0. For example: • "c string tutorial"
  • 4. DECLARATION • Before we actually work with strings, we need to declare them first. • Strings are declared in a similar manner as arrays. Only difference is that, strings are of char type. • char s[5]; Presented by: Muhammad Ehtisham Siddiqui (BSCS) 4
  • 5. INITIALIZATION OF STRINGS • In C, string can be initialized in a number of different ways. • For convenience and ease, both initialization and declaration are done in the same step. • char c[] = "abcd"; • OR, • char c[50] = "abcd"; • String can also be initialized using pointers as: • char *c = "abcd"; Presented by: Muhammad Ehtisham Siddiqui (BSCS) 5
  • 6. READING STRINGS FROM USER Presented by: Muhammad Ehtisham Siddiqui (BSCS) 6 • You can use the scanf() function to read a string like any other data types. • However, the scanf() function only takes the first entered word. • The function terminates when it encounters a white space (or just space). • char c[20]; • scanf("%s", c); •
  • 7. EXAMPLE #1 #include <stdio.h> int main() { char name[20]; printf("Enter name: "); scanf("%s", name); printf("Your name is %s.", name); return 0; } Presented by: Muhammad Ehtisham Siddiqui (BSCS) 7
  • 8. EXAMPLE #2 Presented by: Muhammad Ehtisham Siddiqui (BSCS) 8 • #include <stdio.h> • int main() • { • char name[30], ch; • int i = 0; • printf("Enter name: "); • while(ch != 'n') // terminates if user hit enter • { • ch = getchar(); • name[i] = ch; • i++; • } • name[i] = '0'; // inserting null character at end • printf("Name: %s", name); • return 0; • }
  • 9. EXAMPLE #3 Presented by: Muhammad Ehtisham Siddiqui (BSCS) 9 • #include <stdio.h> • int main() • { • char name[30]; • printf("Enter name: "); • gets(name); //Function to read string from user. • printf("Name: "); • puts(name); //Function to display string. • return 0; • }
  • 10. Presented by: Muhammad Ehtisham Siddiqui (BSCS) 10