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

PPTX
C programming Tutorial Session 2
PPTX
C programming Tutorial Session 3
PPTX
C programming Tutorial Session 1
PPT
Basic concept of c++
PPTX
PPT
Basic of c &c++
PPS
T02 a firstcprogram
PPT
(02) c sharp_tutorial
C programming Tutorial Session 2
C programming Tutorial Session 3
C programming Tutorial Session 1
Basic concept of c++
Basic of c &c++
T02 a firstcprogram
(02) c sharp_tutorial

What's hot (19)

PPTX
Constants and variables in c programming
PPT
Basics of c++
ODP
(2) cpp imperative programming
PPTX
Constant, variables, data types
PDF
Assignment
PDF
C++ Tokens
PDF
Arraysincv109102017 180831194256
PPS
C language
PPTX
45 Days C++ Programming Language Training in Ambala
PPTX
Programming in c
PPTX
Data Type in C Programming
PPTX
Learn C LANGUAGE at ASIT
PPT
Data type
PPTX
Variables and data types in C++
PPTX
Numeric Data Types & Strings
PPT
Mesics lecture 3 c – constants and variables
PPTX
C Programming Language Step by Step Part 3
PDF
Keywords, identifiers ,datatypes in C++
PPTX
Character set of c
Constants and variables in c programming
Basics of c++
(2) cpp imperative programming
Constant, variables, data types
Assignment
C++ Tokens
Arraysincv109102017 180831194256
C language
45 Days C++ Programming Language Training in Ambala
Programming in c
Data Type in C Programming
Learn C LANGUAGE at ASIT
Data type
Variables and data types in C++
Numeric Data Types & Strings
Mesics lecture 3 c – constants and variables
C Programming Language Step by Step Part 3
Keywords, identifiers ,datatypes in C++
Character set of c
Ad

Similar to C programming Tutorial Session 4 (20)

PDF
Strings IN C
PPTX
Strings in C language
PPTX
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
PPT
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
PPTX
String.pptx
PPTX
pps unit 3.pptx
PDF
0-Slot21-22-Strings.pdf
DOCX
C Programming Strings.docx
PPT
Strings
PDF
String notes
PPT
Chapterabcdefghijklmnopqrdstuvwxydanniipo
PPT
THE FORMAT AND USAGE OF STRINGS IN C.PPT
PPT
Lecture 05 2017
PDF
5 2. string processing
PPTX
Week6_P_String.pptx
PDF
C programming & data structure [character strings & string functions]
PDF
[ITP - Lecture 17] Strings in C/C++
PPTX
C programming - String
PPT
PPTX
Handling of character strings C programming
Strings IN C
Strings in C language
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
String.pptx
pps unit 3.pptx
0-Slot21-22-Strings.pdf
C Programming Strings.docx
Strings
String notes
Chapterabcdefghijklmnopqrdstuvwxydanniipo
THE FORMAT AND USAGE OF STRINGS IN C.PPT
Lecture 05 2017
5 2. string processing
Week6_P_String.pptx
C programming & data structure [character strings & string functions]
[ITP - Lecture 17] Strings in C/C++
C programming - String
Handling of character strings C programming
Ad

More from Muhammad Ehtisham Siddiqui (20)

PPTX
C programming Tutorial Session 4
PPTX
HTML5 Web storage
PPTX
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
PPTX
JavaScript Session 2
PPTX
JavaScript Session 3
PPTX
Javascript session 1
PPTX
Html audio video
PPTX
Html 5 geolocation api
PPTX
Buiding Next Generation Websites Session 8 by Muhammad Ehtisham Siddiqui
PPTX
Building Next Generation Websites Session 7 by Muhammad Ehtisham Siddiqui
PPTX
Building Next Generation Websites Session 6 by Muhammad Ehtisham Siddiqui
PPTX
Building Next Generation Websites by Muhammad Ehtisham Siddiqui
PPTX
Building Next Generation Websites Session6
PPTX
Building Next Generation Websites Session5
PPTX
Building Next Generation Websites Session4
PPTX
Office session14
PPTX
Office session13
PPTX
Office session12
PPTX
Office session11
C programming Tutorial Session 4
HTML5 Web storage
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
JavaScript Session 2
JavaScript Session 3
Javascript session 1
Html audio video
Html 5 geolocation api
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 6 by Muhammad Ehtisham Siddiqui
Building Next Generation Websites by Muhammad Ehtisham Siddiqui
Building Next Generation Websites Session6
Building Next Generation Websites Session5
Building Next Generation Websites Session4
Office session14
Office session13
Office session12
Office session11

Recently uploaded (20)

PPTX
Odoo ERP for Injection Molding Industry – Optimize Production & Reduce Scrap
PPTX
Bandicam Screen Recorder 8.2.1 Build 2529 Crack
PPTX
How to Odoo 19 Installation on Ubuntu - CandidRoot
PPTX
Viber For Windows 25.7.1 Crack + Serial Keygen
PDF
CapCut PRO for PC Crack New Download (Fully Activated 2025)
PPTX
MLforCyber_MLDataSetsandFeatures_Presentation.pptx
PDF
MiniTool Power Data Recovery 12.6 Crack + Portable (Latest Version 2025)
PDF
What Makes a Great Data Visualization Consulting Service.pdf
PPTX
Lecture 5 Software Requirement Engineering
PPTX
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
PPTX
Plex Media Server 1.28.2.6151 With Crac5 2022 Free .
PDF
Crypto Loss And Recovery Guide By Expert Recovery Agency.
PDF
Cloud Native Aachen Meetup - Aug 21, 2025
PPTX
ROI from Efficient Content & Campaign Management in the Digital Media Industry
PPTX
string python Python Strings: Literals, Slicing, Methods, Formatting, and Pra...
PDF
AI-Powered Fuzz Testing: The Future of QA
PPTX
Foundations of Marketo Engage: Nurturing
PDF
Streamlining Project Management in Microsoft Project, Planner, and Teams with...
PPTX
4Seller: The All-in-One Multi-Channel E-Commerce Management Platform for Glob...
PDF
Website Design & Development_ Professional Web Design Services.pdf
Odoo ERP for Injection Molding Industry – Optimize Production & Reduce Scrap
Bandicam Screen Recorder 8.2.1 Build 2529 Crack
How to Odoo 19 Installation on Ubuntu - CandidRoot
Viber For Windows 25.7.1 Crack + Serial Keygen
CapCut PRO for PC Crack New Download (Fully Activated 2025)
MLforCyber_MLDataSetsandFeatures_Presentation.pptx
MiniTool Power Data Recovery 12.6 Crack + Portable (Latest Version 2025)
What Makes a Great Data Visualization Consulting Service.pdf
Lecture 5 Software Requirement Engineering
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
Plex Media Server 1.28.2.6151 With Crac5 2022 Free .
Crypto Loss And Recovery Guide By Expert Recovery Agency.
Cloud Native Aachen Meetup - Aug 21, 2025
ROI from Efficient Content & Campaign Management in the Digital Media Industry
string python Python Strings: Literals, Slicing, Methods, Formatting, and Pra...
AI-Powered Fuzz Testing: The Future of QA
Foundations of Marketo Engage: Nurturing
Streamlining Project Management in Microsoft Project, Planner, and Teams with...
4Seller: The All-in-One Multi-Channel E-Commerce Management Platform for Glob...
Website Design & Development_ Professional Web Design Services.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