Today’s Material Strings Definition Representation Initialization

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
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 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
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.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
Chapter 10.
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.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 8 Arrays and Strings
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C-strings Array with base type char One character per indexed variable
Computer Science 210 Computer Organization Strings in 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.
1 بنام خدا زبان برنامه نویسی C (21814( Lecture 13 Chapter 13 Strings.
Introduction to C programming
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 13 Strings (Continued)
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
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)
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
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”
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.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
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.
COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez.
Strings, Pointers and Tools
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
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.
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.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
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.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 13 Strings (Continued)
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.
Strings (Continued) Chapter 13
Chapter 9 Strings Copyright © 2016 Pearson, Inc. All rights reserved.
Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
Strings A string is a sequence of characters treated as a group
Arrays in C.
Pointers, Dynamic Data, and Reference Types
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length.
Strings Chapter 13 Copyright © 2008 W. W. Norton & Company.
Chapter 8 Character Arrays and Strings
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Presentation transcript:

Today’s Material Strings Definition Representation Initialization Reading/Writing Strings String Operations: strlen, strcpy, strcat, strcmp Array of Strings Command Line Arguments

Strings: Definition A string is an array of characters terminated by the NULL char ‘\0’ Example: char str[8]; Declares a char array that contains at most 8 chars If char array str will be used to store strings, it can contain at most 7 chars and MUST be terminated by the NULL char ‘\0’

Strings: Representation If we store the string “abcd” in str, here is how it will look like We do not know what’s stored after the NULL char str: a b c d \0 ? ? ? 1 2 3 4 5 6 7 String MUST be NULL terminated, i.e., the end of the string must be set to 0

Empty String Empty String “” corresponds to a char array whose first element is the NULL char ‘\0’ str: \0 ? ? ? ? ? ? ? 1 2 3 4 5 6 7 The first char of the empty string is the NULL char

Max Length String A string of length 8, e.g., “abcdefgh” can NOT be stored in str str: a b c d e f g h 1 2 3 4 5 6 7 No place to store the NULL char! This is a char array containing 8 chars But this is NOT a string. A string MUST always be terminated by the NULL char

Strings: Warning Just to stress again, a declaration such as char str[8] simply tells us that we can store at most 8 chars in str At any point during the program execution we may be storing up to 8 chars in str But if “str” is to store a string, we can store at most 8-1=7 chars in it, and must always terminate the string with the NULL char

String Initialization A char array may be initialized during declaration just like any other array char str[8] = {‘a’, ‘b’, ‘c’}; Recall that unspecified array elements are filled with 0, that is, the NULL char. So the above declaration corresponds to the following string a b c \0 1 2 3 4 5 6 7 str: NULL terminated as required

String Initialization (cont) If a char array will store strings, there is a simpler way to specify the initializer We just put the string inside double quotes, called a string literal char str[8] = “abc”; /* same as before */ a b c \0 1 2 3 4 5 6 7 str: NULL terminated as required

String Initialization (cont) If we don’t specify the length of the array in the initializer, the compiler will allocate as many chars as the length of the string + the NULL char char str[] = “abc”; str: a b c \0 1 2 3

String Initialization (cont) Specific to strings, we can initialize string literals as follows: char *str = “abc”; str: a b c \0 1 2 3 The difference between this declaration and the previous declaration is that the strings initialized this way are READ-ONLY and can NOT be modified Strings declared as char str[]=“abc”; can be modified if desired

Note on char *p; declarations char *p; simply declares a pointer to a char, but does not allocate any space for a string You can use this pointer to point to strings and manipulate them, but cannot use this pointer without first pointing it to a valid string char *p; char str[] = “abcde”; *p = ‘x’; /* Illegal at this point */ p = &str[1]; *p = ‘x’; /* OK now */

Wrap-up Example char *str1=“abc”; /* A string literal of 4 bytes. Can’t be changed */ char str2[]=“abc”;/* A string of 4 bytes. Can be changed */ char str3[20]=“abc”; /* A string of 20 bytes (19 chars+1 NULL char) * Initialized to abc */ char str4[40]; /* An array of 40 chars. Can store a string of * length at most 39 chars. One char must be * left for the NULL char */ char *p; /* Simply declares a char pointer that is not pointing to * a valid memory address yet. This is NOT a string. * *p would be invalid at this point */ main(){ p = &str2[1]; *p = ‘X’; /* Makes str2 = “aXc” */ p = str3; *p = ‘Y’; /* Makes str3 = “Ybc” */ p = &str3[3]; *p = ‘Z’; *(p+1) = ‘\0’; /* Makes str3 = “YbcZ” */ str4[0] = ‘k’; str4[1]=‘l’; str4[2]=‘\0’; /* str4 = “kl” */ printf(“str1: <%s>, str2: <%s>, str3: <%s>, str4: <%s>\n”, str1, str2, str3, str4); } /* end-main */

Location of Strings in Program’s Address Space Stack Variables with automatic storage duration i.e., local variables and function parameters Dynamically allocated data Heap Variables (data) allocated with malloc uninitialized data (bss) str4 would be here initialized data Data str2 & str3 would be here .rodata section str1 & printf’s string literals would be here Code Text

Printing Strings C allows you to print strings using 2 functions (1) puts(str); (2) printf(“%s”, str); char str1[]=“This is my first string”; /* Prints the string and moves the cursor to * the beginning of the next line */ puts(str1); /* Prints the string starting at the cursor position */ printf(“%s”, str1); /* Reserve 40 spaces and print the string right-adjusted */ printf(“%40s”, str1); /* Reserve 40 spaces and print the string left-adjusted */ printf(“%-40s”, str1);

Printing Strings (cont) C allows you to print strings using 2 functions (1) puts(str); (2) printf(“%s”, str); char str1[]=“This is my first string”; /* Print only the first 10 chars of the string, * right-adjusted*/ printf(“%.10s”, str1); /* Reserve 40 spaces and print only the first 10 chars * of the string, right-adjusted */ printf(“%40.10s”, str1); * of the string, left-adjusted */ printf(“%-40.10s”, str1);

Reading Strings C allows you to read strings using 2 functions (1) gets(str); (2) scanf(“%s”, str); char str2[80]; /* Reads the current input chars until the end of the line * ‘\n’ char is seen */ gets(str2); /* Skips all white space chars (space, tab, newline), and * start reading input until the next space char is seen */ scanf(“%s”, str2);

Reading Strings (cont) char str2[80]; /* Skips all white space chars (space, tab, newline), and * start reading input until the next space char is seen */ scanf(“%s”, str2); /* If the input is the following: _ is a space char */ _ _xyz123_ _ _45_ _67 scanf will skip 2 white spaces and start filling str2 with “xyz123” Then it will encounter a white space and stop reading input The next scanf(“%s”, …) will skip these white spaces and return “45”

Reading Strings (cont) You can write your own function to read a string until the end of the line is encountered char *ReadLine(char *str){ char ch; char *p = str; while((ch=getchar()) != ‘\n’) *p++=ch; *p = ‘\0’; /* Mark the end of the string with NULL char */ return str; } /* end-ReadLine */ main(){ char str[80]; ReadLine(str); printf(“Line of input = <%s>\n”, str); } /* end-main */

Reading Strings (cont) Another version would be to read until the end of the line is seen OR until “n” chars are read char *ReadNLine(char *str, int n){ char ch; char *p = str; while (n-- > 0){ if ((ch = getchar()) == ‘\n’) break; *p++ = ch; } /* end-while */ *p = ‘\0’; /* Mark the end of the string with NULL char */ return str; } /* end-ReadNLine */ main(){ char str[80]; char *p = NULL; p = ReadNLine(str, 79); /* Can store at most 79 chars */ printf(“Line of input = <%s>\n”, p); } /* end-main */

String Operations: strlen C standard library provides many functions to manipulate strings You need to include <string.h> to use these functions Here are some of the important functions strlen(const char *str); strcpy(char *str1, const char *str2); strcat(char *str1, const char *str2); strcmp(const char *str1, const char *str2); Here are the implementations for these functions just for illustration

String Operations: strlen, strcpy int strlen(const char *str){ int len = 0; while (*str++) len++; return len; } /* end-strlen */ char *strcpy(char *str1, const char *str2){ char *p = str1; while (*str2) *p++ = *str2++; *p = ‘\0’; return str1; } /* end-strcpy */

String Operations: strcat, strcmp char *strcat(char *str1, const char *str2){ char *p = str1; while(*p) p++; while (*str2) *p++ = *str2++; *p = ‘\0’; return str1; } /* end-strcat */ int strcmp(const char *str1, const char *str2){ while (*str1 && *str2 && *str1 == *str2){ str1++; str2++; } /* end-while */ return *str1-*str2; } /* end-strcmp */

Array of Strings How do we store an array of strings? Here is one way to declare an array of strings char planets[][8] = { “Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”, “Pluto”}; We are allowed to omit the number of rows Since that is obvious from the number of elements C requires that we specify the number of columns

Array of Strings Layout of this declaration would be as follows: 1 2 3 4 5 6 7 M e r c u r y \0 1 V e n u s \0 \0 \0 2 E a r t h \0 \0 \0 3 M a r s \0 \0 \0 \0 4 J u p i t e r \0 5 S a t u r n \0 \0 6 U r a n u s \0 \0 7 N e p t u n e \0 8 P l u t o \0 \0 \0 Each row has the same length (8 bytes) although only 3 planes have names long enough to fill the entire row This leads to wasted space

Array of Strings The alternative is to make each row to be of different length, just long enough to hold the string The trick is to create an array of pointer to strings char *planets[] = { “Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”, “Pluto”};

Array of Strings Here is how this alternative array of strings looks like in memory planets M e r c u r y \0 V e n u s \0 1 2 E a r t h \0 3 M a r s \0 4 5 J u p i t e r \0 6 S a t u r n \0 7 8 U r a n u s \0 N e p t u n e \0 P l u t o \0 Each string requires just enough space required to store the string: No wasted space

Array of Strings We can print the array of strings as follows char *planets[] = { “Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”, “Pluto”}; for (i=0; i<=8; i++){ printf(“Planet[%d]: <%s>\n”, i, planets[i]); } /* end-for */

Command Line Arguments When we run a program, we will often need to supply it with info from the command line All Unix commands work this way You supply command line arguments to determine the command’s behavior Here are a couple of examples % ls % ls –l % ls –a –l % ls –al /usr/include

Command Line Arguments Command line arguments are available to your programs too They are passed to “main” function as arguments by the operating system Here is how you access the command line arguments in your C programs int main(int argc, char *argv[]){ int i; for (i=0; i<argc; i++) printf(“Argument[%d]: %s\n”, i, argv[i]); } /* end-main */

Command Line Arguments If the name of the executable of your program is “a.out” and you execute it as follows % a.out arg1 xyz arg3 argc will be 4 and argv will be an array of strings pointing to “a.out” “arg1” “xyz” “arg3” argv a . o u t \0 1 a r g 1 \0 2 x y z \0 3 4 NULL a r g 3 \0