Lecture 09 Strings, IDEs. - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Lecture 09 Strings, IDEs.

Description:

Lecture 09. Strings, IDEs. METU Dept. of Computer Eng. Summer 2002. Ceng230 - Section 01 ... Write a function that takes an array as input and returns the ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 25
Provided by: ahm4
Category:
Tags: ides | lecture | strings

less

Transcript and Presenter's Notes

Title: Lecture 09 Strings, IDEs.


1
Lecture 09Strings, IDEs.
Mon July 29, 2002
  • METU Dept. of Computer Eng. Summer 2002
  • Ceng230 - Section 01
  • Introduction To C Programming
  • by Ahmet Sacan

2
C String
  • is a sequence of one or more characters
    terminated by a NULL ( '\0' )character.
  • The NULL character is crucial in determining the
    end of the string.

3
Goal Replay..
  • Write a function that takes an array as input and
    returns the length of the string.

4
  • int strlen(char str )
  • int i0
  • while (stri ! '\0') i
  • return i

int strlen(char str) int i0 while (stri
! '\0') i return i
int strlen(char str) char p
str while(p) p return p - str
int strlen(char str) int i0 while
(stri) return i - 1
5
ltstring.hgt
  • size_t strlen(const char s)
  • return length of string ( of chars preceding
    '\0')
  • char strcpy (char dest, const char src)
  • copies the string src into dest (including
    NULL-char) returns dest.
  • char strcat (char dest, const char src)
  • concatenates src to the end of dest, adds NULL at
    the end of dest returns dest.
  • int strcmp (const char s1, const char s2)
  • compares strings s1 and s2, returns a negative
    value if s1 is lexicographically less than s2
    zero if s1 is equal to s2 a positive number if
    s1 is greater than s2.

6
  • char strchr(const char s, char c)
  • returns a pointer to the first occurrence of c in
    the string s. (NULL if not found)
  • char strrchr(const char s, char c)
  • returns a pointer to the last occurrence of c in
    the string s. (NULL if not found)
  • char strstr(const char s1, const char s2)
  • returns a pointer to the first occurrence of s2
    in string s1.

7
  • char strncpy (char dest, const char src,
  • size_t n)
  • copies at most the first n characters of src into
    dest.
  • char strncat (char dest, const char src,
  • size_t n)
  • concatenates at most n characters of src to the
    end of dest.
  • int strncmp (const char s1, const char s2)
  • compares at most max(n, s1.length, s2.length)
    characters.

8
  • size_t an unsigned integral type defined in
    ltstddef.hgt
  • const char string parameters that are not
    modified by the function.

9
  • define MAX 32
  • char xMAX, yMAX
  • printf("d", strlen("hello.")
  • printf("s", strcpy(x, "green"))
  • printf("s", strcpy(y, strrchr("blue", 'u') ) )
  • x2 '\0'
  • printf("s", strcat(x, y) )
  • printf("s", strcpy(x, y) )
  • printf("d", strcmp(x,y) )
  • printf("s", strchr("blackwhite", 'w') )

10
Goal
  • write your own strcpy(s1, s2) function.
  • copies s2 into s1, including NULL-char.
  • returns s1

11
  • char my_strcpy(char dest, char src)
  • char todest, fromsrc
  • while(to from)
  • to, from
  • return dest

char my_strcpy(char dest, char src) char
todest, fromsrc while(to from)
return dest
12
Goal
  • Write your own strcat(s1, s2) function
  • appends s2 at the end of s1.

13
  • char my_strcat (char to, char from)
  • strcpy(to strlen(to), from)
  • return to

14
Goal
  • Write strstr(s1,s2) function that locates the
    first occurrence of s2 in s1.

15
  • char my_strstr(char str, char find)
  • char p
  • for(p str p strchr(p, find) p)
  • if(!strncmp (p, find, strlen(find))
  • return p
  • return NULL

16
String Arrays
  • char days732
  • "Monday", "Tuesday", "Wednesday",
  • "Thursday", "Friday", "Saturday",
  • "Sunday"
  • printf( "s", days2 )

17
  • void main()
  • char str "flower", p
  • int i
  • for(p str5 p gt str0 )
  • printf("c", p--)
  • printf("\n")
  • for(p str5, i0 p-i gt str )
  • printf("c", (--p - i) )
  • printf("\n")
  • for(p str5, i0 ilt5 i)
  • printf("c", p-i)
  • printf("\n")
  • for(p str5 p gt str p--)
  • printf("c", strp-str )
  • printf("\n")

18
Goal int-sort cheat.
  • Read student grades (integers 0-100) into an
    array, and print in ascending order.

19
Goal buble-sort.
  • Write a program that reads a list of floats,
    stores them in an array, sorts the numbers in
    ascending order, and prints the ordered list.

20
  • include ltstdio.hgt
  • define MAX_SIZE 1000
  • void ReadList(float a , int size)
  • void PrintList(float a , int size)
  • void BubbleSort(float a , int size)
  • void main( )
  • int arrMAX_SIZE, size
  • ReadList(arr, size)
  • PrintList(arr, size)
  • BubbleSort(arr, size)
  • PrintList(arr, size)

21
  • void ReadList(float a , int size)
  • int i0
  • for( iltsize i)
  • scanf("f", ai )
  • void PrintList(float a , int size)
  • int i0
  • for( iltsize i)
  • printf("f, ", ai)
  • printf("\n")

22
  • void BubbleSort(float a , int size)
  • int i, unsorted
  • float f
  • do
  • unsorted 0
  • for(i0 iltsize-1 i)
  • if(ai gt ai1)
  • f ai ai ai1 ai1
    f
  • unsorted
  • size--
  • while(unsorted)

23
Goal SelectionSort
  • Write a function that sorts an array of floats
    using selection sort algorithm.

24
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com