SlideShare a Scribd company logo
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 | WeiYuan
Wei-Yuan Chang
 
Poetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePoetry with R -- Dissecting the code
Poetry with R -- Dissecting the code
Peter Solymos
 
php string part 3
php string part 3php string part 3
php string part 3
monikadeshmane
 
What are arrays in java script
What are arrays in java scriptWhat are arrays in java script
What are arrays in java script
Miguel Silva Loureiro
 
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
 
02 arrays
02 arrays02 arrays
02 arrays
Rajan Gautam
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
Gil Cohen
 
Array
ArrayArray
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
 
P2 2017 python_strings
P2 2017 python_stringsP2 2017 python_strings
P2 2017 python_strings
Prof. Wim Van Criekinge
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
Muthu Vinayagam
 
C programming
C programmingC programming
C programming
Samsil Arefin
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
Emertxe Information Technologies Pvt Ltd
 
Kotlin for Android Developers - 2
Kotlin for Android Developers - 2Kotlin for Android Developers - 2
Kotlin for Android Developers - 2
Mohamed Nabil, MSc.
 
Lisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligenceLisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligence
ArtiSolanki5
 
Regression and Classification with R
Regression and Classification with RRegression and Classification with R
Regression and Classification with R
Yanchang 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

C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
Shahzaib Ajmal
 
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
Samsil Arefin
 
String
StringString
String
SANTOSH RATH
 
C Language Lecture 5
C Language Lecture  5C Language Lecture  5
C Language Lecture 5
Shahzaib Ajmal
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
MomenMostafa
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
Rumman Ansari
 
Lecture 1 string functions
Lecture 1  string functionsLecture 1  string functions
Lecture 1 string functions
Awinash 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 programming
CHAITRAB29
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
Chhom Karath
 
C Language Lecture 22
C Language Lecture 22C Language Lecture 22
C Language Lecture 22
Shahzaib Ajmal
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
Shahzaib Ajmal
 
Lecture14.pdf
Lecture14.pdfLecture14.pdf
Lecture14.pdf
JoyPalit
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
manoj11manu
 
06 1 조건문
06 1 조건문06 1 조건문
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
karmuhtam
 
LAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAMLAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAM
SaraswathiRamalingam
 
[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++
Muhammad Hammad Waseem
 
Unit 3 Input Output.pptx
Unit 3 Input Output.pptxUnit 3 Input Output.pptx
Unit 3 Input Output.pptx
Precise Mya
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
8759000398
 
string , pointer
string , pointerstring , pointer
string , pointer
Arafat Bin Reza
 

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

C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
Shahzaib Ajmal
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
Shahzaib Ajmal
 
C Language Lecture 19
C Language Lecture 19C Language Lecture 19
C Language Lecture 19
Shahzaib Ajmal
 
C Language Lecture 18
C Language Lecture 18C Language Lecture 18
C Language Lecture 18
Shahzaib Ajmal
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
Shahzaib Ajmal
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
Shahzaib Ajmal
 
C Language Lecture 14
C Language Lecture 14C Language Lecture 14
C Language Lecture 14
Shahzaib Ajmal
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
Shahzaib Ajmal
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
Shahzaib Ajmal
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
Shahzaib Ajmal
 
C Language Lecture 8
C Language Lecture 8C Language Lecture 8
C Language Lecture 8
Shahzaib Ajmal
 
C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
Shahzaib Ajmal
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
Shahzaib Ajmal
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
Shahzaib Ajmal
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
Shahzaib Ajmal
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
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

How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
dot55audits
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 

Recently uploaded (20)

How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 

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