CSC 2400 Computer Systems I Lecture 6 Strings. 2 Characters The char type is an 8-bit byte containing ASCII code values (e.g., ‘A’ = 65, ‘B’ = 66,...)

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
Strings.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() 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.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
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.
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
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 Fundamentals of Strings and Characters Characters.
41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.
1 CS 201 String Debzani Deb. 2 Distinction Between Characters and Strings When using strcat, one may be tempted to supply a single character as one of.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CS Nov 2006 C-strings.
Computer Science 210 Computer Organization Strings in C.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
CPSC 441 Tutorial TA: Fang Wang Introduction to C.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
Introduction to C programming
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
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.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
1 Homework HW6 On line – due next class Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
David Notkin Autumn 2009 CSE303 Lecture 17 #preprocessor Debugging is twice as hard as writing the code in the first place. Therefore, if you write the.
Chapter 8: Character and String CMPD144: Programming 1.
Lecture Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
 Integers and Characters  Character Strings  Input and Output.
5-1 Embedded Systems C Programming Language Review and Dissection III Lecture 5.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Strings, Pointers and Tools
Characters and Strings
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
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.
Strings CSCI 112: Programming in C.
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
ECE Application Programming
C Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
File Access (7.5) CSE 2031 Fall July 2018.
Introduction to C CSE 2031 Fall /3/ :33 AM.
CSE 303 Lecture 14 Strings in C
Plan for the Day: I/O (beyond scanf and printf)
Strings A string is a sequence of characters treated as a group
Input and Output Lecture 4.
sscanf()- string scan function
Introduction to C EECS May 2019.
C Characters and Strings
EECE.2160 ECE Application Programming
Presentation transcript:

CSC 2400 Computer Systems I Lecture 6 Strings

2 Characters The char type is an 8-bit byte containing ASCII code values (e.g., ‘A’ = 65, ‘B’ = 66,...) Often, char is treated like (and converted to) int contains character classification functions: isalnum(ch) alphanumeric[a-zA-Z0-9] isalpha (ch) alphabetic[a-zA-Z] isdigit(ch) digit[0-9] ispunct(ch) isspace(ch) white space[ \t\n] isupper(ch) upper-case[A-Z] islower(ch) lower-case[a-z]

3 In Java, strings are regular objects In C, strings are just char arrays with a NUL (‘\0’) terminator “a cat” = A literal string (“a cat”) – is automatically allocated memory space to contain it and the terminating \0 – has a value which is the address of the first character – can’t be changed by the program (common bug!) All other strings must have space allocated to them by the program Strings aca t\0

4 Strings char *makeBig(char *s) { s[0] = toupper(s[0]); return s; } makeBig(“a cat”);

5 Strings We normally refer to a string via a pointer to its first character: char *str = “my string”; char *s; s = &str[0]; s = str; C functions only know string ending by \0: char *str = “my string”;... int i; for (i = 0; str[i] != ‘\0’; i++) putchar(str[i]); char *s; for (s = str; *s; s++) putchar(*s);

6 Strings Can treat like arrays: char c; char line[100]; for (i = 0; i < 100 && line[i]; i++) { if (isalpha(line[i])... }

7 Copying strings Copying content vs. copying pointer to content s = t copies pointer – s and t now refer to the same memory location strcpy(s, t); copies content of t to s char mybuffer[100];... mybuffer = “a cat”; is incorrect (but appears to work!) Use strcpy(mybuffer, “a cat”) instead

8 Example string manipulation #include int main(void) { char line[100]; char *family, *given, *gap; printf(“Enter your name:”); fgets(line,100,stdin); line[strlen(line)-1] = '\0'; given = line; for (gap = line; *gap; gap++) if (isspace(*gap)) break; *gap = ‘\0’; family = gap+1; printf(“Your name: %s, %s\n”, family, given); return 0; }

9 string.h library Assumptions: – #include – strings are NUL -terminated – all target arrays are large enough Operations: – char *strcpy(char *dest, char *source) copies chars from source array into dest array up to NUL – char *strncpy(char *dest, char *source, int num) copies chars; stops after num chars if no NUL before that; appends NUL

10 string.h library int strlen(const char *source) – returns number of chars, excluding NUL char *strchr(const char *source, const char ch) – returns pointer to first occurrence of ch in source; NUL if none char *strstr(const char *source, const char *search) – return pointer to first occurrence of search in source

11 Formatted strings String parsing and formatting (binary from/to text) int sscanf(char *string, char *format,...) – parse the contents of string according to format – placed the parsed items into 3 rd, 4 th, 5 th,... argument – return the number of successful conversions int sprintf(char *buffer, char *format,...) – produce a string formatted according to format – place this string into the buffer – the 3 rd, 4 th, 5 th,... arguments are formatted – return number of successful conversions

12 Formatted strings The format strings for sscanf and sprintf contain – plain text (matched on input or inserted into the output) – formatting codes (which must match the arguments) The sprintf format string gives template for result string The sscanf format string describes what input should look like

13 Formatted strings Formatting codes for sscanf Codemeaningvariable %c matches a single characterchar %d matches an integer in decimalint %f matches a real number (ddd.dd)float %s matches a string up to white spacechar * %[^c] matches string up to next c charchar *

14 Formatted strings Formatting codes for sprintf Values normally right-justified; use negative field width to get left- justified Codemeaningvariable %nc%nc char in field of n spaceschar %nd%nd integer in field of n spacesint, long %n.mf%n.mf real number in width n, m decimals float, double %n.mg%n.mg real number in width n, m digits of precision float, double %n.ms%n.ms first m chars from string in width nchar *

15 Formatted strings - examples char *msg = “Hello there”; char *nums = “ ”; char s[10], t[10]; int a, b, c, n; n = sscanf(msg, “%s %s”, s, t); n = printf(“%10s %-10s\n”, t, s); n = sscanf(nums, “%d %d %d”, &a, &b, &c); printf(“%d flower%s\n”, n, n > 1 ? “s” : “ “); printf(“a = %d, answer = %d\n”, a, b+c);