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.

Slides:



Advertisements
Similar presentations
Problem Solving & Program Design in C
Advertisements

Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
C Language.
Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Chapter 9 Character Strings
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 10.
Chapter 8 Characters and Strings 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 8 - Characters and Strings Outline 8.1Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
1 Character Processing How characters can be treated as small integers? How characters are stored and manipulated in a machine? How use is made of certain.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
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.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Basic Input - Output. Output functions  printf() – is a library function that displays information on-screen. The statement can display a simple text.
© 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.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
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.
Lecture 13. Outline Standard Input and Output Standard Input and Output (I/O)– Review & more Buffered/unbuffered input Character I/O Formatted I/O Redirecting.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
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.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
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.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
CTYPE.H Introduction  The ctype header is used for testing and converting characters.  A control character refers to a character that.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
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.
CHARACTER INPUT / OUTPUT AND INPUT VALIDATION. Introduction Input and output devices: keyboards, disk drives, mouse, monitors, printers. I/O functions.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 1.2 Introduction to C++ Programming
C Characters and Strings
User-Written Functions
C Characters and Strings
Character Processing How characters can be treated as small integers?
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 18 I/O in C.
A First Book of ANSI C Fourth Edition
I/O in C + Misc Lecture Sudeshna Sarkar, IIT Kharagpur.
Chapter 8 - Characters and Strings
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Character Processing How characters can be treated as small integers?
Introduction to C EECS May 2019.
C Characters and Strings
Presentation transcript:

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 array of characters. The array of characters stores each characters ASCII code. Every character has an associated ASCII code (American Standard Code for Information Interchange). The code ranges from for upper-case letters, lower-case letters, numeric digits, punctuation marks and other symbols.

Declaring a String A string is an array of characters. For example: To hold a string of 10 characters you need to declare an array of type char with 11 elements. char S[11]; But why 11 and not 10. This is so the 11th position is reserved for the string terminator which is referred by \0. The string terminator \0 is also called the null character.

Initializing the string The string is initialized with character constants. For example: If we want to initialize digits 1 to 9 in a string S, it can be done in the following manner. char S[10] = {1,2,3,4,5,6,7,8,9,\0}; But it is convenient to use a literal string. A literal string is one enclosed in double quotes. S[10] = { }. In this case a null terminator character is not required as the system puts it itself

We can also initialise in the following manner char S[3]; S[0] = A; S[1] = B; S[2] = \0; In this it is the users responsibility to put the terminator character

Accessing Elements in an Array The elements of the string can be acccessed using a for loop. for(i=0; S[i] != \0; i++) { printf(The elements %d is %c,S[i],S[i]); } The condition in the for loop says loop until it reaches the terminator or null character which signifies the actual end of string. Why do I use the word actual? As the string can be declared of a

size 20 but can contain only 7 characters and the 8th character to be a null character. For example: char S[20] = Jamaica; int i; for(i=0; S[i] != \0; i++) { printf(%c, S[I]); } Here it is not ideal to have the size as condition. It is often good programming practice to have the condition of null character in a for loop. When accessing character array elements.

Inputting using scanf The scanf requires a modifier %s to accept a string. For example char S[5]; scanf(%s,S); Here & is not required as S has address of the first element of the character array. S = A scanf requires you to specify the address to

to accept as input. Since S already has the address stored it does not require &. The scanf accepts the input from the first non- white-space character encountered to the next white-space character (space, tab or newline). It does not include the white-space characters. Multiple string can be entered using scan like: scanf(%s %s %s,S,S1,S2); If you enter less number of strings than expected by scanf it will continue to look for remaining strings until you enter it.

Inputting using gets(): The gets() accepts string from keyboard. The gets() reads all characters entered up to the first newline character encountered as opposed to scanf which stops when it encounters any whitespace line tab or space. The syntax is: gets(string name);

Printing Strings The string can be printed using a printf or puts. In the case of printf it needs the format specifier %s. For example: printf(%s,S); In the case of puts the syntax is: puts(string variable/literal string); Example:puts(S); puts(Welcome); puts( ); puts(\n);

Character Test Functions isalnum()Returns true if character is a letter or digit isalpha()Returns true if character is a letter. isascii()Returns true if character is a standard ASCII character (between ) isdigit()Returns true if character is a digit islower()Returns true of character is a lowercase character isupper()Returns true if character is a uppercase character ispunct()Returns true is character is a punctuation character

isspace() Returns true is character is a white space character. This includes tab, space, carriage return isxdigit()Return true if character is a hexadecimal digit (0-9, a - f, A -F) Note The header ctype.h contains the above functions so include this header file.

Miscellaneous Character functions toupperConverts from lowercase to uppercase tolower()Converts from upper-case to lower-case Note: All the character functions are defined in the header file ctype.h. To use these functions, you need to use this header file.

Passing Strings to functions A string is a character array, so it should be passed in in a similar manner as integer arrays. It should in the prototype in the following syntaxes: data type function-name(char arrayname[]); data type function-name(char *arrayname); data type function-name(char arrayname[size of array]); It should be specified in the calling function as: functionName(arrayname);

It should be specified in the function definition or description as: data type functionName(char arrayname[]){} data type functionName(char arrayname[size of the array]){}

Example Problem of Strings in Functions: #include void evalLetter_Digit(char S[]); void main() { char S[20]; do { printf(\nPlease enter the string without any special characters:); gets(S); }while(!isalnum(c))

evalLetter_Digit(char S[]) { int i, alpha,digit = 0; for(i=0; S[i] != \0; i++) { if(isalpha (S[i]) alpha++ else if(isdigit (S[i]) digit++; }

Printf(\n\n The number of letters in the string %s is %d, s, alpha); printf(\n\n The numbr of digits in the string %s is : %d,s,digit); }

Accepting Keyboard Input Most C programs require some form of input from the keyboard (that is, from stdin). Character input The character input functions reads input from a stream one character at a time. When called, each of these functions returns the next character in the stream, or EOF if the end of the file has been reached or an error has occurred. EOF is a symbolic constant defined in stdio.h as –1. Character input functions differ in terms of buffering and echoing

Some character input functions are buffered. This means that the operating system holds all characters in a temporary storage space until you press Enter, and then the system sends the characters to the stdin stream. Others are unbuffered, meaning that each character is sent to stdin as soon as the key is pressed. Some input functions automatically echo each character to stdout as it is received. Others dont echo; the character is sent to stdin and not stdout. Because stdout is assigned to the screen, thats where input is echoed.

The getchar() Function The function getchar() obtains the next character from the stream stdin. It provides buffered character input with echo. The use of getchar is demonstrated below. Notice that the putchar() function simply displays a single character onscreen. 1.#include 2.void main() 3.{int ch = 0; 4.while (ch != '\n') 5.{ 6.ch = getchar(); 7.putchar(ch); 8.} 9.}

On line 4, the getchar()function is called and waits to receive a character from stdin. Because getchar is a buffered input function, no characters are received until you press Enter. However each key you press is echoed immediately on the screen. When you press Enter, all the characters you entered, including the newline, are sent to stdin by the operating system. The getchar() function returns the characters one at a time, assigning each in turn to ch. Each character is compared to the newline character \n and, if not equal, displayed onscreen with putchar(). When a newline is returned by getchar(), the while loop terminates. The getchar() function can be used to input entire lines of text, as shown in the previous example. However, other input functions are better suited for this task.

The getch() Function The getch() function obtains the next character from the stream stdin. It provides unbuffered character input without echo. The getch() functon isnt part of the ANSI standard, meaning it amy not be available on every system. The header file foor this function is conio.h. Because it is unbuffered, getch() returns each character as soon as the key is pressed, without waiting for the user to press Enter. Because getch() doesnt echo its output, the characters arent displayed onscreen. getch() may be used as follows:

#include void main() { int ch; while (ch != \r') { ch = getch(); putchar(ch); }

When this program runs, getch() returns each character as soon as you press a key - it doesnt wait for you to press Enter. There is no echo, so the only reason that each character is displayed onscreen is the call to putchar(). To understand this better remove the line with putchar(ch) and run the program. When you rerun the program you will find that nothing you type is echoed to the screen.

The getche() Function The getche() function is exactly like getch(), except that it echoes each character to stdout. When the program runs, each key you press is displayed onscreen twice – once as echoed by getche(), and once as echoed by putchar(). getche() is not an ANSI-standard comand, but many C compilers support it.