puts() Implementation without the newline: - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

puts() Implementation without the newline:

Description:

Title: PowerPoint Presentation Author: B Smith Last modified by: B Smith Created Date: 10/16/2004 5:45:19 PM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 14
Provided by: BSm5
Category:

less

Transcript and Presenter's Notes

Title: puts() Implementation without the newline:


1
(No Transcript)
2
puts() Implementation without the newline
  • puts eg.c - puts study-no newline
  • 30
  • 31
  • 32 / puts() implemented without the \n /
  • 33 int puts2( char strng )
  • 34
  • 35 int ch
  • 36 while( ch strng )
  • 37 putchar( ch )
  • 38 return / Note that the putchar('\n') has
    been dropped /
  • 39

3
puts() Implementation without the newline
  • puts eg.c - puts study - main routine
  • 1 include ltstdio.hgt
  • 2 int main()
  • 3
  • 4 char seasons1 "Winter"
  • 5 char seasons2 "Spring"
  • 6
  • 7 putchar('\n')
  • 8
  • 9 printf("Season1 written with puts1 is")
  • 10 puts1(seasons1)
  • 11 printf("Season2 written with puts1 is")
  • 12 puts1(seasons2)
  • 13
  • 14 printf("Season1 written with puts2 is")
  • 15 puts2(seasons1) / no '\n' sent to stdout /
  • 16 printf("Season2 written with puts2 is")
  • 17 puts2(seasons2)
  • 18

4
puts() Implementations
  • puts eg.c - puts study - main routine
  • 1
  • 2 Season1 written with puts1 isWinter
  • 3 Season2 written with puts1 isSpring
  • 4 Season1 written with puts2 isWinterSeason2
    written with puts2 isSpring

5
Copying A Longer String To A Shorter String
  • mystrcpy.c - strcopy explorations
  • 1 include ltstdio.hgt
  • 2
  • 3 int main()
  • 4
  • 5 char seasons1 "Winter"
  • 6 char seasons2 "A Very Long Spring"
  • 7
  • 8 puts("Here is seasons1")
  • 9 puts(seasons1)
  • 10 puts("Here is seasons2")
  • 11 puts(seasons2)
  • 12
  • 13 strcpy1(seasons1, seasons2)
  • 14
  • 15 puts("Here again is seasons1")
  • 16 puts(seasons1)
  • 17
  • 18 puts("Now, here is seasons2") / seasons2
    overwritten? /

6
Zooming In
  • mystrcpy.c - strcopy explorations-lines 1-13
  • 1 include ltstdio.hgt
  • 2
  • 3 int main()
  • 4
  • 5 char seasons1 "Winter"
  • 6 char seasons2 "A Very Long Spring"
  • 7
  • 8 puts("Here is seasons1")
  • 9 puts(seasons1)
  • 10 puts("Here is seasons2")
  • 11 puts(seasons2)
  • 12
  • 13 strcpy1(seasons1, seasons2)

7
Zooming In
  • mystrcpy.c - strcopy explorations-lines 14-22
  • 14
  • 15 puts("Here again is seasons1")
  • 16 puts(seasons1)
  • 17
  • 18 puts("Now, here is seasons2") / seasons2
    overwritten? /
  • 19 puts(seasons2)
  • 20
  • 21 return 0
  • 22

8
What Happened To seasons2?
  • mystrcpy.log - strcopy output
  • 1 Here is seasons1
  • 2 Winter
  • 3 Here is seasons2
  • 4 A Very Long Spring
  • 5 Here again is seasons1
  • 6 A Very Long Spring
  • 7 Now, here is seasons2
  • 8 Long Spring

9
Ex 9.2 1
  • Determine the value of text, (text 3)), and
  • (text 10), assuming that text is an array of
    characters and the
  • following has been stored in the array
  • a. now is the time
  • b. rocky raccoon welcomes you
  • c. Happy Holidays
  • d. The good ship

10
9.2 2a
  • The following function, convert(), \marches
    along"
  • the string passed to it and sends each character
    in the string one at
  • a time to the toUpper() function until the Null
    character is
  • encountered.

11
Convert to Uppercase - array version
  • 1 char toUpper(char) /function proto /
  • 2
  • 3 void convert(char strng) / convert a string
    to uppercase letters/
  • 4
  • 5 int i 0
  • 6
  • 7 while (strngi ! '\0')
  • 8
  • 9 strngi toUpper(strngi)
  • 10 i
  • 11
  • 12 return
  • 13

12
Convert to Uppercase - called by convert()
  • 1
  • 2 char toUpper(char letter) / convert to uc /
  • 3
  • 4 if( letter gt 'a' letter lt 'z')
  • 5 return(letter - 'a' 'A')
  • 6 else
  • 7 return (letter)
  • 8

13
9.2 2a - soln
  • Convert to Uppercase - ptr version
  • char toUpper(char) /function proto /
  • void convert(char strng)
  • while (strng ! '\0')
  • strng toUpper(strng)
  • return
Write a Comment
User Comments (0)
About PowerShow.com