Presentation is loading. Please wait.

Presentation is loading. Please wait.

240-222 CPT: Strings/101 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.

Similar presentations


Presentation on theme: "240-222 CPT: Strings/101 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship."— Presentation transcript:

1 240-222 CPT: Strings/101 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship to pointers 10. Strings Chapter 8

2 240-222 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

3 240-222 CPT: Strings/103 1. What is a String? A string is a sequence of charcters which end with the NULL character '\0'. e.g: "hello" "235-4567 Call" "9"

4 240-222 CPT: Strings/104 Use: char color[] = "blue"; l or equivalently: char color[] = {'b','l','u','e','\0'};

5 240-222 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’

6 240-222 CPT: Strings/106 2. 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

7 240-222 CPT: Strings/107 3. 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

8 240-222 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; }

9 240-222 CPT: Strings/109 4. 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; }

10 240-222 CPT: Strings/1010 Output: "99.0" converted to double is 99.000 Function prototype in stdlib.h : double atof(const char *nptr);

11 240-222 CPT: Strings/1011 5. Input and Output of Strings #include int main() { char word[7]; scanf("%s", word); printf("%s", word); puts(word); return 0; }

12 240-222 CPT: Strings/1012 Reversing (Again)Fig. 8.13 #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

13 240-222 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]); */ }

14 240-222 CPT: Strings/1014 Output Enter a line of text: hello Line backwards is: olleh..... ‘h’‘e’‘l’ ‘o’‘\0’ s

15 240-222 CPT: Strings/1015 6. 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()

16 240-222 CPT: Strings/1016 6.1. Copying a String: strcpy() Fig. 8.18 #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; }

17 240-222 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);

18 240-222 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

19 240-222 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

20 240-222 CPT: Strings/1020 /* pointer version no. 2 */ char *strcpy(char *s, const char *t) { char *temp = s; while ((*s++ = *t++) != '\0') ; return temp; } continued

21 240-222 CPT: Strings/1021 /* pointer version no. 3 */ char *strcpy(char *s, const char *t) { char *temp = s; while (*s++ = *t++) ; return temp; }

22 240-222 CPT: Strings/1022 6.2. Concatenating Strings: strcat() Fig. 8.19 #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; }

23 240-222 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);

24 240-222 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; }

25 240-222 CPT: Strings/1025 In picture form: ‘H’‘o’‘\0’ ‘b’‘o’‘\0’ p s1 s2

26 240-222 CPT: Strings/1026 6.3. 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

27 240-222 CPT: Strings/1027 Using strcmp() Fig. 8.21 #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; }

28 240-222 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

29 240-222 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

30 240-222 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; }

31 240-222 CPT: Strings/1031 6.4. 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; }

32 240-222 CPT: Strings/1032 Function prototype in string.h : char *strchr(const char *s, int c); ‘h’‘e’‘l’ ‘o’‘\0’ sh rest

33 240-222 CPT: Strings/1033 6.5. String Length: strlen() Fig. 8.38 Function prototype in string.h : size_t strlen(const char *s); size_t is equivalent to the type unsigned int (see string.h ).

34 240-222 CPT: Strings/1034 Coding strlen() size_t strlen(const char *s) { int n; for (n = 0; *s != '\0'; s++) n++; return (size_t) n; }


Download ppt "240-222 CPT: Strings/101 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship."

Similar presentations


Ads by Google