Assertions and Strings in C Programming

cs 131 autumn 2022 n.w
1 / 13
Embed
Share

Dive into the concepts of assertions and strings in C programming. Explore how assertions work in code and the fundamentals of working with strings. Learn about string functions, string indexing, and more to enhance your C programming skills.

  • C programming
  • Assertions
  • Strings
  • Functions
  • Programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CS& 131, Autumn 2022 Lecture 17: assertions; strings Thank you to Marty Stepp and Stuart Reges for parts of these slides 1

  2. Assertion example 1 void mystery(int x, int y) { int z = 0; // Point A while (x >= y) { // Point B x = x - y; z++; if (x != y) { // Point C z = z * 2; } // Point D } Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. x < y x == y z == 0 Point A SOMETIMES SOMETIMES ALWAYS Point B NEVER SOMETIMES SOMETIMES Point C SOMETIMES NEVER NEVER SOMETIMES SOMETIMES NEVER Point D // Point E printf("%d", z); } Point E ALWAYS NEVER SOMETIMES 2

  3. Strings string: A type that stores a sequence of text characters. In C strings are just arrays of characters char* name = "text"; char* name = expression; char name[] = "text"; char name[] = expression; Examples: char name[] = "Paddington Bear"; printf("%s\n", name); 3

  4. Indexes Characters of a string are numbered with 0-based indexes: char name[] = "Ultimate"; index character 0 U 1 l 2 t 3 i 4 m 5 a 6 t 7 e 8 \0 First character's index : 0 Last character's index : 1 less than the string's length The individual characters are values of type char All strings are terminated with the null character '\0' 4

  5. String functions Function name strcat(str1, str2) Must include string.h Description Concatenates string str2 onto the end of string str1. Appends the first n characters of str2 onto the end of str1. Copies str2 into str1 Copies as much of str2 into str1 as will fit. Fills any extra space with the null character. returns a pointer to the first occurrence of char1 in str1. returns a pointer to the last occurrence of char1 in str1. returns a pointer to the first occurrence of str2 in str1. Returns the length of string str1. Splits string into tokens on any character in delimiter string. One split per call. strncat(str1, str2, n) strcpy(str1, str2) strncpy(str1, str2, n) strchr(str1, char1) strrchr(str1, char1) strstr(str1, str2) strlen(str1) strtok(str1, del) 5

  6. String function examples // index 012345678901 char s1[] = "Sir Purrcival"; char s2[] = "merlin cat"; printf("%s", strlen(s1)); // 13 printf("%s\n", strchr(s2, 'i')); // in cat printf("the word if %s\n", strstr(s2, "in")); // in cat 6

  7. Modifying strings You can only modify strings by setting a character to a different character char word[] = "merlin"; word[0] = 'M'; // "Merlin" You can't: change the length of a string concatenate something to it unless there is extra room even if there is room, no handy simple operator like + 7

  8. Creating a string with a set size You can specify a size when you declare a string char name[size] Sets aside that many characters of size in memory Useful when you want to concatenate strings Example: char cats [20] = "merlin"; strcat(cats, " and percy"); 8

  9. Strings question Write a program that reads two people's first names and suggests a name for their company Example Output: Founder 1 first name? Danielle Founder 2 first name? John Company Size? small Suggested company name: JODANI Founder 1 first name? Danielle Founder 2 first name? John Company Size? Big Suggested company name: DANIJO 9

  10. Reading strings using scanf You can read strings from the user using scanf and %s as a placeholder scanf("%s", &variable_name) However the following code won't work: char* first_name; scanf("%s", &first_name); char last_name[]; scanf("%s", &last_name); C allocates space for a variable when it is declared. How much space is needed for an int, a double, a string? 10

  11. Creating a string with a set size You can specify a size when you declare a string char name[size] Sets aside that many characters of size in memory Useful when you want to concatenate strings Example: char cats [20] = "merlin"; strcat(cats, " and percy"); 11

  12. Reading strings using scanf C doesn't know how much space to leave for a string so you have to specify by including a length. Specify a maximum size for the string charvariable_name [ size ]; Add a number to the %s to cap the characters read in case too many are input max should be one less than the size of the storing string Example: char first_name[20]; scanf("%19s", &first_name); char last_name[20]; scanf("%19s", &last_name); 12

  13. String test functions Function Description strcmp(str1, str2) If str1 and str2 are equal returns 0. If str1 is alphabetically before str2 or a substring of str2 returns a negative number. Otherwise returns a positive number. Works like strcmp but only compares up to the first n characters of both strings strncmp(str1, str2, n) char name[] = "Sir Purrcival"; if(strncmp(name, "King")) { printf("This cat clearly rules the house"); } else if(strcmp(name, "cat")) { printf("Give your cat a real name!"); } 13

More Related Content