24
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 09 Strings, IDEs

Embed Size (px)

DESCRIPTION

Mon July 29, 2002. Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan. C String. is a sequence of one or more characters terminated by a NULL ( '\0' )character. - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 09 Strings, IDEs

Lecture 09Strings, IDEs.

METU Dept. of Computer Eng. Summer 2002Ceng230 - Section 01 Introduction To C Programmingby Ahmet Sacan

Mon July 29, 2002

Page 2: Lecture 09 Strings, IDEs

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.

Page 3: Lecture 09 Strings, IDEs

Goal Replay..

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

Page 4: Lecture 09 Strings, IDEs

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;

}

Page 5: Lecture 09 Strings, IDEs

<string.h>

• 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.

Page 6: Lecture 09 Strings, IDEs

• 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.

Page 7: Lecture 09 Strings, IDEs

• 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.

Page 8: Lecture 09 Strings, IDEs

• size_t : an unsigned integral type defined in <stddef.h>

• const char* : string parameters that are not modified by the function.

Page 9: Lecture 09 Strings, IDEs

#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') );

Page 10: Lecture 09 Strings, IDEs

Goal

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

Page 11: Lecture 09 Strings, IDEs

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;

}

Page 12: Lecture 09 Strings, IDEs

Goal

• Write your own strcat(s1, s2) function– appends s2 at the end of s1.

Page 13: Lecture 09 Strings, IDEs

char * my_strcat (char *to, char *from) {

strcpy(to + strlen(to), from);

return to;

}

Page 14: Lecture 09 Strings, IDEs

Goal

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

Page 15: Lecture 09 Strings, IDEs

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;

}

Page 16: Lecture 09 Strings, IDEs

String Arrays

char days[7][32] = {

"Monday", "Tuesday", "Wednesday",

"Thursday", "Friday", "Saturday",

"Sunday" };

printf( "%s", days[2] );

Page 17: Lecture 09 Strings, IDEs

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");

}

Page 18: Lecture 09 Strings, IDEs

Goal: int-sort cheat.

• Read student grades (integers: 0-100) into an array, and print in ascending order.

Page 19: Lecture 09 Strings, IDEs

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.

Page 20: Lecture 09 Strings, IDEs

#include <stdio.h>#define MAX_SIZE 1000void 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);

}

Page 21: Lecture 09 Strings, IDEs

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");

}

Page 22: Lecture 09 Strings, IDEs

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);}

Page 23: Lecture 09 Strings, IDEs

Goal: SelectionSort

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

Page 24: Lecture 09 Strings, IDEs