1 / 24

CS1010 Discussion Group 11

CS1010 Discussion Group 11. Week 10 – Characters and Strings. HELLO!. S lides are at http ://www.comp.nus.edu.sg/~yanhwa /. Game of Life. If life was a game, it’s definitely a pay-to-win game with DLCs. How is life?. We are three weeks from Week 13 :). Lab 5. Due on Wed. Nanotable 1.

montoyaa
Download Presentation

CS1010 Discussion Group 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1010 Discussion Group 11 Week 10 – Characters and Strings

  2. HELLO! Slides are at http://www.comp.nus.edu.sg/~yanhwa/

  3. Game of Life If life was a game, it’s definitely a pay-to-win game with DLCs

  4. How is life? We are three weeks from Week 13 :)

  5. Lab 5 Due on Wed

  6. Nanotable 1 Lab 4 • Easiest way: For insertion of a new element, scan array from back to front and find its correct place while shifting larger elements to the right • Use a variable “size”, update the variable during insertion and deletion (if have), to be able to easily check if an array is empty or not. Initialisation can just be setting size to 0.

  7. Characters and strings Lecture Summary • Characters are stored in one byte and look like this: ‘a’, ‘b’, ‘3’, ‘5’, ‘\n’, ‘\0’ • char grade = A what is wrong with this statement? • The complier will think that A is a variable • You need the single quotation marks ‘ ‘ to turn it into a character

  8. Implicit type conversion Lecture Summary • The standard integer types are ranked in the order: • _Bool < char < short < int < long < long long • Integer types smaller than int are promoted when an integer operation is performed on them. Example: • char c1, c2; • c1 = c1 + c2;

  9. null-terminated string Lecture Summary • Two ways to declare: • Char arrays (that can be modified!) char name[12] = "Chan Tan"; • String literals / String constant char *namePtr = "Chan Tan"; [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] name[0] C h a n T a n \0 \0 \0 \0 namePtr C h a n T a n \0

  10. null-terminated string Lecture Summary • char *namePtr = "Chan Tan"; • namePtr = "Lee Hsu"; • Address of the string will change (another location in memory!) namePtr This is a string constant = string literal = immutable L C h e e a n H T s a u \0 n \0

  11. null-terminated string Lecture Summary • “A” vs ‘A’ • First one is a string constant having character ‘A’ and ‘\0’ (null char) • Null character is automatically assigned by complier

  12. scanf Lecture Summary fgets • Fgets is “file get string” • Specify “buffer size” so there is a limit on how much input to take • reads until size – 1 or until newline • Reads in the newline character on interactive input • Scanf is “scan formatted” • There could be buffer overflow when using ‘%s’. Input string is longer than buffer • Reads until white space

  13. Tutorial 1 ch1 = 2; ch2 = A \062 is in octal (base 8) = 50 \x41 is in hexademical (base 16) = 65

  14. Tutorial 3 name should be declared to contain up to 9 characters, to cater to \0: char username[9]; However, since “pineapple” is more than 8 characters, it spills into “unauthorised” memory space.

  15. Tutorial 4 It depends on what values are stored in the memory after the array board. Assuming that it is the case below . %s picks up from the starting character specified and ending at a null character \0

  16. Tutorial 5 The strcpy() function attempts to copy the characters of the string (“yes”) pointed to by str2 into the space pointed to by fruit2. However, fruit2 is pointing to a string literal, which is a read-only space.

  17. Tutorial 5 strcpy(name, "Lee Hsu"); name is a pointer to the first element of the character array [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] name[0] C h a n T a n \0 \0 \0 \0

  18. Throwback to pointers and arrays int a[10]; You are in fact declaring a pointer a to the first element in the array a is exactly the same as &a[0]. The only difference between a and a pointer variable is that the array name is a constant pointer - you cannot change the location it points at. When you write an expression such as a[i] this is converted into a pointer expression that gives the value of the appropriate element. To be more precise, a[i] is exactly equivalent to *(a+i) https://www.le.ac.uk/users/rjm1/cotter/page_59.htm

  19. Count non space How should we read input? Should we use a while loop and check the characters one by one? When to end the while loop? How do we know whether a character is a space or non space How to write count_nonspace(char *str) instead? See lecture mystrlen

  20. Count non space printf("Enter a string with at most %d characters: ", MAX_LENGTH); while (i < MAX_LENGTH && (str[i] = getchar()) != '\n') { i++; } str[i] = '\0'; printf("Number of non-white-space characters = %d\n", count_nonspace(str)); • Extra: how to use getchar() to read a string? If we don’t use fgets

  21. Converting string Skip over space characters

  22. Palindrome • How to check palindrome? (…) How to “disregard case”?

  23. Hangman v2 Array of pointers to strings

  24. Practice Practice S08P05 – Pig Latin Are you a master of string and character functions in string.h?

More Related