Strings. Daily Puzzle

Similar documents
LESSON 4. The DATA TYPE char

Strings and Library Functions

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.

Course organization. Course introduction ( Week 1)

CSC 270 Survey of Programming Languages. C-String Values

Chapter 8 - Characters and Strings

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

Introduction to string

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

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

UNIT-I Input/ Output functions and other library functions

Chapter 8 Character Arrays and Strings

Chapter 8 C Characters and Strings

Fundamentals of Programming. Lecture 11: C Characters and Strings

C: How to Program. Week /May/28

Characters and Strings

8. Characters, Strings and Files

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

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

BİL200 TUTORIAL-EXERCISES Objective:

Intermediate Programming, Spring 2017*

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

ONE DIMENSIONAL ARRAYS

Fundamentals of Programming

C Characters and Strings

Lecture 4. Console input/output operations. 1. I/O functions for characters 2. I/O functions for strings 3. I/O operations with data formatting

Fundamental of Programming (C)

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

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

today cs3157-fall2002-sklar-lect05 1

THE FUNDAMENTAL DATA TYPES

ARRAYS(II Unit Part II)

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

scanf erroneous input Computer Programming: Skills & Concepts (CP) Characters Last lecture scanf error-checking our input This lecture

C programming basics T3-1 -

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

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

ECET 264 C Programming Language with Applications. C Program Control

C mini reference. 5 Binary numbers 12

Imperative Programming

Tail recursion. Decision. Assignment. Iteration

Library Functions. General Questions

CCE1111 Programming for Engineers [C Programming Language]

Fundamentals of Programming

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

Solutions to Assessment

Iteration. Side effects

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

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

Array Initialization

C++ Quick Reference. switch Statements

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

SWEN-250 Personal SE. Introduction to C

Approximately a Test II CPSC 206

Standard I/O in C and C++

Strings. Steven R. Bagley

8. Characters and Arrays

printf( Please enter another number: ); scanf( %d, &num2);

Lecture 02 C FUNDAMENTALS

Personal SE. Functions, Arrays, Strings & Command Line Arguments

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

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

Discussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:

Fundamentals of Computer Programming Using C

Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

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

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Fundamental of Programming (C)

Scientific Programming in C V. Strings

Computer Programming: Skills & Concepts (CP) Files in C

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.

20 Dynamic allocation of memory: malloc and calloc

0 vs NULL // BBM 101. Introduction to Programming I. Lecture #12 C Pointers & Strings

A First Book of ANSI C Fourth Edition. Chapter 9 Character Strings

Multiple Choice Questions ( 1 mark)

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

In the case of the dynamic array, the space must be deallocated when the program is finished with it. free(x);

LAB7 : Characters and Strings

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Vidyalankar. F.E. Sem. II Structured Programming Approach Prelim Question Paper Solution

C PROGRAMMING. Characters and Strings File Processing Exercise

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

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

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

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

Chapter 7. Basic Types

Computer Programming Unit v

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

Computers Programming Course 5. Iulian Năstac

Transcription:

Lecture 20 Strings Daily Puzzle German mathematician Gauss (1777-1855) was nine when he was asked to add all the integers from 1 to 100 = (1+100)+(2+99)+... = 5050. Sum all the digits in the integers from 1 through 1,000,000,000 i.e. 1+2+3+4+5+6+7+8+9+1+0+1+1+... +1+0+0+0+0+0+0+0+0+0

Daily Puzzle Solution: 999,999,999 + 0 = 81 999,999,998 + 1 = 81 999,999,997 + 2 = 81 etc. 500,000,000 pairs of 81 = 40,500,000,000 + 1 =40,500,000,001 char Characters are small integers A character constant is written between single quotes, as in a, b, or c.

char A typical declaration for a variable of type char is: char c; Character variables can be initialized as: char c1 = a, c2 = b, c3 = * ; char Characters have an underlying integer-valued representation. There is no particular relationship between the value of the character constant representing a digit and the digits intrinsic integer value. i.e. the value of 7 is not 7.

char The standard ASCII values run from 0 to 127, for a total of 128 values. char Print the ASCII values for the printable characters: void printascii(void) {! int i, j=0;! printf("ascii! Character\n");! for (i=32; i<127; i++)! {! j = j + 1;! printf("%3d is %c ", i, i);! if (j % 5 == 0)! printf("\n");! } }

I/O C provides two functions, getchar() and putchar() to input and output characters respectively. To read a character getchar() is used; to write a character putchar() is used. I/O So, either of the following expressions can be used to read the next character in ch: scanf( %c, &ch);! ch = getchar();

I/O A function to echo characters:! void echo(void)! {! char c;! c = getchar();! putchar(c);! } I/O #include <stdio.h> int main(void) { int c; while ((c = getchar())!= EOF) if ( a <= c && c <= z ) putchar(c + A a ); else if (c == \n ) putchar( \n ); else putchar(c); return 0; }

Character functions The system provides a standard header file ctype.h, which contains a set of macros that are used to analyze characters and functions that are used to convert characters. #include <ctype.h> Character functions These macros all take an argument of type int, and return an int value that is either nonzero (true) or zero (false).

Character functions Macro Nonzero (true) is returned if isalpha(c) c is a letter (a-z, A-Z) isupper(c) c is an uppercase letter (A-Z) islower(c) c is an lowercase letter (a-z) isdigit(c) c is a digit (0-9) isalnum(c) c is a letter or digit (a-z, A-Z, 0-9) isxdigit(c) c is a hexadecimal digit (0-9, a-f, A-F) isspace(c) c is a white space character ispunct(c) c is a punctuation character isprint(c) c is a printable character isgraph(c) c is printable, but not a space iscntrl(c) c is a control character isascii(c) c is an ASCII code Character functions Macro toupper(c) tolower(c) toascii(c) Effect Changes c from lowercase to uppercase Changes c from uppercase to lowercase Changes c to ASCII code

Character functions #include <stdio.h> int main(void) { int c; while ((c = getchar())!= EOF) if (islower(c)) putchar(toupper(c)); else if (c == \n ) putchar( \n ); else putchar(c); return 0; } Character functions A function to check if a character is a vowel. The function return an int value that is either nonzero (true) or zero (false). int isvowel(int c) { switch (c) { case a : ; case e : ; case i : ; case o : ; case u : return 1; break; default : return 0; } }

Ooooh... the buffer One of the inherent things about characters is that they are different. Numbers are happy-go-lucky, self-contained entities - characters not so much. Problem number one is the buffer - the place where keyboard input is stored until processed. Ooooh... the buffer When you type... keypresses are stored in a piece of memory called the buffer 1 0 c a t

What about the buffer?? user enters: 12 4 7 23 1 2 4 7 2 3 program wants: scanf( %d%d%d%d, &a, &b, &c, &d); a=12, b=4, c=7, d=23 buffer empty! What about the buffer?? user enters: 12 cat 1 2 c a t program wants: scanf( %d%c, &a, &b); a=12, b=space c a t buffer not empty!

buffers strout asks for a string and an integer - after which the user hits the enter key! while (true) {! strout();! printf("perform the calculation again (y/n)? ");! scanf("%c", &ans);! if (ans == 'n')!! true = 0; } buffers fix it by reading in the <enter> to a trash variable. while (true) {! strout();! printf("perform the calculation again (y/n)? ");! scanf("%c%c", &ans, &trsh);! if (ans == 'n')!! true = 0; }

buffers use %*c which discards the character input (in the buffer) ~~~~~~~~~~~~~~~~~~~ while (true) {! strout();! printf("perform the calculation again (y/n)? ");! scanf("%c%*c", &ans);! if (ans == 'n')!! true = 0; } buffers while (true) {! strout(); Some methods suggest using fflush. Don t. It is for the output buffer, and using it for stdin is an undefined behavior. Avoid it.! printf("perform the calculation again (y/n)? ");! scanf("%c", &ans); fflush(stdin);! if (ans == 'n')!! true = 0; }

strings A string is a one-dimensional array of type char. The general form of a string is: char string_name[size]; strings SHEAR MADNESS For example, the following creates a string capable of storing 19 characters: char str[20];

strings no & needed char w[100]; scanf( %s, w); the only type of array which can be read in whole used for string I/O strings char w[100]; scanf( %s, w); user types this this is stored in w The quick brown fox The the rest remains unread - and in the buffer

strings A better way is to use the gets function:! char w[100];! gets(w); but it is kind-of insecure running a program containing a gets will result in the message: warning: this program uses gets(), which is unsafe. strings The best way is to use the fgets function:! char w[100];! fgets(w,100,stdin); *way* better reads a maximum of 99 characters from standard input.

initializing strings Recall that arrays, including character arrays, can be initialized. char s[] = { D, N, A, \0 }; The \0 or null character is used to terminate the string. initializing strings There is a second equivalent syntax for initializing character arrays. char s[] = DNA ;

bad things can happen char w[5]; scanf( %s, w); user types this this is stored in w vaderwasasithlord vaderwasasithlord from here on the characters are stored in an illegal manner bad things can happen char w[5]; fgets(w,5,stdin); user types this this is stored in w vaderwasasithlord vade only allows 4 characters as input

string functions The standard library string.h contains many useful string handling functions. string concatenation strcat(s1,s2) appends s2 to s1 and returns s1 char s1[20] = use ; char s2[20] = force ; strcat(s1,s2); s1 now contains useforce

string concatenation strncat(s1,s2,n) appends n characters of s2 to s1 and returns s1 char s1[20] = use ; char s2[20] = force ; strcat(s1,s2,2); s1 now contains usefo string comparison strcmp(s1,s2) compares s1 and s2 - returns an integer <, = or > to 0 depending on whether s1 is lexicographically less than, equal to, or greater than s2. char s1[20] = use ; char s2[20] = force ; x = strcmp(s1,s2); x has the value 15

string comparison strncmp(s1,s2) same as strcmp, except it compares only the first n characters of s1 and s2. char s1[20] = fort ; char s2[20] = force ; x = strncmp(s1,s2,2); x has the value 0 string copying strcpy(s1,s2) string s2 is copied into s1 until \0 is moved. Whatever exists in s1 is overwritten. The string s1 is returned. char s1[20] = use ; char s2[20] = force ; strcpy(s1,s2); s1 has the value force

string copying strncpy(s1,s2) same as strcpy, except it only copies the first n characters of s2 into s1. char s1[20] = use ; char s2[20] = force ; strncpy(s1,s2,1); s1 has the value fse string length strlen(s1) a count of the number of characters in the string s before \0 is found. char s1[20] = use ; x = strlen(s1); x has the value 3

e.g. char s1[20] = usethefork ; char s2[20] = usetheforce ; if(strcmp(s1,s2) == 0) printf( forces are equal\n ); else printf( dark side prevails\n ); an array of strings An array of strings is a two-dimensional array of characters in which each row is one string.

an array of strings For example: char sentence[100][12]; Defines the variable sentence to be an array of strings, capable of storing 100 words, each with a length of 11 characters. string conversions There are three functions, defined in the standard library stdlib.h which convert strings to numbers. atof(s) atoi(s) atol(s) Converts a string s to double. Converts a string s to int. Converts a string s to long int.

string conversions char pi[] = 3.14159 double PI;!! PI = atof(pi); printf( %lf\n, PI); cool string output char str[15]; int n;! printf("enter a string: "); scanf("%s", str);! printf("how many characters of the string do you wish to print? "); scanf("%d", &n);! printf("%.*s", n, str);

getchar is like Lego getchar() can be used to build bigger functions. For example a piece of code to read in integers without errors. getchar is like Lego int readint(void) { char ch, integer[20]; int true = 1, i=0; read in a character do { ch = getchar(); if (ch == '\n' ch == EOF) { true = 0; integer[i] = '\0'; } else if (isdigit(ch)) { integer[i] = ch; i = i + 1; } } while (true); is the character a digit, i.e. 0 to 9? convert from a string to an int. } return atoi(integer);