Upload
caesar-stanton
View
22
Download
1
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
Lecture 09Strings, IDEs.
METU Dept. of Computer Eng. Summer 2002Ceng230 - Section 01 Introduction To C Programmingby Ahmet Sacan
Mon July 29, 2002
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.
Goal Replay..
• Write a function that takes an array as input and returns the length of the string.
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;
}
<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.
• 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.
• 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.
• size_t : an unsigned integral type defined in <stddef.h>
• const char* : string parameters that are not modified by the function.
#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') );
Goal
• write your own strcpy(s1, s2) function.– copies s2 into s1, including NULL-char.– returns s1
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;
}
Goal
• Write your own strcat(s1, s2) function– appends s2 at the end of s1.
char * my_strcat (char *to, char *from) {
strcpy(to + strlen(to), from);
return to;
}
Goal
• Write strstr(s1,s2) function that locates the first occurrence of s2 in s1.
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;
}
String Arrays
char days[7][32] = {
"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday",
"Sunday" };
printf( "%s", days[2] );
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");
}
Goal: int-sort cheat.
• Read student grades (integers: 0-100) into an array, and print in ascending order.
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.
#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);
}
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");
}
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);}
Goal: SelectionSort
• Write a function that sorts an array of floats using selection sort algorithm.