240-222 CPT: Strings/101 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of...

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

Transcript of 240-222 CPT: Strings/101 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of...

240-222 CPT: Strings/10 1

240-222 Computer Programming Techniques240-222 Computer Programming TechniquesSemester 1, 1998Semester 1, 1998

Objectives of these slides:– to discuss strings and their relationship to

pointers

10. StringsChapter 8

240-222 CPT: Strings/10 2

Overview: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

240-222 CPT: Strings/10 3

1. What is a String?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"

240-222 CPT: Strings/10 4

Use:Use:

char color[] = "blue";

or equivalently:

char color[] = {'b','l','u','e','\0'};

240-222 CPT: Strings/10 5

"a" is not 'a'"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’

240-222 CPT: Strings/10 6

2. Accessing Strings with Pointers2. Accessing Strings with Pointers

char name[] = "Andrew"; 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

240-222 CPT: Strings/10 7

3. Word Counting3. Word Counting

#include <stdio.h>#include <ctype.h> /* for isspace() */

int words_cnt(char *);

int main(){ char *test = "hello world";

printf("Word count: %d\n", words_cnt(test); return 0;}

continued

240-222 CPT: Strings/10 8

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;}

240-222 CPT: Strings/10 9

4. String Conversion Functions 4. String Conversion Functions Sec. 8.4Sec. 8.4#include <stdio.h>#include <stdlib.h>

int main(){ double d;

d = atof("99.0"); printf("\"99.0\" converted to double

is %.3f\n", d); return 0;}

240-222 CPT: Strings/10 10

Output:Output:

"99.0" converted to double is 99.000

Function prototype in stdlib.h:double atof(const char *nptr);

240-222 CPT: Strings/10 11

5. Input and Output of Strings5. Input and Output of Strings

#include <stdio.h>

int main(){ char word[7];

scanf("%s", word);

printf("%s", word); puts(word);

return 0;}

240-222 CPT: Strings/10 12

Reversing (Again)Reversing (Again) Fig. 8.13Fig. 8.13

#include <stdio.h>#include <string.h>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

240-222 CPT: Strings/10 13

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

240-222 CPT: Strings/10 14

OutputOutput

Enter a line of text:hello

Line backwards is:olleh

. . . . .‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’

s

240-222 CPT: Strings/10 15

6. String Manipulation6. 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()

240-222 CPT: Strings/10 16

6.1. Copying a String: strcpy()6.1. Copying a String: strcpy()Fig. 8.18Fig. 8.18#include <stdio.h>#include <string.h>

int main(){ char x[] = "Happy Birthday to You"; char y[25];

printf("String in x is: %s\nString in y is: %s\n", x, strcpy(y, x));

return 0;}

240-222 CPT: Strings/10 17

Output:Output:

String in x is Happy Birthday to YouString in y is Happy Birthday to You

Function prototype in string.h:char *strcpy(char *s1,

const char *s2);

240-222 CPT: Strings/10 18

Coding strcpy()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

240-222 CPT: Strings/10 19

/* pointer version no. 1 */char *strcpy(char *s, const char *t){ char *temp = s;

while ((*s = *t) != '\0') { s++; t++; } return temp;}

continued

240-222 CPT: Strings/10 20

/* pointer version no. 2 */char *strcpy(char *s, const char *t){ char *temp = s; while ((*s++ = *t++) != '\0') ; return temp;}

continued

240-222 CPT: Strings/10 21

/* pointer version no. 3 */char *strcpy(char *s, const char *t){ char *temp = s; while (*s++ = *t++) ; return temp;}

240-222 CPT: Strings/10 22

6.2. Concatenating Strings: strcat() 6.2. Concatenating Strings: strcat() Fig. 8.19Fig. 8.19

#include <stdio.h> #include <string.h> 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; }

240-222 CPT: Strings/10 23

Output:Output:

s1 = Happys2 = New Yearstrcat(s1, s2) = Happy New Years1 = Happy New Year

Function prototype in string.h:char *strcat(char *s1,

const char *s2);

240-222 CPT: Strings/10 24

Coding strcat()Coding strcat()

char *strcat(char *s1, const char *s2) { char *p = s1; while(*p) /* while (*p != '\0') */

p++; while (*p++ = *s2++) ; return s1; }

240-222 CPT: Strings/10 25

In picture form:In picture form:

‘H’ ‘o’ ‘\0’‘b’ ‘o’ ‘\0’

p

s1

s2

240-222 CPT: Strings/10 26

6.3. Comparing Strings: strcmp()6.3. Comparing Strings: strcmp()

int strcmp(const char *s1, const char *s2);

Compares the string s1 to the string s2

Returns < 0 if s1 < s20 if s1 == s2> 0 if s1 > s2

240-222 CPT: Strings/10 27

Using strcmp() Fig. 8.21Using strcmp() Fig. 8.21

#include <stdio.h>#include <string.h>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;}

240-222 CPT: Strings/10 28

Output:Output:

s1 = Happy New Years2 = Happy New Years3 = Happy Holidaysstrcmp(s1, s2) = 0strcmp(s1, s3) = 6strcmp(s3, s1) = -6

240-222 CPT: Strings/10 29

Coding strcmp()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

240-222 CPT: Strings/10 30

The pointer version:The pointer version:

int strcmp(const char *s, const char *t){ for ( ; *s == *t; s++, t++) if (*s == '\0') return 0; return *s - *t;}

240-222 CPT: Strings/10 31

6.4. Searching a String: strchr() 6.4. Searching a String: strchr() Sec. 8.8Sec. 8.8

#include <stdio.h>#include <string.h>

int main(){ char *rest, *sh = "Hello"; char c = 'l';

rest = strchr(sh, c); printf("%s", rest); /* gives llo */ return 0;}

240-222 CPT: Strings/10 32

Function prototype in string.h:char *strchr(const char *s, int c);

‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’

shrest

240-222 CPT: Strings/10 33

6.5. String Length: strlen() Fig. 8.386.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).

240-222 CPT: Strings/10 34

Coding strlen()Coding strlen()

size_t strlen(const char *s){ int n;

for (n = 0; *s != '\0'; s++) n++; return (size_t) n;}