Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

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 i=0; while (str[i] != '\0') i++; return i; } int strlen(char * str){ int i=0; while (str[i] != '\0') i++; return i; } int strlen(char * str){ char * p = str; while(*p) p++; return p - str; } int strlen(char * str){ int i=0; while (str[i++]) ; return i - 1; }

5 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 const char* : string parameters that are not modified by the function.

9 #define MAX 32 char x[MAX], y[MAX]; printf("%d", strlen("hello."); printf("%s", strcpy(x, "green")); printf("%s", strcpy(y, strrchr("blue", 'u') ) ); x[2] = '\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 *to=dest, *from=src; while(*to = *from) to++, from++; return dest; } char * my_strcpy(char *dest, char *src) { char *to=dest, *from=src; 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 days[7][32] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; printf( "%s", days[2] );

17 void main(){ char str[ ] = "flower", *p; int i; for(p = &str[5]; p >= &str[0]; ) printf("%c", *p--); printf("\n"); for(p = str+5, i=0; p-i >= str; ) printf("%c", *(--p - i++) ); printf("\n"); for(p = &str[5], i=0; i<=5; i++) printf("%c", p[-i]); printf("\n"); for(p = str+5; p >= str; p--) printf("%c", str[p-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 #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 arr[MAX_SIZE], size; ReadList(arr, size); PrintList(arr, size); BubbleSort(arr, size); PrintList(arr, size); }

21 void ReadList(float a[ ], int size) { int i=0; for( ; i<size; i++) scanf("%f", &a[i] ); } void PrintList(float a[ ], int size) { int i=0; for( ; i<size; i++) printf("%f, ", a[i]); printf("\n"); }

22 void BubbleSort(float a[ ], int size) { int i, unsorted; float f; do { unsorted = 0; for(i=0; i<size-1; i++) if(a[i] > a[i+1]) { f = a[i]; a[i] = a[i+1]; a[i+1] = f; unsorted++; } size--; } while(unsorted); }

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

24


Download ppt "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."

Similar presentations


Ads by Google