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.

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

Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CHAPTER 8 CHARACTER AND STRINGS
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.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
File IO and command line input CSE 2451 Rong Shi.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter 3 Input and Output
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
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 12 Files (reference: Deitel ’ s chap 11) chap8.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Computer Programming for Engineers
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Characters and Strings
CS 1704 Introduction to Data Structures and Software Engineering.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Chapter 9 C Formatted Input/Output
TMF1414 Introduction to Programming
File Access (7.5) CSE 2031 Fall July 2018.
A First Book of ANSI C Fourth Edition
Programming in C Input / Output.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Lecture 13 Input/Output Files.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 9 - Formatted Input/Output
Module 12 Input and Output
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

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 as a special character known as null character (a character whose bits are zero, named as NUL in ASCII). Represented as ‘\0’. But C allows string constants. Ex: “Students”

One-dimensional character Arrays Declaration String can be represented as one-dimensional arrays like char name[30]; Initialization character arrays allow a shorthand initialization. Ex : char s[17] = “I love chocolate”; Similarly char s[17]={‘I’,’ ‘,’l’,’o’,’v’,’e’,’ ‘, ’c’,’h’,’o’,’c’,’o’,’l’,’a’,’t’,’e’} can also be written.

One-dimensional character Arrays Initialization Alternatively C allows the declaration like char str[]=“Good morning” ; Here the size of the string is 13 bytes.(12 for letters and one for terminating NUL) Printing String –The conversion type ‘s’ is used to print strings. –Width and precision specifications may be used with %s conversion specifier. Ex : printf(“%8.6s”,str); Whose Output will be “ Good m” But printf(“%-8.6s”,str); will give “Good m “ where the minus sign will make it left justified.

One-dimensional character Arrays Rules When the field width is less than the string then the entire string is printed. The integer value on the right side of the decimal point indicates the no of characters to be printed. When number of characters to be printed is taken as zero, nothing is printed. The minus sign in the specification makes the string to be printed with left justification.

One-dimensional character Arrays Printing String The library function puts() can also be used to print the strings. This function terminates the line with a new line,’\n’. It returns an EOF if an error occurs and returns a positive number upon success. The library function sprintf(), similar to printf() is used to construct formatted strings in memory and to transmit over a communication channel or to a special device. The library function puts() may be used to copy a string to the standard output, whose single parameter is the start address of the string.

One-dimensional character Arrays Printing String Ex : void main() { char buff[100]; float x=1.2345; int I; sprintf(buff,”x=%f”,x); for(i=0;i<=5;i++) puts(buff+i); } will give the output like x= = Here the puts() function puts a new-line every time.

One-dimensional character Arrays String input Strings can be read by using the %s conversion specifier with scanf() function. Scanf() only recognizes a sequence of characters delimited by white space as an external string. It is the job of the programmer to ensure that there is enough memory space to receive and store the incoming string along with the terminating null, which is generated automatically and is stored by scanf(). void main() { char str[30]; scanf(“%s”,str);. printf(“str=%s”,str); if input is intellectual output will be intellectual if input is anu acharya output will be anu. if input is “anuradha” output will be “anuradha”.

One-dimensional character Arrays String input C supports variable field width and precision. Ex : printf(“%*.*s”,w,d,str); Thus printf(“%*.*s”,3,4,str); if str is anuradha then output will be anur. Using Scanset A scanset conversion consists of a list of acceptable characters enclosed within square brackets like [a-z]. If ‘-’ is required actually it must be the first or the last character in the list. If the first character after ‘[‘ is ‘^’ then rest of the scanset specifies unacceptable characters.

One-dimensional character Arrays Using Scanset Ex : scanf(“%[a-z]”,str); if input value is hello Manas then output will be hello manas only. But if input is manas1234 output will be manas only. Single-line input using scanset with ^ If we want to terminate the string when ‘\n’ is entered then we have to write scanf(“%[^\n]”,str); Multiline input using scanset If the string is to be terminated only when ‘+’ is entered then it is required to write scanf(“%[^+]”,str); Here the ‘+’ is not included in the input stream will be stored in the buffer and will be taken as the input for the next scanf() automatically. Both %s and %[ ] automatically generate the NUL.

One-dimensional character Arrays Scanf() with conversion specifier %c Ex : char str[10]; for(i=0;i<=4;i++) {scanf(“%10c”,str); str[9]=‘\0’; printf(“%s”,str);} input output abcdefghi abcdefghi abcdefghijklmnopqrabcdefghi klmnopqr ppppppppp

One-dimensional character Arrays gets() function Reads the complete input line and is stored in the memory area as a null-terminated string. Ex : gets(str); input : Belive in GOD Sscanf() function This function applies scanf() type conversions to data held in a buffer as a single string but will nor read data from standard input. Ex : char inbuf[100]; int num; gets(inbuf); sscanf(inbuf,”%d”,&num); printf(“num = %d”,num); input = 1234 output = 1234

One-dimensional character Arrays atoi() function This function takes the address of an area of memory and converts the string stored at that location to an integer. Sscanf() can do all possible conversions where as atoi() can only do single decimal integer conversions. In the above example the same thing can be done by num=atoi(inbuf);

One-dimensional character Arrays fscanf() and fprintf() There are three I/O streams. They are stdin,stdout and stderr. fprintf() sends formatted output to a stream and fscanf() scans and formats input from a stream. Ex : int x,y; fprintf(stdout,”Enter values for x and y : “); fscanf(stdin,”%d %d”,&x,&y); fprintf(stdout,”x = %d y = %d”,x,y); input :- Enter values for x and y : 2 3 output :- x = 2 y = 3

One-dimensional character Arrays Similarly the output can also be send to stderr like fprintf(stderr,”Error in the input”); Note : If we write int i =‘1’; i will not store 1 but will store the ascii equivalent of the character ‘1’ which is 49. Thus irrespective of the values of ‘1’ and ‘0’ ‘1’-’1’=0 and ‘0’-’0’=0 are true. Or in general c-’0’ gives its value,where c is a variable holding some digit character. So “245” is not the integer 245. It can be converted into the corresponding integer by calling atoi() function like int i; i=atoi(“245”);