SlideShare a Scribd company logo
1 of 28
Download to read offline
Computing Fundamentals
Dr. Muhammad Yousaf Hamza
Deputy Chief Engineer, PIEAS
Characters and Strings
Dr. Yousaf, PIEAS
Printing with puts( )
• For example:
char sentence[] = "The quick brown fox";
puts(sentence);
• Prints out:
The quick brown fox
Dr. Yousaf, PIEAS
#include <stdio.h>
int main()
{
char name1[10]={'y', 'o','u','s','a','f','0'} ;
char name2[10] = "Shahid";
printf("Name1 = %cn", name1[3]); // note the use of %c
printf("Name1 = %sn", name1); // note the use of %s
printf("Name2 = %cn", name2[3]);
printf("Name2 = %sn", name2);
puts(name2);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Inputting String with scanf
• Inputting strings
– Use scanf
scanf("%s", word);
// note the use of %s, no use of & sign
• Copies input into word[]
• Do not need & (because a string is a pointer
(Later))
– Remember to leave room in the array for '0'
// Compare it with
scanf(“%c",& x); // note
the use of %c
Dr. Yousaf, PIEAS
#include <stdio.h>
int main()
{
char fname[20], lname[20];
puts("Please enter your first name (maximum 20 charters) and
then please press enter");
puts("Please enter your Last Name (maximum 20 charters) and
then please press enter");
scanf("%s%s", fname, lname); // to read one or more strings
printf("nWelcome Mr. %s %s", fname,lname);
getchar(); return 0; }
Dr. Yousaf, PIEAS
Inputting Strings with scanf ( )
Inputting Strings with scanf ( )
• To read a string include:
– %s scans up to but not including the “next” white space character
– %ns scans the next n characters or up to the next white space
character, whichever comes first
• Example:
scanf ("%s%s%s", s1, s2, s3); // when ever space would be
entered, scan for the next string will strat
scanf ("%2s%3s%2s", s1, s2, s3); //
– Note: No ampersand(&) when inputting strings into character
arrays! (We’ll explain why later …)
• Difference between gets
– gets( ) reads a line
– scanf("%s",…) read up to the next space
Dr. Yousaf, PIEAS
An Example
#include <stdio.h>
int main ()
{
char lname[81], fname[81];
int count, id_num;
puts ("Enter the last name, firstname, ID number separated by
spaces, then press Enter n");
count = scanf ("%s%s%d", lname, fname,&id_num);
printf ("%d items entered: %s %s %dn",
count,fname,lname,id_num);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
#include <stdio.h>
int main()
{
char name1[20];
puts("Please enter your name , maximum 20 characters and then
please press enter");
gets(name1); // get string, it takes only one string
printf("Name1 = %sn", name1); // note the use of %s
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Inputting Strings with gets( )
Inputting Strings with gets( )
• gets( ) gets a line from the standard input.
• char your_line[100];
printf("Enter a line:n");
gets(your_line);
puts("Your input follows:n");
puts(your_line);
– You can overflow your string buffer, so be careful!
Dr. Yousaf, PIEAS
Inputting String with scanf
• Inputting strings
– Use scanf
scanf("%s", word);
// note the use of %s, no use of & sign
• Copies input into word[]
• Do not need & (because a string is a pointer
(Later))
– Remember to leave room in the array for '0'
// Compare it with
scanf(“%c",& x); // note
the use of %c
Dr. Yousaf, PIEAS
#include <stdio.h>
int main()
{
char fname[20], lname[20];
puts("Please enter your first name (maximum 20 charters) and
then please press enter");
puts("Please enter your Last Name (maximum 20 charters) and
then please press enter");
scanf("%s%s", fname, lname); // to read one or more strings
printf("nWelcome Mr. %s %s", fname,lname);
getchar(); return 0; }
Dr. Yousaf, PIEAS
Inputting Strings with scanf ( )
Inputting Strings with scanf ( )
• To read a string include:
– %s scans up to but not including the “next” white space character
– %ns scans the next n characters or up to the next white space
character, whichever comes first
• Example:
scanf ("%s%s%s", s1, s2, s3); // when ever space would be
entered, scan for the next string will strat
scanf ("%2s%3s%2s", s1, s2, s3); //
– Note: No ampersand(&) when inputting strings into character
arrays! (We’ll explain why later …)
• Difference between gets
– gets( ) reads a line
– scanf("%s",…) read up to the next space
Dr. Yousaf, PIEAS
An Example
#include <stdio.h>
int main ()
{
char lname[81], fname[81];
int count, id_num;
puts ("Enter the last name, firstname, ID number separated by
spaces, then press Enter n");
count = scanf ("%s%s%d", lname, fname,&id_num);
printf ("%d items entered: %s %s %dn",
count,fname,lname,id_num);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
#include <stdio.h>
int main()
{
char name1[20];
puts("Please enter your name , maximum 20 characters and then
please press enter");
gets(name1); // get string, it takes only one string
printf("Name1 = %sn", name1); // note the use of %s
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Inputting Strings with gets( )
Inputting Strings with gets( )
• gets( ) gets a line from the standard input.
• char your_line[100];
printf("Enter a line:n");
gets(your_line);
puts("Your input follows:n");
puts(your_line);
– You can overflow your string buffer, so be careful!
Dr. Yousaf, PIEAS
Strings Operations
Dr. Yousaf, PIEAS
The C String Library
Use #include <string.h>
– Includes functions such as:
• Computing length of string
• Copying strings
• Concatenating strings
• These techniques used to make
– Word processors
– Page layout software
– Typesetting programs
Dr. Yousaf, PIEAS
strlen
• char str[15] = "unix and c";
• printf("length = %d", strlen(str) );
Output: Length = 10
• int sgl;
char str[15] = "unix and c";
sgl = strlen(str)
printf("length = %d", sgl );
Dr. Yousaf, PIEAS
A copy of source is made at destination
– destination should have enough room
(its length should be at least the size of source)
Dr. Yousaf, PIEAS
String Copy (strcpy)
String Copy (strcpy)
// String Copy strcpy
#include <stdio.h>
#include <string.h> // it must be included for string
operations
int main()
{
char name1[20] = "Yousaf";
char name2[20];
strcpy(name2,name1); // copies name1 to name2
printf("Name1 = %sn", name1);
printf("Name2 = %sn", name2);
getchar();
return 0; }
Dr. Yousaf, PIEAS
• Ensure that str1 has sufficient space for the
concatenated string!
– Array index out of range will be the most
popular bug in your C programming career.
Dr. Yousaf, PIEAS
String Concatenation (strcat)
String Concatenation (strcat)
// String Concatenation
#include <stdio.h>
#include <string.h>
int main()
{
char name1[20] = "Muhammad ";
char name2[20]= "Yousaf";
strcat(name1,name2); // name1name2
printf("Name1 = %sn", name1);
printf("Name2 = %sn", name2);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
String Upper Case and Lower Case
#include <stdio.h>
#include <string.h>
int main()
{
char name[20] = "HeLLo";
puts(name);
puts(strlwr(name)); // hello
puts(strupr(name)); // HELLO
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Example
#include <string.h>
#include <stdio.h>
int main() {
char str1[27] = "abc";
char str2[100];
printf("%dn",strlen(str1));
strcpy(str2,str1);
puts(str2);
puts("n");
strcat(str2,str1);
puts(str2);
}
Dr. Yousaf, PIEAS
Character Searching (strchr)
#include<stdio.h>
#include<string.h>
int main()
{
char name[20]= "Yousaf";
char x = ‘b';
if (strchr(name, x) == NULL)
printf ("The character %c was not found.n",x);
else
printf ("The character %c was found", x);
//Rest of the code
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
String Operations
The continue statement
/*Program to determine how many
vowels are in a line of text*/
#include<stdio.h>
#include<conio.h>
int main()
{
char sentence [50];
int i, nvowels = 0, nconsonants = 0;
puts("Enter a line of text");
gets(sentence);
for (i = 0; sentence[i] !='0'; i++)
{
if ( sentence[i] == 'a‘
|| sentence[i] == 'e'
|| sentence[i] == 'i'
|| sentence[i] == 'o‘
|| sentence[i] == 'u‘
|| sentence[i] == 'A‘
|| sentence[i] == 'E'
|| sentence[i] == 'I'
|| sentence[i] == 'O'
|| sentence[i] == 'U' )
{
nvowels++;
continue;
}
if(sentence[i] != ' ')
nconsonants++;
}
printf("No. of Vowels are %dn",
nvowels);
printf("No. of Consonants are %dn",
nconsonants);
getchar(); return 0; }
Dr. Yousaf, PIEAS

More Related Content

What's hot

Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanWei-Yuan Chang
 
Poetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePoetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePeter Solymos
 
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...Matt Harrison
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Matt Harrison
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheetGil Cohen
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)gekiaruj
 
Kotlin for Android Developers - 2
Kotlin for Android Developers - 2Kotlin for Android Developers - 2
Kotlin for Android Developers - 2Mohamed Nabil, MSc.
 
Lisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligenceLisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligenceArtiSolanki5
 
Regression and Classification with R
Regression and Classification with RRegression and Classification with R
Regression and Classification with RYanchang Zhao
 

What's hot (17)

Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuan
 
Poetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePoetry with R -- Dissecting the code
Poetry with R -- Dissecting the code
 
php string part 3
php string part 3php string part 3
php string part 3
 
What are arrays in java script
What are arrays in java scriptWhat are arrays in java script
What are arrays in java script
 
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
 
02 arrays
02 arrays02 arrays
02 arrays
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
Array
ArrayArray
Array
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
P2 2017 python_strings
P2 2017 python_stringsP2 2017 python_strings
P2 2017 python_strings
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
 
C programming
C programmingC programming
C programming
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
Kotlin for Android Developers - 2
Kotlin for Android Developers - 2Kotlin for Android Developers - 2
Kotlin for Android Developers - 2
 
Lisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligenceLisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligence
 
Regression and Classification with R
Regression and Classification with RRegression and Classification with R
Regression and Classification with R
 

Similar to C Language Lecture 13

Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderSamsil Arefin
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string libraryMomenMostafa
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
Lecture 1 string functions
Lecture 1  string functionsLecture 1  string functions
Lecture 1 string functionsAwinash Goswami
 
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 programmingCHAITRAB29
 
Lecture14.pdf
Lecture14.pdfLecture14.pdf
Lecture14.pdfJoyPalit
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3karmuhtam
 
LAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAMLAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAMSaraswathiRamalingam
 
Unit 3 Input Output.pptx
Unit 3 Input Output.pptxUnit 3 Input Output.pptx
Unit 3 Input Output.pptxPrecise Mya
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx8759000398
 

Similar to C Language Lecture 13 (20)

C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical order
 
String
StringString
String
 
C Language Lecture 5
C Language Lecture  5C Language Lecture  5
C Language Lecture 5
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
Lecture 1 string functions
Lecture 1  string functionsLecture 1  string functions
Lecture 1 string functions
 
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
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 
C Language Lecture 22
C Language Lecture 22C Language Lecture 22
C Language Lecture 22
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
 
Lecture14.pdf
Lecture14.pdfLecture14.pdf
Lecture14.pdf
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
06 1 조건문
06 1 조건문06 1 조건문
06 1 조건문
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
LAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAMLAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAM
 
[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++
 
Unit 3 Input Output.pptx
Unit 3 Input Output.pptxUnit 3 Input Output.pptx
Unit 3 Input Output.pptx
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
string , pointer
string , pointerstring , pointer
string , pointer
 

More from Shahzaib Ajmal

More from Shahzaib Ajmal (16)

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 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
C Language Lecture 14
C Language Lecture 14C Language Lecture 14
C Language Lecture 14
 
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 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
 
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

AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint23600690
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17Celine George
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxMarlene Maheu
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 

Recently uploaded (20)

AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 

C Language Lecture 13

  • 1. Computing Fundamentals Dr. Muhammad Yousaf Hamza Deputy Chief Engineer, PIEAS
  • 3. Printing with puts( ) • For example: char sentence[] = "The quick brown fox"; puts(sentence); • Prints out: The quick brown fox Dr. Yousaf, PIEAS
  • 4. #include <stdio.h> int main() { char name1[10]={'y', 'o','u','s','a','f','0'} ; char name2[10] = "Shahid"; printf("Name1 = %cn", name1[3]); // note the use of %c printf("Name1 = %sn", name1); // note the use of %s printf("Name2 = %cn", name2[3]); printf("Name2 = %sn", name2); puts(name2); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 5. Inputting String with scanf • Inputting strings – Use scanf scanf("%s", word); // note the use of %s, no use of & sign • Copies input into word[] • Do not need & (because a string is a pointer (Later)) – Remember to leave room in the array for '0' // Compare it with scanf(“%c",& x); // note the use of %c Dr. Yousaf, PIEAS
  • 6. #include <stdio.h> int main() { char fname[20], lname[20]; puts("Please enter your first name (maximum 20 charters) and then please press enter"); puts("Please enter your Last Name (maximum 20 charters) and then please press enter"); scanf("%s%s", fname, lname); // to read one or more strings printf("nWelcome Mr. %s %s", fname,lname); getchar(); return 0; } Dr. Yousaf, PIEAS Inputting Strings with scanf ( )
  • 7. Inputting Strings with scanf ( ) • To read a string include: – %s scans up to but not including the “next” white space character – %ns scans the next n characters or up to the next white space character, whichever comes first • Example: scanf ("%s%s%s", s1, s2, s3); // when ever space would be entered, scan for the next string will strat scanf ("%2s%3s%2s", s1, s2, s3); // – Note: No ampersand(&) when inputting strings into character arrays! (We’ll explain why later …) • Difference between gets – gets( ) reads a line – scanf("%s",…) read up to the next space Dr. Yousaf, PIEAS
  • 8. An Example #include <stdio.h> int main () { char lname[81], fname[81]; int count, id_num; puts ("Enter the last name, firstname, ID number separated by spaces, then press Enter n"); count = scanf ("%s%s%d", lname, fname,&id_num); printf ("%d items entered: %s %s %dn", count,fname,lname,id_num); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 9. #include <stdio.h> int main() { char name1[20]; puts("Please enter your name , maximum 20 characters and then please press enter"); gets(name1); // get string, it takes only one string printf("Name1 = %sn", name1); // note the use of %s getchar(); return 0; } Dr. Yousaf, PIEAS Inputting Strings with gets( )
  • 10. Inputting Strings with gets( ) • gets( ) gets a line from the standard input. • char your_line[100]; printf("Enter a line:n"); gets(your_line); puts("Your input follows:n"); puts(your_line); – You can overflow your string buffer, so be careful! Dr. Yousaf, PIEAS
  • 11. Inputting String with scanf • Inputting strings – Use scanf scanf("%s", word); // note the use of %s, no use of & sign • Copies input into word[] • Do not need & (because a string is a pointer (Later)) – Remember to leave room in the array for '0' // Compare it with scanf(“%c",& x); // note the use of %c Dr. Yousaf, PIEAS
  • 12. #include <stdio.h> int main() { char fname[20], lname[20]; puts("Please enter your first name (maximum 20 charters) and then please press enter"); puts("Please enter your Last Name (maximum 20 charters) and then please press enter"); scanf("%s%s", fname, lname); // to read one or more strings printf("nWelcome Mr. %s %s", fname,lname); getchar(); return 0; } Dr. Yousaf, PIEAS Inputting Strings with scanf ( )
  • 13. Inputting Strings with scanf ( ) • To read a string include: – %s scans up to but not including the “next” white space character – %ns scans the next n characters or up to the next white space character, whichever comes first • Example: scanf ("%s%s%s", s1, s2, s3); // when ever space would be entered, scan for the next string will strat scanf ("%2s%3s%2s", s1, s2, s3); // – Note: No ampersand(&) when inputting strings into character arrays! (We’ll explain why later …) • Difference between gets – gets( ) reads a line – scanf("%s",…) read up to the next space Dr. Yousaf, PIEAS
  • 14. An Example #include <stdio.h> int main () { char lname[81], fname[81]; int count, id_num; puts ("Enter the last name, firstname, ID number separated by spaces, then press Enter n"); count = scanf ("%s%s%d", lname, fname,&id_num); printf ("%d items entered: %s %s %dn", count,fname,lname,id_num); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 15. #include <stdio.h> int main() { char name1[20]; puts("Please enter your name , maximum 20 characters and then please press enter"); gets(name1); // get string, it takes only one string printf("Name1 = %sn", name1); // note the use of %s getchar(); return 0; } Dr. Yousaf, PIEAS Inputting Strings with gets( )
  • 16. Inputting Strings with gets( ) • gets( ) gets a line from the standard input. • char your_line[100]; printf("Enter a line:n"); gets(your_line); puts("Your input follows:n"); puts(your_line); – You can overflow your string buffer, so be careful! Dr. Yousaf, PIEAS
  • 18. The C String Library Use #include <string.h> – Includes functions such as: • Computing length of string • Copying strings • Concatenating strings • These techniques used to make – Word processors – Page layout software – Typesetting programs Dr. Yousaf, PIEAS
  • 19. strlen • char str[15] = "unix and c"; • printf("length = %d", strlen(str) ); Output: Length = 10 • int sgl; char str[15] = "unix and c"; sgl = strlen(str) printf("length = %d", sgl ); Dr. Yousaf, PIEAS
  • 20. A copy of source is made at destination – destination should have enough room (its length should be at least the size of source) Dr. Yousaf, PIEAS String Copy (strcpy)
  • 21. String Copy (strcpy) // String Copy strcpy #include <stdio.h> #include <string.h> // it must be included for string operations int main() { char name1[20] = "Yousaf"; char name2[20]; strcpy(name2,name1); // copies name1 to name2 printf("Name1 = %sn", name1); printf("Name2 = %sn", name2); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 22. • Ensure that str1 has sufficient space for the concatenated string! – Array index out of range will be the most popular bug in your C programming career. Dr. Yousaf, PIEAS String Concatenation (strcat)
  • 23. String Concatenation (strcat) // String Concatenation #include <stdio.h> #include <string.h> int main() { char name1[20] = "Muhammad "; char name2[20]= "Yousaf"; strcat(name1,name2); // name1name2 printf("Name1 = %sn", name1); printf("Name2 = %sn", name2); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 24. String Upper Case and Lower Case #include <stdio.h> #include <string.h> int main() { char name[20] = "HeLLo"; puts(name); puts(strlwr(name)); // hello puts(strupr(name)); // HELLO getchar(); return 0; } Dr. Yousaf, PIEAS
  • 25. Example #include <string.h> #include <stdio.h> int main() { char str1[27] = "abc"; char str2[100]; printf("%dn",strlen(str1)); strcpy(str2,str1); puts(str2); puts("n"); strcat(str2,str1); puts(str2); } Dr. Yousaf, PIEAS
  • 26. Character Searching (strchr) #include<stdio.h> #include<string.h> int main() { char name[20]= "Yousaf"; char x = ‘b'; if (strchr(name, x) == NULL) printf ("The character %c was not found.n",x); else printf ("The character %c was found", x); //Rest of the code Dr. Yousaf, PIEAS
  • 28. The continue statement /*Program to determine how many vowels are in a line of text*/ #include<stdio.h> #include<conio.h> int main() { char sentence [50]; int i, nvowels = 0, nconsonants = 0; puts("Enter a line of text"); gets(sentence); for (i = 0; sentence[i] !='0'; i++) { if ( sentence[i] == 'a‘ || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] == 'o‘ || sentence[i] == 'u‘ || sentence[i] == 'A‘ || sentence[i] == 'E' || sentence[i] == 'I' || sentence[i] == 'O' || sentence[i] == 'U' ) { nvowels++; continue; } if(sentence[i] != ' ') nconsonants++; } printf("No. of Vowels are %dn", nvowels); printf("No. of Consonants are %dn", nconsonants); getchar(); return 0; } Dr. Yousaf, PIEAS