0) /* return 1 if name1 > name2 */ strcpy(mixed,name1); else strcpy(mixed,name2); printf("The biggest name alphabetically is %s\n",mixed); strcpy(mixed,name1); strcat(mixed," "); strcat(mixed,name2); printf("Both names are %s\n",mixed); }"> 0) /* return 1 if name1 > name2 */ strcpy(mixed,name1); else strcpy(mixed,name2); printf("The biggest name alphabetically is %s\n",mixed); strcpy(mixed,name1); strcat(mixed," "); strcat(mixed,name2); printf("Both names are %s\n",mixed); }">
Download presentation
Presentation is loading. Please wait.
2
Character Arrays strlen("hello, world"); /* string constant */ strlen(array); /* char array[100]; */ strlen(ptr); /* char *ptr; */ char pmessage[] = "now is the time"; /* an array */ char *amessage = "now is the time"; /* a pointer */
3
main( ) { char name1[12],name2[12],mixed[25]; char title[20]; strcpy(name1,"Rosalinda"); strcpy(name2,"Zeke"); strcpy(title,"This is the title."); printf(" %s\n\n"title); printf("Name 1 is %s\n",name1); printf(Name 2 is %s\n",name2); if (strcmp(name1,name2)>0) /* return 1 if name1 > name2 */ strcpy(mixed,name1); else strcpy(mixed,name2); printf("The biggest name alphabetically is %s\n",mixed); strcpy(mixed,name1); strcat(mixed," "); strcat(mixed,name2); printf("Both names are %s\n",mixed); }
4
strlen /* strlen: return length of string s */ int strlen(char *s) { int n; for (n = 0; s[n] != '\0', n++) ; return n; }
5
strlen /* strlen: return length of string s */ int strlen(char *s) { int n; for (n = 0; *s != '\0', s++) n++; return n; }
6
strcpy /* strcpy: copy t to s; array subscript version */ void strcpy(char *s, char *t) { int i; i = 0; while ((s[i] = t[i]) != '\0') i++; }
7
strcpy /* strcpy: copy t to s; pointer version */ void strcpy(char *s, char *t) { while ((*s = *t) != '\0') { s++; t++; }
8
strcmp /* strcmp: return 0 if s>t */ int strcmp(char *s, char *t) { int i; for (i = 0; s[i] == t[i]; i++) if (s[i] == '\0') return 0; return s[i] − t[i]; }
9
strcmp /* strcmp: return 0 if s>t */ int strcmp(char *s, char *t) { for ( ; *s == *t; s++, t++) if (*s == '\0') return 0; return *s − *t; }
10
strlen /* strlen: return length of string s */ int strlen(char *s) { char *p = s; while (*p != '\0') p++; return p − s; }
11
Array of strings
12
argc, argv main(int argc, char* argv[]) a.out -i 2 -g -x 3 4 argc = 7 argv[0] = "a.out" argv[1] = "-i" argv[2] = "2" argv[3] = "-g" argv[4] = "-x" argv[5] = "3" argv[6] = "4"
13
Bubble SORT for (i=0; i<n-1; i++) { for (j=0; j<n-1-i; j++) if (a[j+1] < a[j]) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; }
14
void shiftArray(int *p, int N, int shift) { int i; /* if shifting right */ if (shift > 0 && shift < N) { for (i = N - 1 - shift; i >= 0; i --) p[i + shift] = p[i]; for (i = shift - 1; i >= 0; i --) p[i] = 0; } /* if shifting left */ else if (shift < 0 && -shift < N) { shift = -shift; for (i = 0; i <= N - 1 - shift; i++) p[i] = p[i + shift]; for (i = N - shift; i < N; i++) p[i] = 0; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.