CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship to pointers 10. Strings Chapter 8
CPT: Strings/102 Overview: 1.What is a String? 2.Accessing Strings with Pointers 3.Word Counting 4.String Conversion Functions 5.I/O of Strings 6.String Manipulation
CPT: Strings/ What is a String? A string is a sequence of charcters which end with the NULL character '\0'. e.g: "hello" " Call" "9"
CPT: Strings/104 Use: char color[] = "blue"; l or equivalently: char color[] = {'b','l','u','e','\0'};
CPT: Strings/105 "a" is not 'a' char example[] = "a"; looks like: A character array for a string must always have 1 extra element for the '\0' character. ‘a’‘\0’
CPT: Strings/ Accessing Strings with Pointers char name[] = "Andrew"; l is equivalent to: char *name = "Andrew"; printf("%s %s\n", name, name+2); Print out: Andrew drew ‘A’‘n’‘d’‘r’‘e’‘w’‘\0’ name
CPT: Strings/ Word Counting #include #include /* for isspace() */ int words_cnt(char *); int main() { char *test = "hello world"; printf("Word count: %d\n", words_cnt(test); return 0; } continued
CPT: Strings/108 int words_cnt(char *s) /* count the number of words */ { int cnt = 0; while (*s != '\0') { while (isspace(*s)) /* skip spaces */ s++; if (*s != '\0') { /* found word */ cnt++; while (!isspace(*s) && *s != '\0') s++; /* skip word */ } } return cnt; }
CPT: Strings/ String Conversion Functions Sec. 8.4 #include #include int main() { double d; d = atof("99.0"); printf("\"99.0\" converted to double is %.3f\n", d); return 0; }
CPT: Strings/1010 Output: "99.0" converted to double is Function prototype in stdlib.h : double atof(const char *nptr);
CPT: Strings/ Input and Output of Strings #include int main() { char word[7]; scanf("%s", word); printf("%s", word); puts(word); return 0; }
CPT: Strings/1012 Reversing (Again)Fig #include #include void reverse(char *); int main() { char sentence[80]; printf("Enter a line of text:\n"); gets(sentence); printf("\nLine backwards is:\n"); reverse(sentence); return 0; } continued
CPT: Strings/1013 void reverse(char *s) /* print s in reverse order */ { int i; for(i=strlen(s)-1; i >= 0; i--) putchar(*(s+i)); /* same as putchar(s[i]); */ }
CPT: Strings/1014 Output Enter a line of text: hello Line backwards is: olleh..... ‘h’‘e’‘l’ ‘o’‘\0’ s
CPT: Strings/ String Manipulation 6.1.Copying a String: strcpy() 6.2.Concatenating Strings: strcat() 6.3.Comparing Strings: strcmp() 6.4.Searching a String: strchr() 6.5.String Length: strlen()
CPT: Strings/ Copying a String: strcpy() Fig #include #include int main() { char x[] = "Happy Birthday to You"; char y[25]; printf("String in x is: %s\n String in y is: %s\n", x, strcpy(y, x)); return 0; }
CPT: Strings/1017 Output: String in x is Happy Birthday to You String in y is Happy Birthday to You Function prototype in string.h : char *strcpy(char *s1, const char *s2);
CPT: Strings/1018 Coding strcpy() /* array version */ char *strcpy(char *s, const char *t) { int i; i = 0; while ((s[i] = t[i]) != '\0') i++; return s; } continued
CPT: Strings/1019 /* pointer version no. 1 */ char *strcpy(char *s, const char *t) { char *temp = s; while ((*s = *t) != '\0') { s++; t++; } return temp; } continued
CPT: Strings/1020 /* pointer version no. 2 */ char *strcpy(char *s, const char *t) { char *temp = s; while ((*s++ = *t++) != '\0') ; return temp; } continued
CPT: Strings/1021 /* pointer version no. 3 */ char *strcpy(char *s, const char *t) { char *temp = s; while (*s++ = *t++) ; return temp; }
CPT: Strings/ Concatenating Strings: strcat() Fig #include #include int main() { char s1[20] = "Happy "; char s2[] = "New Year "; printf("s1 = %s\ns2 = %s\n", s1, s2); printf("strcat(s1, s2) = %s\n", strcat(s1, s2)); printf("s1 = %s\n", s1); return 0; }
CPT: Strings/1023 Output: s1 = Happy s2 = New Year strcat(s1, s2) = Happy New Year s1 = Happy New Year Function prototype in string.h : char *strcat(char *s1, const char *s2);
CPT: Strings/1024 Coding strcat() char *strcat(char *s1, const char *s2) { char *p = s1; while(*p) /* while (*p != '\0') */ p++; while (*p++ = *s2++) ; return s1; }
CPT: Strings/1025 In picture form: ‘H’‘o’‘\0’ ‘b’‘o’‘\0’ p s1 s2
CPT: Strings/ Comparing Strings: strcmp() int strcmp(const char *s1, const char *s2); l Compares the string s1 to the string s2 Returns 0 if s1 > s2
CPT: Strings/1027 Using strcmp() Fig #include #include int main() { char *s1 = "Happy New Year"; char *s2 = "Happy New Year"; char *s3 = "Happy Holidays"; printf("%s%s\n %s%s\n %s%s\n %s%2d\n %s%2d\n %s%2d\n", "s1 = ", s1, "s2 = ", s2, "s3 = ", s3, "strcmp(s1, s2) = ", strcmp(s1, s2), "strcmp(s1, s3) = ", strcmp(s1, s3), "strcmp(s3, s1) = ", strcmp(s3, s1)); return 0; }
CPT: Strings/1028 Output: s1 = Happy New Year s2 = Happy New Year s3 = Happy Holidays strcmp(s1, s2) = 0 strcmp(s1, s3) = 6 strcmp(s3, s1) = -6
CPT: Strings/1029 Coding strcmp() The array version: int strcmp(const char *s, const char *t) { int i; for (i=0; s[i] == t[i]; i++) if (s[i] == '\0') return 0; return s[i] - t[i]; } continued
CPT: Strings/1030 The pointer version: int strcmp(const char *s, const char *t) { for ( ; *s == *t; s++, t++) if (*s == '\0') return 0; return *s - *t; }
CPT: Strings/ Searching a String: strchr() Sec. 8.8 #include #include int main() { char *rest, *sh = "Hello"; char c = 'l'; rest = strchr(sh, c); printf("%s", rest); /* gives llo */ return 0; }
CPT: Strings/1032 Function prototype in string.h : char *strchr(const char *s, int c); ‘h’‘e’‘l’ ‘o’‘\0’ sh rest
CPT: Strings/ String Length: strlen() Fig Function prototype in string.h : size_t strlen(const char *s); size_t is equivalent to the type unsigned int (see string.h ).
CPT: Strings/1034 Coding strlen() size_t strlen(const char *s) { int n; for (n = 0; *s != '\0'; s++) n++; return (size_t) n; }