1 / 13

Computer Science 210 Computer Organization

Computer Science 210 Computer Organization. Strings in C. Representing Strings. printf(&quot;Hi Ken!<br>&quot;);. 'H'. 'i'. ' '. 'K'. 'e'. 'n'. '!'. '<br>'. nul. A string is a set of ASCII values that inhabit a sequence of bytes A string should always end in a nul character (ASCII 0)

Download Presentation

Computer Science 210 Computer Organization

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. Computer Science 210Computer Organization Strings in C

  2. Representing Strings printf("Hi Ken!\n"); 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' nul A string is a set of ASCII values that inhabit a sequence of bytes A string should always end in a nul character (ASCII 0) Many string functions use nul as a sentinel

  3. The String “Type” and Variables char *greeting = "Hi Ken!\n"; printf("%s", greeting); greeting 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' nul The pointer to a char named greeting holds the address of the first byte The standard string processing functions view strings as of type char*

  4. The String “Type” and Variables char *greeting = "Hi Ken!\n"; printf("%s", greeting); greeting 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' nul GREETING .STRINGz"Hi Ken!\n" LEA R0, GREETING PUTS

  5. View a String as an Array of char char *greeting = "Hi Ken!\n"; int i = 0; for (; greeting[i] != 0; i++) putchar(greeting[i]); greeting 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' nul A string looks like an array in memory So, we can use an index to access or modify a character in any cell

  6. The string Library and strlen #include <string.h> char *greeting = "Hi Ken!\n"; int i = 0; for (; i < strlen(greeting); i++) putchar(greeting[i]); greeting 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' nul The string library includes several common string functions (length, concatenation, comparison, etc.) strlen is O(n), because it searches for the nul character

  7. The string Library You can pass either a char* or a char array to these functions

  8. The ctype Library #include <string.h> #include <ctype.h> char *greeting = "Hi Ken!\n"; inti = 0; for (; i < strlen(greeting); i++) greeting[i] = toupper(greeting[i]); greeting 'H' 'I' ' ' 'K' 'E' 'N' '!' '\n' nul The ctype library includes several common character functions (test for letter or digit, convert to uppercase, etc.)

  9. String Input with scanf #include <stdio.h> int main(){ char name[20]; printf("Enter your first name: "); scanf("%s", name); printf("%s\n", name); } scanf stops at the first whitespace character and adds a nul character (works for one-word inputs) Must pass scanf the storage, usually an array of char

  10. String Input with gets #include <stdio.h> int main(){ char name[20]; printf("Enter your first name: "); gets(name); printf("%s\n", name); } gets stops at the first newline character, which is not included, and adds a nul character (works for multi-word inputs) Must pass gets the storage, usually an array of char

  11. Other Points about Strings • Can be the element type of an array • Can be the type of a field in a struct

  12. Example: The Argument Vector • The argument vector is an array of strings that are gathered up when a C program is run at the command prompt • The first string is the name of the command • The remaining strings are the arguments, if any • The argument vector and its length are optional formal parameters of the main function

  13. Print the Command Line Arguments /* Prints contents of the argument vector (the name of the program and any command-line arguments). */ #include <stdio.h> intmain(int length, char *argVec[]){ inti; for (i = 0; i < length; i++) printf("Argument %d: %s\n", i, argVec[i]); }

More Related