Lecture 20 Arrays and Strings

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

Introduction to C Programming
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Lectures 10 & 11.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 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 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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.
Pointers in C Rohit Khokher
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
1 Review of Chapter 10: String and Pointers. 2 Outline  String:  Representation of a string: \0  Using scanf to read in string  Initilization of strings.
Chapter 10.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Computer Science 210 Computer Organization Strings in C.
Characters & Strings Lesson 2 CS1313 Spring Characters & Strings Lesson 2 Outline 1.Characters & Strings Lesson 2 Outline 2.Character String Declaration.
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.
Introduction to C programming
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
File IO and command line input CSE 2451 Rong Shi.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
1 This chapter covers both string constants (or literals, as they're called in the C standard) and string variables, which can change during the execution.
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”
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
More Pointers and Arrays Kernighan/Ritchie: Kelley/Pohl: Chapter 5 Chapter 6.
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. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Arrays and Pointers.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Today’s Material Strings Definition Representation Initialization
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
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.
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.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Lecture 8 String 1. Concept of strings String and pointers
C Programming Tutorial – Part I
Strings A string is a sequence of characters treated as a group
Arrays in C.
CS111 Computer Programming
C Stuff CS 2308.
C Arrays.
Pointers and Pointer-Based Strings
C++ Programming Lecture 20 Strings
Presentation transcript:

Lecture 20 Arrays and Strings C Programming Lecture 20 Arrays and Strings

Bubble Sort void swap(int *,int *); void bubble(int a[], int n) /* n is the size of a[] */ { int i, j; for (i = 0; i < n; ++i) for (j = n - 1; i < j; --j) if (a[j-1] > a[j]) swap(&a[j-1], &a[j]); } void swap(int *p, int *q); int tmp; tmp = *p; *p = *q; *q = tmp;

Strings and Pointers In C, a string is a one-dimensional array of type char. A character in a string can be accessed either: As an element of an array, or By making use of a pointer to char. The type pointer to char is conceptually a string. A unique aspect of a string is the use of the character value ‘\0’ to terminate the string.

The End-of-String Sentinel \0 In C, a string is terminated by the end-of-string sentinel \0 (the null character). A constant string such as “abc”is stored in memory as four characters, the last one being \0. The length of the string “abc” is 3, but size of the array that holds the string must be at least 4.

Declaring a String To declare an array of characters that will be used to store a string, you must allocate enough storage to: Hold the maximum number of characters that will be stored in the string. Plus one byte (character) to hold the null character.

Example Declaration of a String My name, Paul Higbee, has 11 characters (including the space), but storing my name in as a string requires an array of 12 characters. So I would declare char myname[12];

Inputting a String from the Keyboard How scanf() works with a string: #define MAX_SIZE 100 int main(void) { char w[MAX_SIZE]; . . . scanf(“%s”, w); First scanf() positions the input stream to an initial nonwhite space character. Second, nonwhite space characters are read in and placed in memory beginning at the base address of w. The process stops when a white space character or EOF is encountered. At that point a null character is placed in memory (by the C system) to end the string.

Example: Inputting a String Using scanf() Suppose you want a user to input their first and last names on a single line: char fname[20], lname[20]; . . . printf(“Input your first and last names: “); scanf(“%s%s”, fname, lname); When the user types their first and last name, then presses ENTER, scanf() will: skip any spaces the user input before their first name start reading characters into fname stop when the space between names is reached place a \0 into the fname array skip the space(s) and start reading characters into lname stop reading characters into lname when \n is reached place a \0 into lname Note that the spaces and \n are removed from the input buffer but not stored in the strings.

Example: Inputting a String Using scanf() printf(“Input your first and last names: “); scanf(“%s%s”, fname, lname); When the user types their first and last name, then presses ENTER, scanf() will: skip any spaces the user input before their first name start reading characters into fname stop when the space between names is reached place a \0 into the fname array skip the space(s) and start reading characters into lname stop reading characters into lname when \n is reached place a \0 into lname Note that the spaces and \n are removed from the input buffer but not stored in the strings.

Initialization of Strings Arrays, including character arrays,can be initialized. However,the compiler allows a special syntax for initialization of strings: char s[] = “abc”; is equivalent to char s[] = {‘a’, ’b’, ‘c’, ‘\0’}; When the string-specific first form is used, the size of the string is one more than the string length in order to terminate the string with the \0 null character.

Initializing a Pointer-to-Character with a String A pointer-to-character can be initialized with a constant string, but the interpretation is different. char *p = “abc”; When a character pointer is initialized with a constant string, the pointer simply stores the address where the constant string is stored in memory. When an array is initialized using a constant string, the characters themselves (including the null character) are stored in the array. The string constant itself remains in another location in memory.

Passing Arguments to main() We need to use arrays of pointers to char to write programs that use command line arguments. Two arguments, conventionally called argc and argv can be used with main() to communicate with the operating system. Instead of int main(void) we use int main(int argc, char *argv[])

The Meaning of argc and argv In the header of main: int main(int argc, char *argv[]) the variable argc provides a count of command line arguments the array argv is an array of pointers to char argv can be thought of as an array of strings.

String Handling Functions The standard library contains many useful string handling functions. All require that strings passed as arguments be null-terminated. All return either an integer value or a pointer to char. The prototypes for the string functions are in string.h.

Some String Handling Functions char *strcat(char *s1, const char *s2); strcat() concatenates s2 onto the end of s1 You must ensure that s1 allocated enough space to hold the result. The string s1 is returned. int strcmp(const char *s1, const char *s2); An integer is returned that is less than, equal to, or greater than zero, depending on whether s1 is lexicographically less than, equal to, or greater than s2.

Two More String Handling Functions strcpy(char *s1, const char *s2); The string s2 is copied into s1 until the \0 is moved. Whatever exists in s1 is overwritten. s1 must have enough space to hold the result. s1 is returned. unsigned strlen(const char *s); A count of the number of characters before \0 is returned.

Examples Using String Handling Functions Declarations and Initializations char s1[] = “beautiful big sky country”, s2[] = “how now brown cow”; Expression Value strlen(s1) 25 strlen(s2 + 8) 9 strcmp(s1, s2) negative integer Statements What is Printed printf(“%s”, s1 + 10); big sky country strcpy(s1 + 10, s2 + 8); strcat(s1, “s!”); printf(“%s”, s1); beautiful brown cows!

Common Programming Errors Overrunning the bounds of a string. It is the programmer’s responsibility to make sure enough space is allocated for a string. Don’t forget to allow space for the null character. Can easily occur when strcat() is used. Using ‘a’ when “a” should be used. “a” is stored as two characters‘a’ and ‘\0’. If str is a string variable, then use scanf(“%s”, str); not scanf(“%s”, &str) to input a string.

System Considerations An ANSI C, string constants that are separated by zero or more white space characters are concatenated by the compiler into one long string. Traditional C does not do this. Example char *long_string; long_string = “A list of words:\n” “\n” “ 1 abacus\n” “ 2 bracelet\n” “ 3 cafeteria\n”;