CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.

Slides:



Advertisements
Similar presentations
Learn all about the year.
Advertisements

 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.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Chubaka Producciones Presenta :.
The Months and The Seasons Prepared by Claudia Doria and Terra Myers.
2012 CALENDAR. JANUARY 2012 Sunday 日 Monday 月 Tuesday 火 Wednesday 水 Thursday 木 Friday 金 Saturday 土
Chapter 9 Character Strings
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
January 2012 Monday Tuesday Wednesday Thursday Friday Sat/ Sun / /8 14/15 21/22 28/
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSATURDAYSUNDAY WEEK WEEK WEEK WEEK WEEK CALENDAR PROJECT.
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.
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Introduction to C programming
What day is today? What`s the date?. Sunday Monday Tuesday Wednesday Thursday Friday Saturday What day is today?
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
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.
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:
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
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. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Pointers and Tools
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
The Calendar.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
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.
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.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
INC 161 , CPE 100 Computer Programming
Fundamentals of Characters and Strings
Strings A string is a sequence of characters treated as a group
Arrays in C.
Engr 0012 (04-1) LecNotes
INC 161 , CPE 100 Computer Programming
EECE.2160 ECE Application Programming
Lec 11.
Teacher name August phone: Enter text here.
C++ Programming Lecture 20 Strings
2015 January February March April May June July August September
Introduction to Problem Solving and Programming
Presentation transcript:

CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004

Introduction to Strings strings are how we store and manipulate text in C strings are basically arrays of char strings are not a data type in C but C does have some library functions for manipulating strings you can declare an array of chars like this: char carray[27]; just like any array, we can initialise an array of chars: char carray[27] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’};

Initialisation - Array Notation We have already seen how to initialise arrays. Let us re-examine this in more detail. Rules of array initialisation we can have as many initialisers as we have array elements we can have fewer, but not more if we have fewer, the remaining elements get set to 0 So for example double values[4] = {1.1, 2.2}; will initialise an array of four doubles with the values 1.1, 2.2, 0.0 and 0.0

String Literals C has a convention about how strings are stored. An extra character is added to the end of the string - the null character – ‘\0’. “Hello” is stored as ‘H’,’e’,’l’,’l’,’o’, and ’\0’ String literals, sometimes called string constants, are a sequence of zero or more characters enclosed in double quotes. – like this: “xyz” or “Hello, world!\n” we’ve already used these a lot printf(“Hello, world!\n”);

Pointer Notation C gives us two ways to declare strings: char astring[] = “Hello”; or char *pstring = “Hello”; The second version is the most common. A pointer to a char is declared and intiialised with a string literal. This can subsequently be processed as an array or pointer, e.g. to access the second element ‘e’ you can use pstring[1] or *++pstring.

Array of Strings We can have an array of strings: char *dayName[] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}; this creates an array of 7 elements of type char* each element is initialized with a pointer to a string literal here’s an example printf (“%s\n”, dayName[0]); which displays Sunday

The strlen() function Very often you need to find the length of a string. There is a function strlen() declared in string.h that can do this: #define MAXSTRING 80 char string[MAXSTRING]; int length; printf(“Enter a string: “); scanf(“%s”, string); length = strlen(string); printf(“You entered %d characters.\n”, length);

String functions (1) Calculate string length, strlen(s1) Counts characters before the ‘\0’ Concatenate two strings, strcat(s1, s2) Result stored in s1 Copy a string, strcpy(s1, s2) Result stored in s1 Concatenate two strings (char limit), strncat(s1, s2, x) Concatenates at most x characters - result stored in s1 Copy a string (char limit), strncpy(s1, string, x) Copy at most x characters - result stored in s1

String functions (2) Compare two strings, strcmp(s1, s2) Returns number less than 0 if s1 s1, returns 0 if s1 is equal to s2 Compare two strings (max char), strncmp(s1, s2, x) Compares first x characters, otherwise as strcmp() Search for a character in a string, strchr(s1, c) Searches for the first occurrence of c in s1. Starts on the left. If found, returns address. If not found, returns NULL Search for a string within a string, strstr(s1, s2) Searches for the first occurrence of s2 in s1 Starts on the left. If found, returns address. If not found, returns NULL

Example: Array of Strings int FindMonthNum (char *monthName){ char *month[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”}; for (i = 0; i < 12; ++i) if (strcmp (monthName, month[i]) == 0) return i+1; return 0; }

The null string The null string is legal C char *c = ""; And it's also quite useful, e.g. in a program dealing with plurals: char *plural_ending; int num_frogs;... plural_ending = (num_frogs==1)? "": "s"; printf("We found %d frog%s in the pond", num_frogs, plural_ending);