Strings. Steven R. Bagley

Similar documents
C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

Chapter 8: Character & String. In this chapter, you ll learn about;

LECTURE 15. String I/O and cstring library

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to string

C Characters and Strings

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Object Oriented Programming COP3330 / CGS5409

COMP1917: 09 Arrays and Strings

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

Arrays, Strings, & Pointers

Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

8. Characters, Strings and Files

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. A Taste of C. Agenda.

Standard C Library Functions

Goals of this Lecture

Array Initialization

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Chapter 8 - Characters and Strings

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

C Examples. Goals of this Lecture. Overview of this Lecture

Characters, Character Strings, and string-manipulation functions in C

Characters and Strings

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Princeton University Computer Science 217: Introduction to Programming Systems. A Taste of C

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

CS107 Spring 2019, Lecture 4 C Strings

File Handling in C. EECS 2031 Fall October 27, 2014

Procedures, Parameters, Values and Variables. Steven R. Bagley

C: How to Program. Week /May/28

CS107, Lecture 4 C Strings

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

C PROGRAMMING. Characters and Strings File Processing Exercise

Chapter 8 C Characters and Strings

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

HW1 due Monday by 9:30am Assignment online, submission details to come

Chapter 9 Strings. With this array declaration: char s[10];

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

Character Arrays. strlen("hello, world"); /* string constant */ strlen(array); /* char array[100]; */ strlen(ptr); /* char *ptr; */

C mini reference. 5 Binary numbers 12

LESSON 4. The DATA TYPE char

Pointers and scanf() Steven R. Bagley

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Fundamentals of Programming. Lecture 11: C Characters and Strings

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

Scientific Programming in C V. Strings

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

Model Viva Questions for Programming in C lab

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Introduction to C/C++ Lecture 5 - String & its Manipulation

C-programming. Goal To give basic knowledge of the C language. Some previous experiences in programming are assumed. Litterature

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

C programming language continued: Recursion, strings and pointer

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

EE 355 Lab 4 - Party Like A Char Star

Strings and Library Functions

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

Computer Programming: Skills & Concepts (CP) Strings

Programming in C. Pointers and Arrays

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CS 137 Part 6. ASCII, Characters, Strings and Unicode. November 3rd, 2017

today cs3157-fall2002-sklar-lect05 1

C programming basics T3-1 -

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

Lecture 17. Strings II. CptS 121 Summer 2016 Armen Abnousi

CS61C : Machine Structures

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

File IO and command line input CSE 2451

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

CS 103 Lab 6 - Party Like A Char Star

Tutorial 1 C Tutorial: Pointers, Strings, Exec

211: Computer Architecture Summer 2016

Lectures 5-6: Introduction to C

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

In the case of the dynamic array, the space must be deallocated when the program is finished with it. free(x);

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

CS 103 Lab - Party Like A Char Star

Strings. Daily Puzzle

Fundamentals of Programming & Procedural Programming

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

Heap Arrays. Steven R. Bagley

Computers Programming Course 11. Iulian Năstac

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

csc/cpe 357 C Quiz Archive

CS11 Introduction to C++ Spring Lecture 8

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

Homework 2 Answers. Due Date: Monday, April 29, 2002, at 11:59PM Points: 100

Transcription:

Strings Steven R. Bagley

Recap Programs are a series of statements Defined in functions Functions, loops and conditionals can alter program flow Data stored in variables or arrays Or pointed at by pointers

Strings C Strings are sequence of chars terminated by a null char \0 Accessed by a pointer to the first character Create space for a new string by using a char array Access the characters either with pointers or as an array There are other ways -- we ll see that later

Defining Strings Two ways of defining a string in C As a pointer to the string char *str = Hello World ; As an array initialised with the string char str[] = Hello World ; These are different can modify the contents of the latter, but not the former First defines a pointer to a string in the program code unchangeable Go demo the difference

String Processing Most string routines will iterate over every character in the string Using a loop And stop when they hit the \0 character Classic example would be a string length routine

Strings as Arrays Need a string and an integer to hold the offset char *string = Hello World ; int i=0; Loop conditional while(string[i]!= \0 ) Don t forget to increment i i++; or i = i + 1; Go implement strlen

Strings using Pointers Need a string char *string = Hello World ; Also a pointer to a character char *p = string; Loop conditional while(*p!= \0 ) Advance pointer to next character p = p + 1; or p++; Set pointer to point to the first character

Speed Daemon Both methods will have the same effect But the array version has to do twice as much work Array access has to take the pointer to the base of the array and add the offset As well as incrementing i In the pointer version, we just move the pointer on by one And in most cases, the CPU can automatically increment the pointer for us

Caesar Cipher Write an encryption program that uses the Caesar Cipher Caesar Cipher rotates each letter along the alphabet by a certain number So using the classic rotate 13, A becomes N, B becomes O etc. Wraps around so M is Z, N is A

Program Logic Visit every character in the string If it is a letter add 13 to its ASCII value If greater than Z wrap it back to A Store value back in string at same position Print it out Can use modular arithmetic to wrap things Go program it up

Reading a string Could use scanf() to read text But this only reads one word There s another routine gets() which returns a complete line

gets char *gets(char *s) Takes a pointer to a string to read the line into Must be enough space to store the string; gets() won t check, that s your job Returns a pointer to the buffer, or NULL if an end of file occurs Newline character is not returned

gets However, there s a problem with gets() It doesn t check how large the buffer is If the user types in too many characters it will overflow the buffer Overwriting valid memory and causing the program to crash

Getting a line char *fgets(char *s, int n, FILE *stream) Takes a pointer to a string to read the line into Reads at most n characters Need to tell it what stream to read from, we use stdin Will return the newline character if it gets to the end of the line...

NULL pointers The NULL pointer is used to say this doesn t point at anything If you try and access it, you ll almost certainly crash the machine But you can always test for it first To speed things up, NULL is defined to be 0 We ll see this in more detail later

Case-sensitivity Need to handle upper case and lower case separately Currently handle it manually Can use isupper() and islower() instead Then adjust accordingly But we can optimise our routine

Changing the rotation Currently, we have a fixed rotation of 13 Be nice if we could specify this One way to do this would be to allow us to put the rotation value on the command line So we pass it to the program as we call it

Command Line C passes the command line argument to main as an array of strings Generally, called argv Also passes a count of how many arguments there are, generally called argc First argument is always the name of the program argv - argument vector

Command Line Can read the rotation value from here Provide a default of 13 if not specified (check how many arguments passed using argc) But need to convert string to an int Can use scanf, well, a special version for strings, sscanf

sscanf() Works identically to the normal scanf Reads characters from a string rather than the stdin First parameter is the string to read the characters from int sscanf(char *s, char *format, );

Character Type C library provides a variety of routines for testing characters All of the form int is (int c) Returns true (non-zero) or false (zero) depending whether the character is or not Must #include <ctype.h>

Function Tests isascii(int c) Between 0 and 127 isalpha(int c) is it an alphabetic character? isdigit(int c) is it a digit? 0-9 isnumber(int c) isalnum(int c) ishexnumber(int c) Any number character (depends on location) Is it a digit or alphabetic chracter? Is it a hex digit 0-9A-F Some of the more common examples

Function islower(int c) isupper(int c) isspace(int c) isprint(int c) int tolower(int c) int toupper(int c) Is it lower case? is it upper case? Tests is it a space character? Including tabs, newlines etc. is the character printable? Converts character to lowercase if uppercase Converts character to uppercase if lowercase Some of the more common examples

Optimized Implementation Often, these routines are written in an optimized fashion Don t use repeated comparisons An array of longs one for each character Each bit of long means something if set Bit 8 is alphabetic character, Bit 12 is lower case, etc

Optimized Implementation Bit 8 is alphabetic character Bit 10 is digit character Bit 12 is lower case Bit 15 is upper case and so on for other bits So character A would have bit 8, and bit 15 set (at least)

Optimized Implementation Can test whether a bit is set using a bitwise-and (the & operator in C) So runetype[ A +1] & 0x00008000L would test if it is upper-case If the bit is set this will return, 0x8000L a non-zero value, so true If not, returns zero so false +1 is so you can cope with EOF which is -1 Try being that clever in Java...

Lookup Table This is called a Lookup Table Rather than calculating the result, it looks it up in the array Very common practice Sometimes used to get approximations for complex calculations Trading memory space for time (e.g. sine/cosine in games, colour transformations etc)

Common String Routines Some string routines are used a lot String Length routine Copy a string Concatenate one string onto another Find the first occurrence of a character in a string See implementation of these

Pointers into Strings Often use pointers to access the individual characters in a string This works very effectively Can also use it to chop strings in half by manipulating the pointer that is passed to string routines Or the position of the null terminator

Printing a String Consider a function that prints a string Take a pointer to the first character Use the * operator to fetch the character If \0 then end Use putchar() to output each character And loop Go write it...

void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

string H e l l o W o r l d \0 void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

p string H e l l o W o r l d \0 void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

p string H e l l o W o r l d \0 void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

p string H e l l o W o r l d \0 void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

p string H e l l o W o r l d \0 void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

p string H e l l o W o r l d \0 void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

p string H e l l o W o r l d \0 void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

Moving Pointers Routine prints from the first character Since it is given a pointer to the first character But if we give it a pointer to the sixth character (string + 6) It will start printing from the sixth character

p string H e l l o W o r l d \0 void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

p string H e l l o W o r l d \0 void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

p string H e l l o W o r l d \0 void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

p string H e l l o W o r l d \0 void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

p string H e l l o W o r l d \0 void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

p string H e l l o W o r l d \0 void putstring(char *string) { char *p = string; } while(*p!= \0 ) { putchar(*p++); }

Common String Routines Some string routines are used a lot String Length routine Copy a string Concatenate one string onto another Find the first occurrence of a character in a string See implementation of these

String Length Already saw this last lecture Step over every character Adding one to a counter When we hit the null character, we stop Counter contains the number of characters Again though we only count the number of characters from the pointer passed to the routine

String Copy Easy to do One pointer points at the source string Other points at the destination Copy value from source pointer to destination pointer Till we reach null character

string H e l l o W o r l d \0

string H e l l o W o r l d \0 string

p string H e l l o W o r l d \0 string

p string H e l l o W o r l d \0 string q

p string H e l l o W o r l d \0 string H q

p string H e l l o W o r l d \0 string H q

p string H e l l o W o r l d \0 string H e q

p string H e l l o W o r l d \0 string H e q

p string H e l l o W o r l d \0 string H e l q

p string H e l l o W o r l d \0 string H e l l q

p string H e l l o W o r l d \0 string H e l l o q

p string H e l l o W o r l d \0 string H e l l o W o r l d \0 q

String Copy Again, can copy from anywhere in the string Or to anywhere in the destination string If we manipulate the pointers If we pointer to the null terminator, we can concatenate two strings together

string W o r l d \0

string W o r l d \0 string H e l l o \0??????

p string W o r l d \0 string H e l l o \0??????

p string W o r l d \0 string H e l l o \0?????? q

p string W o r l d \0 string H e l l o?????? q

p string W o r l d \0 string H e l l o?????? q

p string W o r l d \0 string H e l l o W????? q

p string W o r l d \0 string H e l l o W o???? q

p string W o r l d \0 string H e l l o W o???? q

p string W o r l d \0 string H e l l o W o r??? q

p string W o r l d \0 string H e l l o W o r l?? q

p string W o r l d \0 string H e l l o W o r l d? q

p string W o r l d \0 string H e l l o W o r l d \0 q

String Concatenation How do we find the null? Could use strlen() and add it onto the pointer But we could just advance the pointer in our function while(*p!= 0) p++;

Function Effect strlen(char *s) Returns the length of string s. strcpy(char *s1, char *s2) strcat(char *s1, char *s2) strcmp(char *s1, char *s2) stpcpy(char *s1, char *s2) sprintf(char *s, char *format, ) Copy the string s2 into s1. Returns s1. Concatenates s2 onto s1. Doesn t check for enough room. Compares s1 to s2. As strcpy, but returns pointer to null character As printf, but output written into string s Standard C library routines you can use... Don t let the strings overlap... stpcpy isn t ANSI standard iirc

strcmp Comapres two strings int strcmp(char *s1, char *s2); Returns 0 if the two strings are equal Returns <0 if s1 comes before s2 lexicographically Returns >0 if s1 comes after s2 lexicographically