Practice Web Page - http://www.cs.tau.ac.il/~efif/courses/Programming_Fall_04
#include <stdio.h> int * find_sub_array(int * array, int size, int * sub_array, int sub_size); int main() { int array1[] = {1, 45, 67, 1001, -19, 67, 89, 1004, -867, 34, 3, -1900, 10029}; int array2[] = {34, 3, -1900}; int * position; position = find_sub_array(array1, sizeof (array1) / sizeof (int), array2, sizeof (array2) / sizeof(int)); printf("array2 appears in array1 starting from place : %d.\n", (position == NULL)? -1 : position - array1 + 1); return 0; } int * find_sub_array(int * array, int size, int * sub_array, int sub_size) { int i, j; for (i = 0; i++ < size; ++array) { for (j = 0; j < sub_size; ++j) if ( *(array + j) != sub_array[j]) break; if (j == sub_size) return array; } return NULL; } |
char cstring[] = {'H','e','l','l','o','\0'}; |
char cstring[] = "Hello, world!" |
char *pstring = "Hello"; |
#include <stdio.h> #include <stdio.h> int strlen(const char * str) { const char *eos = str; while( *eos++ ) ; return(eos - str - 1); } int main() { char str[]="Hello World"; printf("Length of \"%s\" is %d\n", str, strlen(str)); return 0; } |
int word_cnt (const char * s) { int cnt = 0; const char *next = s + 1; if (*s == '\0') /*empty string*/ return 0; while(*next != '\0') { if (!isspace(*s) && isspace(*next)) cnt++; s++; next++; } if (!isspace(*s)) cnt++; return cnt; } |
size_t strlen(const char *s); |
char *strcat(char *s1, const char *s2); |
int strcmp(const char *s1, const char *s2); |
int strcnmp(const char *s1, const char *s2, size_t n); |
scanf(%s, str); |
gets(str); |
int strcmp(const char *s1, const char *s2) { int k = 0; while(s1[k] == s2[k] && s1[k]) { k++; } return (s1[k] - s2[k]); } |
char *strcpy(char *s1, const char *s2) { char *head = s1; while(*s2) { *s1++ = *s2++; } return head; } |
#include <stdio.h> #define STR_SIZE 256 #define UPPER 'A' - 'a' char * toupper(char * str); int main() { char str[STR_SIZE]; gets(str); printf("%s\n", toupper(str)); return 0; } char * toupper(char * str) { char *ps = str; while(*ps) { if (*ps >= 'a' && *ps <= 'z') *ps += UPPER; ps++; } return str; } |
include |
extern int printf(const char *, ...); |