Characters and Strings

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
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.
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Variables and constants Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
Introduction to Programming
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
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).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Characters and Strings
1 Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Pointers and Dynamic Arrays
Chapter 7 Pointers and C-Strings
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Fundamentals of Characters and Strings
Command Line Arguments
Command line arguments
Command Line Arguments
Command Line Arguments
CSCI206 - Computer Organization & Programming
Programmazione I a.a. 2017/2018.
Chapter 8 - Characters and Strings
Command Line Arguments
Computer Science 210 Computer Organization
C Stuff CS 2308.
Pointers, Dynamic Data, and Reference Types
C Characters and Strings – Review Lab assignments
Arrays Strings and Parameter Passing CSCI N305
Strings Chapter 13 Copyright © 2008 W. W. Norton & Company.
Strings and Pointer Arrays
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
C++ Programming Lecture 20 Strings
Programming in C Pointers and Arrays.
Lecture 19: Working with strings
Characters and Strings
Characters and Strings
C Characters and Strings
Presentation transcript:

Characters and Strings Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Literals and Variables

Fundamentals of Strings and Characters Building blocks of programs Every program is a sequence of meaningfully grouped characters Character constant An int value represented as a character in single quotes 'z' represents the integer value of z Strings Series of characters treated as a single unit Can include letters, digits and special characters (*, /, $) String literal (string constant) - written in double quotes "Hello" Strings are arrays of characters String a pointer to first character Value of string is the address of first character

Fundamentals of Strings and Characters String declarations Declare as a character array or a variable of type char * char color[] = "blue"; char *colorPtr = "blue"; Remember that strings represented as character arrays end with '\0' color has 5 elements Inputting strings Use scanf scanf("%s", word); Copies input into word[] Do not need & (because a string is a pointer) Remember to leave room in the array for '\0'

Character Pointers String constant acts like a character pointer char *pc = “ABCDE”; /* declare a character pointer variable */ Variable Address Value constant 731 ‘A’ constant 732 ‘B’ constant 733 ‘C’ constant 734 ‘D’ constant 735 ‘E’ constant 736 ‘\0’ pc 800 731 char s1[] = “abc”; s1[0] 900 ‘a’ s1[1] 901 ‘b’ s1[2] 902 ‘c’ s1[3] 903 ‘\0’ ‘A’ ‘B’ ‘C’ ‘D’ ‘E’ ‘\0’ 731 732 733 734 735 736 800

Character Pointers Example: s2 s1[] s3[] s char s1[] = “abc”; CONSTANT MEMORY AREA (READ ONLY) Example: char s1[] = “abc”; char *s2 = “abc”; f() { s1[1] = ‘y’; /* OK */ s2[1] = ‘y’; /* wrong (PC is OK)*/ s1 = “test”; /* wrong */ s2 = “test”; /* OK */ } char s3[] = “abcdef”; f1() char *s = s3; *s = ‘A’; /* s3[0]=‘A’ */ s = “test”; printf(“%s\n%s\n”,s,s2); s2 800 1100 100 100 ‘a’ s1[] 1000 1001 1002 1003 ‘a’ ‘b’ ‘c’ ‘\0’ 101 ‘b’ 102 ‘c’ 103 ‘\0’ 104 105 106 107 ‘t’ ‘e’ ‘s’ ‘\0’ 108 1100 s3[] 2000 s 2001 2002 2003 ‘a’ ‘b’ ‘c’ ‘d’ 2004 ‘e’ s3[0]=‘A’ 2000 2001 2002 2003 ‘a’ ‘b’ ‘c’ ‘d’ 2004 ‘e’ ...

Pointer Arrays Syntax: int *pi[3]; float *pf[3]; int i=1, j=2, k=3; /* pi[0], pi[1], pi[2] */ float *pf[3]; /* pf[0], pf[1], pf[2] */ Example 1: int i=1, j=2, k=3; int *pi[3] = {&i, &j, &k}; Example 2: char *pc[3]={“ABC”, “DEF”, “GH”}; Variable Address Value constant 90 ‘A’ constant 91 ‘B’ constant 92 ‘C’ constant 93 ‘\0’ constant 94 ‘D’ constant 95 ‘E’ constant 96 ‘F’ constant 97 ‘\0’ constant 98 ‘G’ constant 99 ‘H’ Constant 100 ‘\0’ pc[0] 200 90 pc[1] 202 94 pc[2] 204 98 Variable Address Value i 80 1 j 82 2 k 84 3 pi[0] 100 pi[1] 101 pi[2] 102 Const can not be changed

Command-Line Arguments argc and argv In environments those support C, there is a way to pass command-line arguments or parameters to a program when it begin executing. When main is called to begin execution, it is called with two arguments – argc and argv argc : The first (conventionally called argc) is the number of command-line arguments the program was invoked with argv : The second (conventionally called argv) is a pointer to an array of character strings that contain the arguments, one per string. Example: if echo is a program and executed on unix prompt, such as 10 <user:/home/droberts> echo hello world pointer array argv argc e c h o \0 h e l l o \0 w o r l d \0 null 3

Command-Line Arguments Example: print out the arguments. ex: hello world main (int argc, char *argv[]) { int i; for (i = 1; i < argc; i++) printf(“%s%c”, argv[i], (i < argc-1) ? ‘ ’ : ‘\n’); } while (--argc > 0) printf(“%s%c”, *++argv, (argc > 1) ? ‘ ’ : ‘\n’); printf((argc > 1) ? “%s “ ; “%s\n“, *++argv);