Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.

Similar presentations


Presentation on theme: "Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving."— Presentation transcript:

1 Slides from Shane Griffith (TA and guest instructor in Fall 2008) http://www.ece.iastate.edu/~alexs/classes/2009_Fall_185/ CprE 185: Intro to Problem Solving (using C)

2 Strings CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © 2008

3 Chapter 9 (Strings)

4 Outline Declare a String Input a String Assign a String String functions Character type operations

5 Print a character Array Program: // character array char c[10] = {‘E’, ‘E’, ‘/’,‘C’, ‘p’, ‘r’,‘E’}; for(i=0; i< 7; i++) { printf(“%c“, c[i]); } ____________________________________________ Output: EE/CprE

6 Print another way Program: // character array char c[10] = {‘E’, ‘E’, ‘/’,‘C’, ‘p’, ‘r’,‘E’}; printf(“%s”, c); //%s to print a string ____________________________________________ Output: EE/CprE

7 Declare another way Program: // define character array with a string char c[10] = “EE/CprE”; printf(“%s”, c); //%s to print a string ____________________________________________ Output: EE/CprE

8 Character vs. String Do NOT confuse strings with individual characters ‘E’ is a character “E” is a string initialize a character array with 1) Array of characters {‘E’,‘E’,‘/’,‘C’,‘p’,‘r’,‘E’} 2)A String “EE/CprE” //easier to type

9 Declaring strings char c[10] = “EE/CprE”; //only 7 chars What about the uninitialized characters in the string?

10 Array contents Contents of the c array c E E / C p r E \0 ? ? NULL character automatically assigned

11 Array Contents Program: // character array char c[10] = “EE/CprE”; printf(“%s”, c); //output 1 c[8] = ‘a’; printf(“%s”, c); //output 2 _________________________ Output 1: EE/CprE Output 2: EE/CprE c E E / C p r E \0 a ?

12 Strings, what’s happening Prints until NULL character is reached  leave room for it! Program: // array size should be >= 8 char c[7] = “EE/CprE”; printf(“%s”, c); //%s to print a string ____________________________________________ Output:

13 Strings Length is determined by first NULL in the string Most string functions in C add NULL automatically Array of strings is a double array of characters  From the book: char month[12][10] = {“January”, “February”, …, “December”};

14 Program: char c[N]; scanf(“%s”, c); //no & symbol is required printf(“%s”, c); ____________________________________________ Input: “EE CprE” Output: EE //input separated by white space Input a String

15 char c[N]; gets(c); printf(“%s\n”, c); ____________________________________________ Input: “EE CprE” Output: EE CprE Input a String gets  Get a string from user input  reads until enter is pressed

16 Program: // character array char c[N]; c[N] = “Monday”; //will NOT work Assign value to a String Cannot use = operator in C to assign a String

17 #include … char c[N]; char tomorrow[] = “Tuesday”; … strcpy(c, “Monday”); //c is the destination … strcpy(c, tomorrow); //another assignment Assign value to a String Use the String function strcpy in string.h  Copies a string into a destination string

18 #include … char c[7]; strcpy(c, “Wednesday”); Assign value to a String Watch out for overflow (bad) c W e d n e s d a \0 y overflow a y

19 #include … char c[7]; strncpy(c, “Wednesday”, 7); Assign value to a String Better to use strncpy  copies up to n characters from the source c W e d n e s d need NULL

20 #include … char c[7]; strncpy(c, “Wednesday”, 6); c[6] = ‘\0’; //OR c[6] = NULL; //NULL and ‘\0’ are the same Assign value to a String Better to use strncpy  assign NULL to the end afterword c W e d n e s \0

21 String Functions strcmp or strncmp  Compares two strings (good for sorting) strcmp(“Saturday”, “Sunday”); //answer is -1 strncmp(“way”, “wash”, 2); //answer is 0 strlen  Returns the number of characters in “Saturday” strlen(“Saturday”) //answer is 8

22 String Functions strcat  Concatenate two strings (good for sorting) char a[N] = “Hello ”; char b[N] = “World!\n”; strcat(a, b); printf(“%s”, a); ____________________________________________ Output: Hello World!

23 Character Operations isalpha  is the character a letter of the alphabet? isdigit  Is the character a number? islower, isupper  Checks the case of the letter ispunct isspace

24 A good reference http://www.crasseux.com/books/ctutorial/String- library- functions.html#String%20library%20functionshttp://www.crasseux.com/books/ctutorial/String- library- functions.html#String%20library%20functions

25 Questions?

26 THE END


Download ppt "Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving."

Similar presentations


Ads by Google