C Programming. Unit 9. Manipulating Strings File Processing.

Size: px
Start display at page:

Download "C Programming. Unit 9. Manipulating Strings File Processing."

Transcription

1 Introduction to C Programming Unit 9 Manipulating Strings File Processing skong@itt-tech.edu

2 Unit 8 Review Unit 9: Review of Past Material

3 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

4 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'

5 String Library Functions Unit 9: String Manipulation and File Processing

6 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 ih 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

7 Declaring and Initializing Strings Declare a string as an array of characters char string_var[30]; Declare and initialize a string char str[20] = "Initial value"

8 String Input & Output Input scanf("%s", &stringname); Or scanf("%s", " stringname); Output Output printf("%s\n", stringname);

9 Figure 9.1 Right and Left Justification of Strings

10 Formatting String Output Right-justified strings printf("***%8s***%3s***\n", "Short", "Strings"); *** Short***Strings*** Strings Left-justified strings printf("%-20s\n", president);

11 Figure 9.3 Execution of scanf ("%s", dept);

12 #define NUM_LENGTH 10 #include <stdio.h> int main(void){ char inp_numstr[num_length]; int inp_num; printf("enter integer only up 10 digit in length \n"); scanf("%s", inp_numstr); printf("input number is : %s\n", inp_numstr); inp_num = atoi(inp_numstr); /* convert string to int*/ printf("converted number is %d\n", inp_num); }

13 Input and Output for an Array of Strings #define NUM_PEOPLE 30 #define NAME_ LEN char names[num_people][name_len]; for (i = 0; i < NUM_PEOPLE; ++i) { scanf( %s%d, names[i]); printf( %-35s\n,names[i]); }

14 Functions in string.h Copy one string to another strcpy strncpy Concatenate strings strcat strncat Compare strings strcmp strncmp Learn string length strlen Separate a string using a delimiter strtok

15 String Assignment Cannot use standard assignment except during initialization char one_str[20]; one_str = "Test string" /*illegal*/ Use strcpy or strncpy instead char one_str[20]; strcpy(one_str, "Test String"); strncpy(one _ str, "Test String", 20);

16 Copying Substrings Use strncpy to identify the number of characters to copy. char month[5]; char s1[15] = "Jan. 30, 1996"; strncpy(month, s1, 4); month[4] = '\0';

17 Copying a Substring from the Middle Pass a pointer to the element where the copy should start. char day[3]; char s1[15] = "Jan. 30, 1996"; strncpy(day, &s1[5], 2); Day[2] = '\0';

18 Concatenating Strings - strcat Use strcat to concatenate one entire string onto another. #define STRSIZ 10 char first[strsiz] = Georg ; char last[strsiz] = Ohm"; strcat(first, st, last); // GeorgOhm"

19 Concatenating Strings - strncat Avoid overflow. #define STRSIZ 12 char first[strsiz] = "Alessandro ; char last[strsiz] = Volta"; AlessandroVolta is > than 12! strncat(first, last, 1); /* AlessandroV"*/

20 Comparison char str1[]= Amp, str2[]= Volt ; if (str1==str2) // this does not work!!! if (strcmp(str1,str2)==0) //does work!!!

21 Finding the String's Length Use strlen to determine the number of characters in a string - \0 not counted. char s1[] = "Charles Coulomb"; for (i=0; i<strlen(s1) ;i++) if (strcmp(s1[i], )==0 printf( Found space\n ); strtok is a special function that will return portions of a string based upon a token. i.e. a comma or dash or space

22 Character vs. String

23 Character Input & Output ctype.h Library Get a single character from standard input: int ch; ch = getchar(); Output a character to standard out: putchar(ch);

24 Figure 9.15 Implementation of scanline Function Using getchar

25 Character Analysis and Conversion isalpha is character A-Z or a-z isdigit is character 0-9 islower is character lower case isupper is character upper case ispunct is character punctuation mark isspace is character a whitespace character (space, tab, newline) tolower Convert to lower case toupper Convert to upper case ** Note ctype.h library needed for all the functions

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

27 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()

28 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

29 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 Must provide address of variable, number of bytes, a multiplier (useful for an array size), and file pointer

30 Newline and EOF Newline Represented by '\n' Added from the keyboard by using a carriage return EOF Represents the end of a file Example This is a text file!<newline> It has two lines.<newline><eof>

31 EOF Example for (status = scanf("%d", &num); status!= EOF; status = scanf("%d", &num)) processnum();

32 EOF Sample Code -1 Figure 5.11 Batch Version of Sum of Exam Scores Program /* * Compute the sum of the list of exam scores stored in the file scores.dat */ #include <stdio.h> /* defines fopen, fclose, fscanf, fprintf, and EOF */ Int main(void) { FILE *inp; /* input file pointer */ int sum = 0, /* sum of scores input so far */ score, /* current score */ input_status; /* status value returned by fscanf */ inp = fopen("scores.dat", "r");

33 EOF Sample Code -2 (continued) printf("scores\n"); input_status = fscanf(inp, "%d", &score); while (input_status!= EOF) { printf("%5d\n", score); sum += score; input_status = fscanf(inp, "%d", &score); } printf("\nsum of exam scores is %d\n", sum); fclose(inp); return (0); }

34 Streams Input stream Can receive from text input device or file stdin normally represents input from the keyboard Output stream Sends text to the display, a printer, or a file stdout normally represents output to the display Error stream Referenced as stderr Sends error information in a text stream Output on the display in an interactive program

35 Table 12.1 Escape Sequences

36 Escape Sequence Example Output: Final Report Using the code: printf( \f\t\t\tfinal Report\r\t\t\t \n );

37 Placeholders for printf Format Strings Table 12.2

38 Field Width, Justification, and Precision

39 Standard I/O vs. File Pointer I/O Table 12.4

40 Binary Files Advantages More optimal for storing structured data Smaller file size Read and write directly, not conversion to text stream Disadvantages Cannot create files with a text editor Cannot view files with a text editor File format proprietary to application

41 Binary File Functions fopen Use "wb" or "rb" as last parameter fwrite fwrite(&i, sizeof(int), 1, binaryp); fwrite(score, sizeof(int), 10, binaryp); fread fread(&i, sizeof(int), 1, binaryp); fread(score, sizeof(int), 10, binaryp); Returns number of elements successfully read

42 Figure 12.3 Creating a Binary File of Integers

43 Text Files vs. Binary Files Example #define STRSIZ 10 #define MAX 40 typedef struct { char name[strsiz]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; double nums[max], data; planet_t a_planet; int I, n, status; FILE *plan_bin_inp, *plan_bin_outp, *plan_txt_inp, *plan_txt_outp; FILE *doub_bin_inp, *doub_bin_outp, *doub_txt_inp, *doub_txt_outp

44 Opening the Files Text plan_txt_inp = fopen("planets.tx t", "r"); doub_txt_inp = fopen("nums.txt", nums.txt "r"); plan_txt_outp = fopen("pl pl_out.txt txt ", "w"); doub_txt_inp = fopen("nm_out.txt ", "w"); Binary plan_bin_inp = fopen("planets.bi n", "rb"); doub_bin_inp = fopen("nums.bin", nums.bin "rb"); plan_bin_outp = fopen("pl pl_out.bin ", "wb"); doub_bin_inp = fopen("nm_out.bin ", "wb");

45 Copy One Planet Structure to Memory Text fscanf(plan_txt_inp, f( "%s%lf%d%lf%lf",%lf%d%lf%lf" a_planet.name, &a_planet.diameter, &a_planet.moons, &a_planet.orbit_time, &a_planet.rotation_time i Binary fread(&a_planet, sizeof (planet_t), t), 1, plan_bin_inp);

46 Write One Planet Structure to the Output File Text fprintf(plan_txt_outp, "%s %e %d %e %e", a_planet.name, a_planet.diameter, a_planet.moons, a_planet.orbit_time, time, a_planet.rotation_time Binary fwrite(&a_planet, sizeof (planet_t), 1, plan_bin_outp);

47 Copy an Array of Numbers to Memory Text for (i=0; i<max; ++i) { fscanf(doub_txt_inp, "%lf", &nums[i]; } Binary fread(nums, sizeof (double), MAX, doub_bin_inp);

48 Write an Array of Numbers to Memory Text for (i=0; i<max; ++i) { fprintf(doub_txt_outp, "%e\n", nums[i]; } Binary fwrite(nums, sizeof (double), MAX, doub_bin_outp);

49 Fill nums with data until EOF is reached Text for (status=fscanf(doub_txt_inp, "%lf", &data); status!= EOF && n < MAX; status=fscanf(doub_txt_inp, "%lf", &data)) nums[n++] = data; Binary n = fread(nums, sizeof (double), MAX, doub_bin_outp);

Strings. Chuan-Ming Liu. Computer Science & Information Engineering National Taipei University of Technology Taiwan

Strings. Chuan-Ming Liu. Computer Science & Information Engineering National Taipei University of Technology Taiwan Strings Chuan-Ming Liu Computer Science & Information Engineering National Taipei University of Technology Taiwan 1 Outline String Basic String Library Functions Longer Strings: Concatenation and Whole-Line

More information

8. Characters, Strings and Files

8. Characters, Strings and Files REGZ9280: Global Education Short Course - Engineering 8. Characters, Strings and Files Reading: Moffat, Chapter 7, 11 REGZ9280 14s2 8. Characters and Arrays 1 ASCII The ASCII table gives a correspondence

More information

C mini reference. 5 Binary numbers 12

C mini reference. 5 Binary numbers 12 C mini reference Contents 1 Input/Output: stdio.h 2 1.1 int printf ( const char * format,... );......................... 2 1.2 int scanf ( const char * format,... );.......................... 2 1.3 char

More information

Chapter 8 - Characters and Strings

Chapter 8 - Characters and Strings 1 Chapter 8 - Characters and Strings Outline 8.1 Introduction 8.2 Fundamentals of Strings and Characters 8.3 Character Handling Library 8.4 String Conversion Functions 8.5 Standard Input/Output Library

More information

Chapter 8 C Characters and Strings

Chapter 8 C Characters and Strings Chapter 8 C Characters and Strings Objectives of This Chapter To use the functions of the character handling library (). To use the string conversion functions of the general utilities library

More information

C PROGRAMMING. Characters and Strings File Processing Exercise

C PROGRAMMING. Characters and Strings File Processing Exercise C PROGRAMMING Characters and Strings File Processing Exercise CHARACTERS AND STRINGS A single character defined using the char variable type Character constant is an int value enclosed by single quotes

More information

Arrays, Strings, & Pointers

Arrays, Strings, & Pointers Arrays, Strings, & Pointers Alexander Nelson August 31, 2018 University of Arkansas - Department of Computer Science and Computer Engineering Arrays, Strings, & Pointers Arrays, Strings, & Pointers are

More information

C: How to Program. Week /May/28

C: How to Program. Week /May/28 C: How to Program Week 14 2007/May/28 1 Chapter 8 - Characters and Strings Outline 8.1 Introduction 8.2 Fundamentals of Strings and Characters 8.3 Character Handling Library 8.4 String Conversion Functions

More information

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction. Chapter 9 Scanning Computer Science & Engineering 150A Problem Solving Using Computers Lecture 07 - Strings Stephen Scott (Adapted from Christopher M. Bourke) Scanning 9.1 String 9.2 Functions: Assignment

More information

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings Computer Science & Engineering 150A Problem Solving Using Computers Lecture 07 - Strings Scanning Stephen Scott (Adapted from Christopher M. Bourke) 1 / 51 Fall 2009 cbourke@cse.unl.edu Chapter 9 Scanning

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 07 - Strings Stephen Scott (Adapted from Christopher M. Bourke) 1 / 51 Fall 2009 Chapter 9 9.1 String 9.2 Functions: Assignment

More information

Characters and Strings

Characters and Strings Characters and Strings 60-141: Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay Character constants A character in single quotes,

More information

Chapter 8: Character & String. In this chapter, you ll learn about;

Chapter 8: Character & String. In this chapter, you ll learn about; Chapter 8: Character & String Principles of Programming In this chapter, you ll learn about; Fundamentals of Strings and Characters The difference between an integer digit and a character digit Character

More information

Standard C Library Functions

Standard C Library Functions Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

C Libraries. Bart Childs Complementary to the text(s)

C Libraries. Bart Childs Complementary to the text(s) C Libraries Bart Childs Complementary to the text(s) 2006 C was designed to make extensive use of a number of libraries. A great reference for student purposes is appendix B of the K&R book. This list

More information

Standard File Pointers

Standard File Pointers 1 Programming in C Standard File Pointers Assigned to console unless redirected Standard input = stdin Used by scan function Can be redirected: cmd < input-file Standard output = stdout Used by printf

More information

C Characters and Strings

C Characters and Strings CS 2060 Character handling The C Standard Library provides many functions for testing characters in ctype.h. int isdigit(int c); // is c a digit (0-9)? int isalpha(int c); // is c a letter? int isalnum(int

More information

Introduction to Computer Programming Lecture 18 Binary Files

Introduction to Computer Programming Lecture 18 Binary Files Introduction to Computer Programming Lecture 18 Binary Files Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr 1 RECALL: Text File Handling

More information

ONE DIMENSIONAL ARRAYS

ONE DIMENSIONAL ARRAYS LECTURE 14 ONE DIMENSIONAL ARRAYS Array : An array is a fixed sized sequenced collection of related data items of same data type. In its simplest form an array can be used to represent a list of numbers

More information

Fundamentals of Programming. Lecture 11: C Characters and Strings

Fundamentals of Programming. Lecture 11: C Characters and Strings 1 Fundamentals of Programming Lecture 11: C Characters and Strings Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu Sharif University of Technology Computer Engineering Department The lectures of this

More information

File I/O. Last updated 10/30/18

File I/O. Last updated 10/30/18 Last updated 10/30/18 Input/Output Streams Information flow between entities is done with streams Keyboard Text input stream data stdin Data Text output stream Monitor stdout stderr printf formats data

More information

UNIT IV-2. The I/O library functions can be classified into two broad categories:

UNIT IV-2. The I/O library functions can be classified into two broad categories: UNIT IV-2 6.0 INTRODUCTION Reading, processing and writing of data are the three essential functions of a computer program. Most programs take some data as input and display the processed data, often known

More information

10/6/2015. C Input/Output. stdin, stdout, stderr. C Language II. CMSC 313 Sections 01, 02. C opens three input/output devices automatically:

10/6/2015. C Input/Output. stdin, stdout, stderr. C Language II. CMSC 313 Sections 01, 02. C opens three input/output devices automatically: C Input/Output stdin, stdout, stderr C opens three input/output devices automatically: C Language II CMSC 313 Sections 01, 02 stdin The "standard input" device, usually your keyboard stdout The "standard

More information

Structured programming

Structured programming Exercises 10 Version 1.0, 13 December, 2016 Table of Contents 1. Strings...................................................................... 1 1.1. Remainders from lectures................................................

More information

C programming basics T3-1 -

C programming basics T3-1 - C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of

More information

Introduction to string

Introduction to string 1 Introduction to string String is a sequence of characters enclosed in double quotes. Normally, it is used for storing data like name, address, city etc. ASCII code is internally used to represent string

More information

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University C Programming Notes Dr. Karne Towson University Reference for C http://www.cplusplus.com/reference/ Main Program #include main() printf( Hello ); Comments: /* comment */ //comment 1 Data Types

More information

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY Strings Arrays of characters Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY 1 Basics Strings A string is a sequence of characters treated as a group We have already

More information

Strings and Library Functions

Strings and Library Functions Unit 4 String String is an array of character. Strings and Library Functions A string variable is a variable declared as array of character. The general format of declaring string is: char string_name

More information

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types Programming Fundamentals for Engineers 0702113 5. Basic Data Types Muntaser Abulafi Yacoub Sabatin Omar Qaraeen 1 2 C Data Types Variable definition C has a concept of 'data types' which are used to define

More information

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017 C Concepts - I/O Lecture 19 COP 3014 Fall 2017 November 29, 2017 C vs. C++: Some important differences C has been around since around 1970 (or before) C++ was based on the C language While C is not actually

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 4 Input & Output Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline printf scanf putchar getchar getch getche Input and Output in

More information

today cs3157-fall2002-sklar-lect05 1

today cs3157-fall2002-sklar-lect05 1 today homework #1 due on monday sep 23, 6am some miscellaneous topics: logical operators random numbers character handling functions FILE I/O strings arrays pointers cs3157-fall2002-sklar-lect05 1 logical

More information

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb CS 201 String Debzani Deb Strings(2) Two interpretations of String Arrays whose elements are characters. Pointer pointing to characters. Strings are always terminated with a NULL characters( \0 ). C needs

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi CE 43 - Fall 97 Lecture 4 Input and Output Department of Computer Engineering Outline printf

More information

SWEN-250 Personal SE. Introduction to C

SWEN-250 Personal SE. Introduction to C SWEN-250 Personal SE Introduction to C A Bit of History Developed in the early to mid 70s Dennis Ritchie as a systems programming language. Adopted by Ken Thompson to write Unix on a the PDP-11. At the

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 11, FALL 2012

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 11, FALL 2012 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 11, FALL 2012 TOPICS TODAY Characters & Strings in C Structures in C CHARACTERS & STRINGS char type C supports the char data type

More information

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 I/O in C Standard C Library I/O commands are not included as part of the C language. Instead, they are part of the Standard C Library. A collection of functions and macros that must be implemented

More information

Scientific Programming in C V. Strings

Scientific Programming in C V. Strings Scientific Programming in C V. Strings Susi Lehtola 1 November 2012 C strings As mentioned before, strings are handled as character arrays in C. String constants are handled as constant arrays. const char

More information

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA 7/21/2014 1 FILE INPUT / OUTPUT Dong-Chul Kim BioMeCIS CSE @ UTA What s a file? A named section of storage, usually on a disk In C, a file is a continuous sequence of bytes Examples for the demand of a

More information

Appendix A Developing a C Program on the UNIX system

Appendix A Developing a C Program on the UNIX system Appendix A Developing a C Program on the UNIX system 1. Key in and save the program using vi - see Appendix B - (or some other editor) - ensure that you give the program file a name ending with.c - to

More information

Input/Output: Advanced Concepts

Input/Output: Advanced Concepts Input/Output: Advanced Concepts CSE 130: Introduction to Programming in C Stony Brook University Related reading: Kelley/Pohl 1.9, 11.1 11.7 Output Formatting Review Recall that printf() employs a control

More information

Contents. Preface. Introduction. Introduction to C Programming

Contents. Preface. Introduction. Introduction to C Programming c11fptoc.fm Page vii Saturday, March 23, 2013 4:15 PM Preface xv 1 Introduction 1 1.1 1.2 1.3 1.4 1.5 Introduction The C Programming Language C Standard Library C++ and Other C-Based Languages Typical

More information

UNIT-I Input/ Output functions and other library functions

UNIT-I Input/ Output functions and other library functions Input and Output functions UNIT-I Input/ Output functions and other library functions All the input/output operations are carried out through function calls. There exists several functions that become

More information

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa) Looping Forward Through the Characters of a C String A lot of C string algorithms require looping forward through all of the characters of the string. We can use a for loop to do that. The first character

More information

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called. Chapter-12 FUNCTIONS Introduction A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called. Types of functions There are two types

More information

Contents. A Review of C language. Visual C Visual C++ 6.0

Contents. A Review of C language. Visual C Visual C++ 6.0 A Review of C language C++ Object Oriented Programming Pei-yih Ting NTOU CS Modified from www.cse.cuhk.edu.hk/~csc2520/tuto/csc2520_tuto01.ppt 1 2 3 4 5 6 7 8 9 10 Double click 11 12 Compile a single source

More information

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments File I/O Content Input Output Devices File access Function of File I/O Redirection Command-line arguments UNIX and C language C is a general-purpose, high-level language that was originally developed by

More information

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program CMPT 102 Introduction to Scientific Computer Programming Input and Output Janice Regan, CMPT 102, Sept. 2006 0 Your first program /* My first C program */ /* make the computer print the string Hello world

More information

ET156 Introduction to C Programming

ET156 Introduction to C Programming ET156 Introduction to C Programming g Unit 22 C Language Elements, Input/output functions, ARITHMETIC EXPRESSIONS AND LIBRARY FUNCTIONS Instructor : Stan Kong Email : skong@itt tech.edutech.edu General

More information

Course organization. Course introduction ( Week 1)

Course organization. Course introduction ( Week 1) Course organization Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 2-9) Chapter 1: Overall Introduction (Week 1-3) Chapter 2: Types, operators and

More information

Chapter 12. Files (reference: Deitel s chap 11) chap8

Chapter 12. Files (reference: Deitel s chap 11) chap8 Chapter 12 Files (reference: Deitel s chap 11) 20061025 chap8 Introduction of File Data files Can be created, updated, and processed by C programs Are used for permanent storage of large amounts of data

More information

C Basics And Concepts Input And Output

C Basics And Concepts Input And Output C Basics And Concepts Input And Output Report Working group scientific computing Department of informatics Faculty of mathematics, informatics and natural sciences University of Hamburg Written by: Marcus

More information

File IO and command line input CSE 2451

File IO and command line input CSE 2451 File IO and command line input CSE 2451 File functions Open/Close files fopen() open a stream for a file fclose() closes a stream One character at a time: fgetc() similar to getchar() fputc() similar to

More information

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files CS113: Lecture 7 Topics: The C Preprocessor I/O, Streams, Files 1 Remember the name: Pre-processor Most commonly used features: #include, #define. Think of the preprocessor as processing the file so as

More information

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

CSCI 6610: Intermediate Programming / C Chapter 12 Strings ... 1/26 CSCI 6610: Intermediate Programming / C Chapter 12 Alice E. Fischer February 10, 2016 ... 2/26 Outline The C String Library String Processing in C Compare and Search in C C++ String Functions

More information

Computer Programming Unit v

Computer Programming Unit v READING AND WRITING CHARACTERS We can read and write a character on screen using printf() and scanf() function but this is not applicable in all situations. In C programming language some function are

More information

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CSC209H Lecture 4. Dan Zingaro. January 28, 2015 CSC209H Lecture 4 Dan Zingaro January 28, 2015 Strings (King Ch 13) String literals are enclosed in double quotes A string literal of n characters is represented as a n+1-character char array C adds a

More information

Input / Output Functions

Input / Output Functions CSE 2421: Systems I Low-Level Programming and Computer Organization Input / Output Functions Presentation G Read/Study: Reek Chapter 15 Gojko Babić 10-03-2018 Input and Output Functions The stdio.h contain

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

ARRAYS(II Unit Part II)

ARRAYS(II Unit Part II) ARRAYS(II Unit Part II) Array: An array is a collection of two or more adjacent cells of similar type. Each cell in an array is called as array element. Each array should be identified with a meaningful

More information

Euclid s algorithm, 133

Euclid s algorithm, 133 Index A Algorithm computer instructions, 4 data and variables, 5 develop algorithm, 6 American Standard Code for Information Interchange (ASCII) codes, 141 definition, 142 features, 142 Arithmetic expressions

More information

Section 3: Library Functions

Section 3: Library Functions Section 3: Library Functions This section of the manual describes the functions available to programs from the standard Xinu library. C programmers will recognize some of the C library functions (esp.

More information

by Pearson Education, Inc. All Rights Reserved.

by Pearson Education, Inc. All Rights Reserved. The string-handling library () provides many useful functions for manipulating string data (copying strings and concatenating strings), comparing strings, searching strings for characters and

More information

Multiple Choice Questions ( 1 mark)

Multiple Choice Questions ( 1 mark) Multiple Choice Questions ( 1 mark) Unit-1 1. is a step by step approach to solve any problem.. a) Process b) Programming Language c) Algorithm d) Compiler 2. The process of walking through a program s

More information

Input/Output and the Operating Systems

Input/Output and the Operating Systems Input/Output and the Operating Systems Fall 2015 Jinkyu Jeong (jinkyu@skku.edu) 1 I/O Functions Formatted I/O printf( ) and scanf( ) fprintf( ) and fscanf( ) sprintf( ) and sscanf( ) int printf(const char*

More information

Approximately a Test II CPSC 206

Approximately a Test II CPSC 206 Approximately a Test II CPSC 206 Sometime in history based on Kelly and Pohl Last name, First Name Last 5 digits of ID Write your section number(s): All parts of this exam are required unless plainly and

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. An important part of the solution to any problem is the presentation of the results. In this chapter, we discuss in depth the formatting features

More information

Chapter 9 Strings. With this array declaration: char s[10];

Chapter 9 Strings. With this array declaration: char s[10]; Chapter 9 Strings 9.1 Chapter Overview There is no data type in C called ʻstringʼ; instead, strings are represented by an array of characters. There is an assortment of useful functions for strings that

More information

Unit 4. Input/Output Functions

Unit 4. Input/Output Functions Unit 4 Input/Output Functions Introduction to Input/Output Input refers to accepting data while output refers to presenting data. Normally the data is accepted from keyboard and is outputted onto the screen.

More information

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16 Department of Computer Science College of Engineering Boise State University September 11, 2017 1/16 1-dimensional Arrays Arrays can be statically declared in C, such as: int A [100]; The space for this

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 INPUT/OUTPUT INPUT AND OUTPUT Programs must be able to write data to files or to physical output devices such as displays or printers, and to read in data from files or

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 07: Data Input and Output Readings: Chapter 4 Input /Output Operations A program needs

More information

Computer Language. It is a systematical code for communication between System and user. This is in two categories.

Computer Language. It is a systematical code for communication between System and user. This is in two categories. ComputerWares There are 3 types of Computer wares. 1. Humanware: The person, who can use the system, is called 'Human Ware ". He is also called as "User". Users are in two types: i. Programmer: The person,

More information

Index. backslash character, 19 backup, off-site, 11. abs, 72 abstraction, 63, 83, 133, 141, 174, 181 acos, 72

Index. backslash character, 19 backup, off-site, 11. abs, 72 abstraction, 63, 83, 133, 141, 174, 181 acos, 72 Index */, 7, 62 ++, 47 -lm, 71 /*, 7, 62 //, 7, 62 #define, 14, 95, 100, 108, 235 #if, 237 #ifdef, 237 #include, 7, 70, 174 FILE, 236 LINE, 236 * operator, 19, 20, 91, 93, 236 + operator, 19, 20, 236 ++

More information

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS CHARACTER STRINGS 1. Overview The learning objective of this lab session is to: Understand the internal representation of character strings Acquire skills in manipulating character strings with standard

More information

C-LANGUAGE CURRICULAM

C-LANGUAGE CURRICULAM C-LANGUAGE CURRICULAM Duration: 2 Months. 1. Introducing C 1.1 History of C Origin Standardization C-Based Languages 1.2 Strengths and Weaknesses Of C Strengths Weaknesses Effective Use of C 2. C Fundamentals

More information

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER: Page 1 of 8 School of Computer Science 60-141-01 Introduction to Algorithms and Programming Winter 2015 Midterm Examination # 2 Wednesday, March 18, 2015 ANSWERS Duration of examination: 75 minutes STUDENT

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e Storage of data in variables and arrays is temporary such data is lost when a program terminates. Files are used for permanent retention of data. Computers store files on secondary

More information

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays Strings and Pointers Announcements Lab 4 Why are you taking this course? Lab 5 #, 8: Reading in data from file using fscanf Quiz Quiz Strings Special character arrays End in null character, \ char hi[6];

More information

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch. 1 Quick review of previous lecture Ch6 Structure Ch7 I/O EECS2031 Software Tools C - Structures, Unions, Enums & Typedef (K&R Ch.6) Structures Basics: Declaration and assignment Structures and functions

More information

Computers Programming Course 11. Iulian Năstac

Computers Programming Course 11. Iulian Năstac Computers Programming Course 11 Iulian Năstac Recap from previous course Cap. Matrices (Arrays) Matrix representation is a method used by a computer language to store matrices of different dimension in

More information

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017 Computer Security Robust and secure programming in C Marius Minea marius@cs.upt.ro 12 October 2017 In this lecture Write correct code minimizing risks with proper error handling avoiding security pitfalls

More information

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters, Strings Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters, decimal digits, special characters and escape

More information

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output Basic I/O COSC2031 - Software Tools C - Input/Output (K+R Ch. 7) We know how to do some basic input and output: getchar - reading characters putchar - writing characters printf - formatted output Input

More information

CSI 402 Lecture 2 Working with Files (Text and Binary)

CSI 402 Lecture 2 Working with Files (Text and Binary) CSI 402 Lecture 2 Working with Files (Text and Binary) 1 / 30 AQuickReviewofStandardI/O Recall that #include allows use of printf and scanf functions Example: int i; scanf("%d", &i); printf("value

More information

Pointers and File Handling

Pointers and File Handling 1 Pointers and File Handling From variables to their addresses Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 2 Basics of Pointers INDIAN INSTITUTE OF TECHNOLOGY

More information

Library Functions. General Questions

Library Functions. General Questions 1 Library Functions General Questions 1. What will the function rewind() do? A. Reposition the file pointer to a character reverse. B. Reposition the file pointer stream to end of file. C. Reposition the

More information

Advanced C Programming Topics

Advanced C Programming Topics Introductory Medical Device Prototyping Advanced C Programming Topics, http://saliterman.umn.edu/ Department of Biomedical Engineering, University of Minnesota Operations on Bits 1. Recall there are 8

More information

CSC 270 Survey of Programming Languages. C-String Values

CSC 270 Survey of Programming Languages. C-String Values CSC 270 Survey of Programming Languages C Lecture 4 Strings C-String Values The most basic way to represent a string of characters in C++ is using an array of characters that ends with a null byte. Example

More information

DATA STRUCTURES USING C

DATA STRUCTURES USING C DATA STRUCTURES USING C File Handling in C Goals By the end of this unit you should understand how to open a file to write to it. how to open a file to read from it. how to open a file to append data to

More information

Applied C and C++ Programming

Applied C and C++ Programming Applied C and C++ Programming Alice E. Fischer David W. Eggert University of New Haven Michael J. Fischer Yale University August 218 Copyright c 218 by Alice E. Fischer, David W. Eggert, and Michael J.

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 10: Arrays Readings: Chapter 9 Introduction Group of same type of variables that have same

More information

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CSC209H Lecture 3. Dan Zingaro. January 21, 2015 CSC209H Lecture 3 Dan Zingaro January 21, 2015 Streams (King 22.1) Stream: source of input or destination for output We access a stream through a file pointer (FILE *) Three streams are available without

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information

CS240: Programming in C

CS240: Programming in C CS240: Programming in C Lecture 13 si 14: Unix interface for working with files. Cristina Nita-Rotaru Lecture 13/Fall 2013 1 Working with Files (I/O) File system: specifies how the information is organized

More information

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++ Characters, c-strings, and the string Class CS 1: Problem Solving & Program Design Using C++ Objectives Perform character checks and conversions Knock down the C-string fundamentals Point at pointers and

More information

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14 Reading Assignment Strings char identifier [ size ] ; char * identifier ; K.N. King Chapter 13 K.N. King Sections 23.4, 23.5 Supplementary reading Harbison & Steele Chapter 12, 13, 14 Strings are ultimately

More information

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments Lecture 7: Files opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments Lecture 5: Files, I/O 0IGXYVI*MPIW 0 opening/closing files reading/writing

More information