P.Lecture - 2.26.08 - Lecture 12 Announcements Assignment 3...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 12 Announcements • Assignment 3 • Assignment 4 – Out later today - read it for Thursday • Topics for today – Finish strings – Start File IO (ch. 22) Friday’s Quiz • Write a program named cntword that counts the number of words in a text file (a word is any sequence of non-white-space characters). Obtain the file name from the command line and write its output to stdout . • Chapter 11, Exercise 5 • Chapter 11, Exercise 7 • Chapter 12, Exercise 7 • Chapter 12, Exercise 11 String Reminders • A string is a 1D array - all the standard array algorithms for: indexing, searching, adding, removing, sorting the char [ ] can be used • A string is a special kind of 1D array (partially filled, with the ‘\0’ as the end of string indicator) • There is a library of special string functions, in <stdlib.h> – various input and output functions –s t rcpy – strcat –s t rcmp – strlen – number to/from string conversion functions String Examples • Make a computer password out of the first letters of a person’s first, middle and last names, appended with the person’s age – E.g. Harold Joseph Hacker, 19 years old • Reverse the contents of a string – E.g. “desrever” => “reversed” Create a Password Program int main ( ) { char [ ] firstName = “harold"; char [ ] middleName = “joseph"; char [ ] lastName = “hacker"; char [6] password = “”; int age = 19; /* assume this is the age of the user */ char [3] stringAge = “”; /* to hold age as a string */ /* extract&concatonate the first letter of first, middle,last names*/ strcat (password, firstName [0]); strcat (password, middleName [0]); strcat (password, lastName [0]); sprintf (stringAge, “%d”, age); /*convert age to a string */ strcat (password, stringAge); printf ("Your password is %s ”, password); return 0; } What it does Terminal Window
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 String Reverser /* create a copy of a string in reverse order */ int i =0; char ch; char [80] phrase; /* holds the initial string*/ char [80] reversed; /* holds the reversed string*/ gets (phrase); i = strlen (phrase); reversed = “”; while (i >= 0) { ch = phrase [i]; strcat (reversed, ch); i = i - 1; } reversed [strlen (phrase)] = ‘\0’; printf (“the reversed string is %s\n”, reversed); How would you turn this into a utility function? String Reverser - function void StringReverser (const char [ ] phrase, char [ ] reversed ) { /* precondition: assume that phrase has a legal string value */ /* postcondition: reversed will hold the reverse of phrase */ int i =0; char ch; i = strlen (phrase); reversed = “”; while (i >= 0) { ch = phrase [i]; strcat (reversed, ch); i = i - 1; } reversed [strlen (phrase)] = ‘\0’; return; } Files A file is — 1. A named collection of related
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

P.Lecture - 2.26.08 - Lecture 12 Announcements Assignment 3...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online