Week 9 – Lesson 2 Input & Output of Character Strings

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

C Language.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
Kernighan/Ritchie: Kelley/Pohl:
One-dimensional character Arrays C does not have string data type. So strings are represented as arrays of characters. The end of the string is marked.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Khalid Rasheed Shaikh Computer Programming Theory 1.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Computer Programming for Engineers
CS 1704 Introduction to Data Structures and Software Engineering.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Introduction to Programming Using C Arrays. 2 Contents Arrays Subscripting.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
ARRAYS.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
LINKED LISTS.
Lesson #8 Structures Linked Lists Command Line Arguments.
‘C’ Programming Structures and Commands
Strings (Continued) Chapter 13
© 2016 Pearson Education, Ltd. All rights reserved.
Lecture 8 String 1. Concept of strings String and pointers
Input/output.
Revision Lecture
Unit-4, Chapter-2 Managing Input and Output Operations
Quiz 11/15/16 – C functions, arrays and strings
A First Book of ANSI C Fourth Edition
Pointers.
Module 2 Arrays and strings – example programs.
Arrays in C.
Visit for more Learning Resources
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CS111 Computer Programming
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
C Arrays.
Dale Roberts, Lecturer IUPUI
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Week 9 – Lesson 1 Arrays – Character Strings
IPC144 Week 10 – Lesson 2 Working with Files
1) C program development 2) Selection structure
Chapter 9 - Formatted Input/Output
Arrays Strings and Parameter Passing CSCI N305
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Programming Assignment #1 12-Month Calendar—
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Arrays Arrays A few types Structures of related data items
EECE.2160 ECE Application Programming
Character Arrays char string1[] = “first”;
Programming Languages and Paradigms
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Course Outcomes of Programming In C (PIC) (17212, C203):
EECE.2160 ECE Application Programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Getting Started With Coding
Presentation transcript:

Week 9 – Lesson 2 Input & Output of Character Strings IPC144 Week 9 – Lesson 2 Input & Output of Character Strings

Displaying Character Strings In the previous lesson, we could use a for loop with the format specifier “%c” to display each element of the array, since a character string is an “array of characters”. #include<stdio.h> #define SIZE 80 + 1 main() { int i; word1[SIZE] = “hello”; for (i=0; i < SIZE; i++)‏ printf (“%c “, word1[i]); printf (“\n\n”); }

Displaying Character Strings The printf() statement also allows the “%s” format specifier to display the entire character string that is stored in an array of characters. This is extremely convenient since you do NOT require a for loop to display each element in the array of characters. eg. printf (“%s\n\n”, word1);

Displaying Character Strings The “%s” format specifier has additional string alignment and formatting features: “%s” - right justify (0 character offset) “%-s” - left justify (0 character offset) “%25s” - right justify (25 character offset) “%-25s” - left justify (25 character offset)‏ “%-25.15s” - left justify (display no more than 15 characters)

Inputting Character Strings The “%s” format specifier can also be used to read into an array when using the scanf() statement. Note: Since when passing an array to a function, it actually passes a “pointer” to that array, you do NOT use the & symbol. Limitation: “%s” alone does not read multiple words (i.e. words delimited by a space, etc..)‏ eg char word[SIZE+1]; scanf(“%s”, word);

Inputting Character Strings The “%[]” format specifier is used to read in a string, but characters within the square brackets are allowed, and the symbol ^ followed by characters within [] indicates any string except those characters. “%[yYnN]” “%[^0-9]” In this case, if any character not allowed in above examples, then scanning of input stops, and the string value will be terminated...

Inputting Character Strings The “%[^\n]” format specifier can solve the previous problem of scanning multiple words (i.e. reading in white space characters like space). The following method is useful to read a character string with multiple words, but not to include <ENTER> or new-line eg char word[SIZE+1]; scanf(“%[^\n]”, word);

Inputting Character Strings The “%[]” format specifier can also specify how many characters to accept in a line. eg. “%30[^\n]” If more than 30 characters entered, then when <ENTER> is pressed, only first 30 characters assigned to string...

Inputting Character Strings When using scanf with “%s” or “%[^\n]” format specifiers, a function should be created to clear the “\n” character that is stored for the next read. This clearInput function should be called immediately after using scanf function to read in a string.

Inputting Character Strings void clearInput(void)‏ { while(getchar() != '\n')‏ ; } printf ("Please enter string #1: "); scanf ("%[^\n]", x); clearInput(); printf ("Please enter string #2: "); scanf ("%[^\n]", y);

Inputting Character Strings The gets() function is like the getchar() function, except gets() reads in the entire string. eg. gets(name); Similar to scanf(“%[^\n]\n”, name); gets does not know the number of characters passed into the array, so allow a sufficient size to store characters into the array! It might be better to stick to scanf, or functions like getchar() to build a more robust string entry function (see next slide)...

Inputting Character Strings void getString(char x[])‏ { int n=0; char c; while('\n' != (c = getchar()))‏ if (n < SIZE)‏ x[n++] = c; x[n] = '\0'; } /* End of getString function */

Additional Resources Here are some Related-Links for Interest Only: http://cs.senecac.on.ca/~btp100/pages/content/strin.html http://cs.senecac.on.ca/~btp100/pages/content/valid.html http://www.mkssoftware.com/docs/man1/printf.1.asp