1 / 8

Department of Computer and Information Science, School of Science, IUPUI

This course covers the basics of strings and characters in programming, including literals, variables, declarations, pointers, and arrays. Learn how to manipulate and use strings effectively in your programs.

myrtlek
Download Presentation

Department of Computer and Information Science, School of Science, IUPUI

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. Department of Computer and Information Science,School of Science, IUPUI CSCI N305 Characters and Strings Literals and Variables

  2. Fundamentals of Strings and Characters • Characters • Building blocks of programs • Every program is a sequence of meaningfully grouped characters • Character constant • An int value represented as a character in single quotes • 'z' represents the integer value of z • Strings • Series of characters treated as a single unit • Can include letters, digits and special characters (*, /, $) • String literal (string constant) - written in double quotes • "Hello" • Strings are arrays of characters • String a pointer to first character • Value of string is the address of first character

  3. Fundamentals of Strings and Characters • String declarations • Declare as a character array or a variable of type char * char color[] = "blue"; char *colorPtr = "blue"; • Remember that strings represented as character arrays end with '\0' • color has 5 elements • Inputting strings • Use scanf scanf("%s", word); • Copies input into word[] • Do not need & (because a string is a pointer) • Remember to leave room in the array for '\0'

  4. 800 731 731 ‘A’ 732 ‘B’ 733 ‘C’ 734 ‘D’ 735 ‘E’ 736 ‘\0’ Character Pointers • String constant acts like a character pointer char *pc = “ABCDE”; /* declare a character pointer variable */ VariableAddressValue constant 731 ‘A’ constant 732 ‘B’ constant 733 ‘C’ constant 734 ‘D’ constant 735 ‘E’ constant 736 ‘\0’ pc 800 731 char s1[] = “abc”; VariableAddressValue s1[0] 900 ‘a’ s1[1] 901 ‘b’ s1[2] 902 ‘c’ s1[3] 903 ‘\0’

  5. 104 ‘t’ 105 ‘e’ 106 ‘s’ 107 ‘t’ 108 s1[] ‘\0’ 1000 ‘a’ 1001 ‘b’ 1002 ‘c’ 1003 ‘\0’ s s3[] 1100 1100 2000 2000 2000 ‘a’ ‘a’ s3[0]=‘A’ 2001 2001 ‘b’ ‘b’ 2002 2002 ‘c’ ‘c’ 2003 2003 ‘d’ ‘d’ 2004 2004 ‘e’ ‘e’ Character Pointers CONSTANT MEMORY AREA (READ ONLY) Example: char s1[] = “abc”; char *s2 = “abc”; f() { s1[1] = ‘y’; /* OK */ s2[1] = ‘y’; /* wrong (PC is OK)*/ s1 = “test”; /* wrong */ s2 = “test”; /* OK */ } Example: char s3[] = “abcdef”; f1() { char *s = s3; *s = ‘A’; /* s3[0]=‘A’ */ s = “test”; printf(“%s\n%s\n”,s,s2); } s2 800 100 100 ‘a’ 101 ‘b’ 102 ‘c’ 103 ‘\0’ ...

  6. Pointer Arrays • Syntax: int *pi[3]; /* pi[0], pi[1], pi[2] */ float *pf[3]; /* pf[0], pf[1], pf[2] */ Example 1: int i=1, j=2, k=3; int *pi[3] = {&i, &j, &k}; • Example 2: • char *pc[3]={“ABC”, “DEF”, “GH”}; Const can not be changed

  7. argv pointer array e c h o \0 argc h e l l o \0 3 null w o r l d \0 Command-Line Arguments • argcandargv • In environments those support C, there is a way to pass command-line arguments or parameters to a program when it begin executing. • When main is called to begin execution, it is called with two arguments – argc and argv • argc : The first (conventionally called argc) is the number of command-line arguments the program was invoked with • argv : The second (conventionally called argv) is a pointer to an array of character strings that contain the arguments, one per string. Example: if echo is a program and executed on unix prompt, such as 10<user:/home/droberts> echo hello world

  8. Command-Line Arguments Example: print out the arguments. ex: hello world main (int argc, char *argv[]) { int i; for (i = 1; i < argc; i++) printf(“%s%c”, argv[i], (i < argc-1) ? ‘ ’ : ‘\n’); } main (int argc, char *argv[]) { while (--argc > 0) printf(“%s%c”, *++argv, (argc > 1) ? ‘ ’ : ‘\n’); } main (int argc, char *argv[]) { while (--argc > 0) printf((argc > 1) ? “%s “ ; “%s\n“, *++argv); }

More Related