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)

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.
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.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
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”
. 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.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Introduction to C Programming CE Lecture 13 Strings in C.
C strings (Reek, Ch. 9) 1CS 3090: Safety Critical Programming in C.
CS 201 String Debzani Deb.
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
Computer Science 210 Computer Organization Strings in C.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
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.
1 بنام خدا زبان برنامه نویسی C (21814( Lecture 13 Chapter 13 Strings.
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.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
File IO and command line input CSE 2451 Rong Shi.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
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.
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.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
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:
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
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.
Arrays and Pointers.
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
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.
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.
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.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
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.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
A bit of C programming Lecture 3 Uli Raich.
Lecture 8 String 1. Concept of strings String and pointers
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Pointers.
EECE.2160 ECE Application Programming
C++ Pointers and Strings
EECE.2160 ECE Application Programming
C++ Pointers and Strings
Presentation transcript:

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) C stores a string as an array of char’ s –The string is terminated with a NULL character –Null character is written ‘\0’ –Is actually represented by zero (the world’s most expensive one-byte mistake!) Example: – “hello” – “”

Strings and Characters Mind the difference between strings & char s “B” ‘B’.

Declaring Strings.

char Arrays vs char Pointers. p

Accessing char s in a string count the number of blanks in string s.

Printing a String.

Reading Strings: scanf using scanf: –skips leading whitespace –reads characters until finds more whitespace –terminates the string (adds ‘\0’ to the end) Example:.

Reading Strings: gets using gets : void gets(char *s); –does not skip whitespace –reads until end of line –terminates the string (adds ‘\0’ to the end) using getchar : char getchar(); –returns the next char from the input –whatever it might be (space, newline, etc)

Example: use getchar to read string.

Printing Strings & Pointer Math char *p = “Sample”; printf(“%s\n”,p);. printf(“%s\n”,p+2);. printf(“%c\n”,*p);. printf(“%c\n”,*(p+2));. printf(“%c\n”,*p+2);.

String Library Functions

String Library and strlen #include unsigned strlen(const char *p); –returns the length of the string pointed to by p –i.e., the number of characters up to ‘\0’ Examples:.

Implementing strlen.

Implementing strlen with only ptrs. AddrValue Memory p q

Copying a String #include char *strcpy(char *dst,const char *src); –src means source –dst means destination –copies the string (characters) from src to dst stops copying when it finds ‘\0’ in src –returns dst (pointer to the destination string) return value is usually ignored/discarded –warning: ensure that dst string is big enough! ensure strlen(dst) >= strlen(src) or else big trouble!

Implementing strcpy.

.

Concatenating Strings Concatenate means glue together char *strcat(char *dst,const char *src); –writes src onto the end of dst –the ‘\0’ in dst is overwritten –the return value from strcat is usually discarded Examples:.

Comparing Strings int strcmp(const char *p,const char *q); –compares p to q –returns: zero if p and q are identical a negative value if p is lexicographically before q a positive value if q is lexicographically before p lexicographically before: –“a” is before “b”, “A” is before “A”, “ “ is before all letters –“the” is before “them” –Example:.

Other Usful String Functions char *strchr(const char *s, int c) –returns ptr to first appearance of c in s –returns NULL if not found –NOTE: “int c” (C will cast for you) char *strstr(const char *s1, const char *s2) –returns ptr to first appearance of s2 in s1 –returns NULL if not found

Other Usful String Functions #include int atoi(const char *s) –converts leading characters of s into an integer –behaviour is undefined if no valid characters double atof(const char *s) –converts leading characters of s into a double –behaviour is undefined if no valid characters

Arrays of Strings

2D Array of Characters Example: create an array of the months.

Array of Strings Example: create an array of the months

Array of Strings: alternative Example: create an array of the months.

Command Line Arguments

Main is a function; can pass it arguments –the arguments are entered on the command line –main can hence have one of these two formats: int main(void) int main(int argc, char *argv[]) argc: argument count –how many arguments are there –includes the executable name in the count! argv: argument vector –an array of argument strings –Note: can only be strings! –char *argv[] means argv is an array of pointers –could also write it as a double-pointer: char **argv

Example given an executable called myprogram: myprog input true 25 then argc and argv are pre-set as follows:. argv

Ensuring Right Number of Args ensure that myprogram has right num of args: myprog input true 25.

Give a Program that Prints its args.