Introduction to C Programming

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Problem Solving & Program Design in C
Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
Strings string.h library. String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 8 Arrays and Strings
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Chapter 8 Arrays and Strings
1 CHAPTER 7. Objectives: You’ll learn about;  Introduction  Fundamentals of binary data files  Processing binary files  Files with mixed type data.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Characters and Strings File Processing Exercise C Programming:Part 3.
File IO and command line input CSE 2451 Rong Shi.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes C++ errors/debugging build/compile compiler does not recognize a statement build/compile.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
5.6 String Processing Part 2. Sprintf(destnvar,…..regularprintf) Write formatted data to string Same as printf except the output is put in variable. A.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Files A collection of related data treated as a unit. Two types Text
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 7 Text Input/Output Objectives
Lecture 8 String 1. Concept of strings String and pointers
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
C Programming:Part 3 Characters and Strings File Processing Exercise.
CS111 Computer Programming
CS111 Computer Programming
Pointers and Pointer-Based Strings
Text and Binary File Processing
Fundamental of Programming (C)
C Characters and Strings
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

Introduction to C Programming ET2560 Introduction to C Programming Introduction to C Programming Unit 9 Manipulating Strings File Processing Unit 1 Presentations

Unit 8 Review Unit 9: Review of Past Material

Unit 8 Review Arrays Collection of adjacent memory cells Each element has the same data type Array declaration must include size in brackets, after name Array size is a positive integer To access array element, follow the name with subscript Subscript is an integer expression (index) in square brackets Array initializer list in braces Parallel arrays are 2 or more related arrays with identical size Can be passed as argument to function, but not returned

Strings String is not a recognized separate data type in C String variable is an array of characters Sized to accept the maximum number of characters Characters start with element zero All strings must end with a zero-value character '\0'

String Library Functions Unit 9: String Manipulation and File Processing

String Library Since strings are arrays, not single variables, can't use operators Instead, the string library provides functions strcpy(src, dest) - copy "src" (source) to "dest" (destination) strlen(s) - returns the length of string "s" (without 0 terminator) strcat(src, dest) - concatenate "src" to end of "dest" strcmp(s1, s2) - compares "s1" to "s2", returns an int code If s1 precedes s2, returns negative value If s2 precedes s1, returns positive value If equal, returns zero sprintf() - puts formatted data in a string sscanf() - gets formatted data from a string

Using Files in C Unit 9: String Manipulation and File Processing

Files in C All file functions require a file pointer (FILE *) The file library functions (in <stdio.h>) perform file ops Must open a file before using it fopen() library function - returns a file pointer Can open for binary or text file access Text files store data in readable characters - easy to change with editor Binary files store data in machine format - faster to read and write Can open for reading or writing Reading - accesses but does not change a file, used for input Writing - empties or creates a file, then program outputs data When finished using a file, close it with fclose()

Working With Text Files Text files treated like sequential stream of bytes Typically processed from beginning to end To output data to a text file fprintf() - Like printf(), but output goes to the file putchar() - Can output one character at a time to the file puts() - Can output a string to the file To input data from a text file fscanf() - Like scanf(), but input comes from the file getchar() - Can input one character at a time from the file gets() - Can input a string from the file

Working With Binary Files File is a sequential collection of binary data bytes Typically processed from beginning to end Function fwrite() used to output binary data Must provide address of variable, number of bytes, a multiplier (useful for an array size), and file pointer Function fread() used to input binary data