SlideShare a Scribd company logo
1 of 14
Download to read offline
Computing Fundamentals
Dr. Muhammad Yousaf Hamza
Deputy Chief Engineer, PIEAS
Characters and Strings
Dr. Yousaf, PIEAS
Characters
• Characters
– Building blocks of programs
• Every program is a sequence of meaningfully grouped
characters
Dr. Yousaf, PIEAS
#include <stdio.h>
int main()
{
char x = ‘h’;
printf(“Character x is %cn", x); // note the use of %c
getchar();
return 0;
}
Dr. Yousaf, PIEAS
// How to read a character and store in a variable
#include <stdio.h>
int main()
{
char x ;
printf(“Enter a character”);
scanf(“%c",&x); // note the use of %c and &
printf(“ You entered %cn", x); // note the use of %c
getchar();
return 0;
}
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
char x = 'h', y;
printf("Character x is %cn", x); // note the use of %c
printf("Please enter a character and press entern");
y = getchar(); // It is valid. It will give correct output
printf("Character y is %cn", y); // note the use of %c
getchar(); return 0; }
Note:
y = scanf("%c",&y); is not correct, as discussed in the int (or
float) case, previously. Though, it will run but will give you
incorrect output when you print the value of y.
Dr. Yousaf, PIEAS
Strings
• Strings
– When we want to store and print text in C we use a string
– Series of characters treated as a single unit
• Can include letters, digits and special characters (*, /, $)
– char strname[5]="abcd";
– String literal - written in double quotes
“abcd“
A string is an array of type char which has '0'
(named as NULL) as the last character.
– Strings are arrays of characters
• String a pointer to first character
• Value of string is the address of first character (Later)
Dr. Yousaf, PIEAS
String Declaration and Initialization
• String declarations
char c[5]={'a','b','c','d’};
char c[]={'a','b','c','d'}; // it works without mentioning the size
char c[5]="abcd";
char c[]="abcd";
– Declare as a character array or a variable of type char
char color[] = "blue";
– Remember that strings represented as character arrays end with
'0'
• color has 5 elements
• A string is an array of type char which has '0' as the last character.
• '0' is a special character (like 'n') which means "stop now".
– char s[10]="unix"; /* s[4] is '0'; */
– char s[ ]="unix"; /* s has five elements */
Dr. Yousaf, PIEAS
Printing Strings
– printf("Long long ago.");
– Use printf
– char word[] = “Hello”
printf("%s", word); // note the use of %s
Example:
char str[ ] = "A message to display";
printf ("%sn", str);
printf expects to receive a string as an additional
parameter when it sees %s in the format string
printf knows how much to print out because of the
NULL character at the end of all strings.
When it finds a 0, it knows to stop.
Dr. Yousaf, PIEAS
Accessing Individual Characters
• The first element of any array in C is at index 0. The
second is at index 1, and so on ...
char s[6] =“Hello”;
printf("%c", s[2]; // note the use of %c
• This notation can be used in all kinds of statements and
expressions in C:
• For example: char c;
c = s[1];
if (s[0] == '-') …
switch (s[1]) ...
s [0] s[1] s [2] s[3] s[4]
Dr. Yousaf, PIEAS
Example
#include<stdio.h>
int main()
{
char str[15] = "unix and c";
printf("%sn", str); // unix and c
getchar();
return 0;
}
Note: '0' is not printed in the output.
Dr. Yousaf, PIEAS
Example
#include<stdio.h>
int main()
{
char str[15] = "unix and c";
printf("%sn", str);
str[6]='0';
printf("%sn", str);
str[2]='%';
printf("%sn", str);
printf("n");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Example
#include<stdio.h>
int main()
{
char str[15] = "unix and c";
printf("%sn", str);
str[6]='0';
printf("%sn", str);
str[2]='%';
printf("%sn", str);
printf("n");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Example
#include<stdio.h>
int main()
{
char str[15] = "unix and c";
printf("%sn", str); // unix and c
str[6]='0';
printf("%sn", str); // unix a
str[2]='%';
printf("%sn", str); // un%x a
printf("n");
getchar();
return 0;
}
Dr. Yousaf, PIEAS

More Related Content

What's hot

PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
Kumar
 

What's hot (19)

PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Arrays
ArraysArrays
Arrays
 
Smarty Template
Smarty TemplateSmarty Template
Smarty Template
 
Smarty Template
Smarty TemplateSmarty Template
Smarty Template
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
Array in php
Array in phpArray in php
Array in php
 
Array in c
Array in cArray in c
Array in c
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
 
PHP array 1
PHP array 1PHP array 1
PHP array 1
 
arrays in c
arrays in carrays in c
arrays in c
 
Introduction to perl_lists
Introduction to perl_listsIntroduction to perl_lists
Introduction to perl_lists
 
strings in c language and its importance
strings in c language and its importancestrings in c language and its importance
strings in c language and its importance
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 

Similar to C Language Lecture 12

C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
8759000398
 
Review of C.pptx
Review of C.pptxReview of C.pptx
Review of C.pptx
Satyanandaram Nandigam
 

Similar to C Language Lecture 12 (20)

Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
[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++
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
String in c
String in cString in c
String in c
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
3 character strings and formatted input output
3  character strings and formatted input output3  character strings and formatted input output
3 character strings and formatted input output
 
Character array (strings)
Character array (strings)Character array (strings)
Character array (strings)
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 
string , pointer
string , pointerstring , pointer
string , pointer
 
String
StringString
String
 
Array &strings
Array &stringsArray &strings
Array &strings
 
Arrays
ArraysArrays
Arrays
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 
Review of C.pptx
Review of C.pptxReview of C.pptx
Review of C.pptx
 
Strings
StringsStrings
Strings
 

More from Shahzaib Ajmal

More from Shahzaib Ajmal (19)

C Language Lecture 22
C Language Lecture 22C Language Lecture 22
C Language Lecture 22
 
C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
C Language Lecture 19
C Language Lecture 19C Language Lecture 19
C Language Lecture 19
 
C Language Lecture 18
C Language Lecture 18C Language Lecture 18
C Language Lecture 18
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
 
C Language Lecture 8
C Language Lecture 8C Language Lecture 8
C Language Lecture 8
 
C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
 
C Language Lecture 5
C Language Lecture  5C Language Lecture  5
C Language Lecture 5
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
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
kauryashika82
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
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
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

C Language Lecture 12

  • 1. Computing Fundamentals Dr. Muhammad Yousaf Hamza Deputy Chief Engineer, PIEAS
  • 3. Characters • Characters – Building blocks of programs • Every program is a sequence of meaningfully grouped characters Dr. Yousaf, PIEAS
  • 4. #include <stdio.h> int main() { char x = ‘h’; printf(“Character x is %cn", x); // note the use of %c getchar(); return 0; } Dr. Yousaf, PIEAS
  • 5. // How to read a character and store in a variable #include <stdio.h> int main() { char x ; printf(“Enter a character”); scanf(“%c",&x); // note the use of %c and & printf(“ You entered %cn", x); // note the use of %c getchar(); return 0; } Dr. Yousaf, PIEAS
  • 6. #include<stdio.h> int main() { char x = 'h', y; printf("Character x is %cn", x); // note the use of %c printf("Please enter a character and press entern"); y = getchar(); // It is valid. It will give correct output printf("Character y is %cn", y); // note the use of %c getchar(); return 0; } Note: y = scanf("%c",&y); is not correct, as discussed in the int (or float) case, previously. Though, it will run but will give you incorrect output when you print the value of y. Dr. Yousaf, PIEAS
  • 7. Strings • Strings – When we want to store and print text in C we use a string – Series of characters treated as a single unit • Can include letters, digits and special characters (*, /, $) – char strname[5]="abcd"; – String literal - written in double quotes “abcd“ A string is an array of type char which has '0' (named as NULL) as the last character. – Strings are arrays of characters • String a pointer to first character • Value of string is the address of first character (Later) Dr. Yousaf, PIEAS
  • 8. String Declaration and Initialization • String declarations char c[5]={'a','b','c','d’}; char c[]={'a','b','c','d'}; // it works without mentioning the size char c[5]="abcd"; char c[]="abcd"; – Declare as a character array or a variable of type char char color[] = "blue"; – Remember that strings represented as character arrays end with '0' • color has 5 elements • A string is an array of type char which has '0' as the last character. • '0' is a special character (like 'n') which means "stop now". – char s[10]="unix"; /* s[4] is '0'; */ – char s[ ]="unix"; /* s has five elements */ Dr. Yousaf, PIEAS
  • 9. Printing Strings – printf("Long long ago."); – Use printf – char word[] = “Hello” printf("%s", word); // note the use of %s Example: char str[ ] = "A message to display"; printf ("%sn", str); printf expects to receive a string as an additional parameter when it sees %s in the format string printf knows how much to print out because of the NULL character at the end of all strings. When it finds a 0, it knows to stop. Dr. Yousaf, PIEAS
  • 10. Accessing Individual Characters • The first element of any array in C is at index 0. The second is at index 1, and so on ... char s[6] =“Hello”; printf("%c", s[2]; // note the use of %c • This notation can be used in all kinds of statements and expressions in C: • For example: char c; c = s[1]; if (s[0] == '-') … switch (s[1]) ... s [0] s[1] s [2] s[3] s[4] Dr. Yousaf, PIEAS
  • 11. Example #include<stdio.h> int main() { char str[15] = "unix and c"; printf("%sn", str); // unix and c getchar(); return 0; } Note: '0' is not printed in the output. Dr. Yousaf, PIEAS
  • 12. Example #include<stdio.h> int main() { char str[15] = "unix and c"; printf("%sn", str); str[6]='0'; printf("%sn", str); str[2]='%'; printf("%sn", str); printf("n"); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 13. Example #include<stdio.h> int main() { char str[15] = "unix and c"; printf("%sn", str); str[6]='0'; printf("%sn", str); str[2]='%'; printf("%sn", str); printf("n"); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 14. Example #include<stdio.h> int main() { char str[15] = "unix and c"; printf("%sn", str); // unix and c str[6]='0'; printf("%sn", str); // unix a str[2]='%'; printf("%sn", str); // un%x a printf("n"); getchar(); return 0; } Dr. Yousaf, PIEAS