Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today’s Material Strings Definition Representation Initialization

Similar presentations


Presentation on theme: "Today’s Material Strings Definition Representation Initialization"— Presentation transcript:

1 Today’s Material Strings Definition Representation Initialization
Reading/Writing Strings String Operations: strlen, strcpy, strcat, strcmp Array of Strings Command Line Arguments

2 Strings: Definition A string is an array of characters terminated by the NULL char ‘\0’ Example: char str[8]; Declares a char array that contains at most 8 chars If char array str will be used to store strings, it can contain at most 7 chars and MUST be terminated by the NULL char ‘\0’

3 Strings: Representation
If we store the string “abcd” in str, here is how it will look like We do not know what’s stored after the NULL char str: a b c d \0 ? ? ? 1 2 3 4 5 6 7 String MUST be NULL terminated, i.e., the end of the string must be set to 0

4 Empty String Empty String “” corresponds to a char array whose first element is the NULL char ‘\0’ str: \0 ? ? ? ? ? ? ? 1 2 3 4 5 6 7 The first char of the empty string is the NULL char

5 Max Length String A string of length 8, e.g., “abcdefgh” can NOT be stored in str str: a b c d e f g h 1 2 3 4 5 6 7 No place to store the NULL char! This is a char array containing 8 chars But this is NOT a string. A string MUST always be terminated by the NULL char

6 Strings: Warning Just to stress again, a declaration such as char str[8] simply tells us that we can store at most 8 chars in str At any point during the program execution we may be storing up to 8 chars in str But if “str” is to store a string, we can store at most 8-1=7 chars in it, and must always terminate the string with the NULL char

7 String Initialization
A char array may be initialized during declaration just like any other array char str[8] = {‘a’, ‘b’, ‘c’}; Recall that unspecified array elements are filled with 0, that is, the NULL char. So the above declaration corresponds to the following string a b c \0 1 2 3 4 5 6 7 str: NULL terminated as required

8 String Initialization (cont)
If a char array will store strings, there is a simpler way to specify the initializer We just put the string inside double quotes, called a string literal char str[8] = “abc”; /* same as before */ a b c \0 1 2 3 4 5 6 7 str: NULL terminated as required

9 String Initialization (cont)
If we don’t specify the length of the array in the initializer, the compiler will allocate as many chars as the length of the string + the NULL char char str[] = “abc”; str: a b c \0 1 2 3

10 String Initialization (cont)
Specific to strings, we can initialize string literals as follows: char *str = “abc”; str: a b c \0 1 2 3 The difference between this declaration and the previous declaration is that the strings initialized this way are READ-ONLY and can NOT be modified Strings declared as char str[]=“abc”; can be modified if desired

11 Note on char *p; declarations
char *p; simply declares a pointer to a char, but does not allocate any space for a string You can use this pointer to point to strings and manipulate them, but cannot use this pointer without first pointing it to a valid string char *p; char str[] = “abcde”; *p = ‘x’; /* Illegal at this point */ p = &str[1]; *p = ‘x’; /* OK now */

12 Wrap-up Example char *str1=“abc”; /* A string literal of 4 bytes. Can’t be changed */ char str2[]=“abc”;/* A string of 4 bytes. Can be changed */ char str3[20]=“abc”; /* A string of 20 bytes (19 chars+1 NULL char) * Initialized to abc */ char str4[40]; /* An array of 40 chars. Can store a string of * length at most 39 chars. One char must be * left for the NULL char */ char *p; /* Simply declares a char pointer that is not pointing to * a valid memory address yet. This is NOT a string. * *p would be invalid at this point */ main(){ p = &str2[1]; *p = ‘X’; /* Makes str2 = “aXc” */ p = str3; *p = ‘Y’; /* Makes str3 = “Ybc” */ p = &str3[3]; *p = ‘Z’; *(p+1) = ‘\0’; /* Makes str3 = “YbcZ” */ str4[0] = ‘k’; str4[1]=‘l’; str4[2]=‘\0’; /* str4 = “kl” */ printf(“str1: <%s>, str2: <%s>, str3: <%s>, str4: <%s>\n”, str1, str2, str3, str4); } /* end-main */

13 Location of Strings in Program’s Address Space
Stack Variables with automatic storage duration i.e., local variables and function parameters Dynamically allocated data Heap Variables (data) allocated with malloc uninitialized data (bss) str4 would be here initialized data Data str2 & str3 would be here .rodata section str1 & printf’s string literals would be here Code Text

14 Printing Strings C allows you to print strings using 2 functions
(1) puts(str); (2) printf(“%s”, str); char str1[]=“This is my first string”; /* Prints the string and moves the cursor to * the beginning of the next line */ puts(str1); /* Prints the string starting at the cursor position */ printf(“%s”, str1); /* Reserve 40 spaces and print the string right-adjusted */ printf(“%40s”, str1); /* Reserve 40 spaces and print the string left-adjusted */ printf(“%-40s”, str1);

15 Printing Strings (cont)
C allows you to print strings using 2 functions (1) puts(str); (2) printf(“%s”, str); char str1[]=“This is my first string”; /* Print only the first 10 chars of the string, * right-adjusted*/ printf(“%.10s”, str1); /* Reserve 40 spaces and print only the first 10 chars * of the string, right-adjusted */ printf(“%40.10s”, str1); * of the string, left-adjusted */ printf(“%-40.10s”, str1);

16 Reading Strings C allows you to read strings using 2 functions
(1) gets(str); (2) scanf(“%s”, str); char str2[80]; /* Reads the current input chars until the end of the line * ‘\n’ char is seen */ gets(str2); /* Skips all white space chars (space, tab, newline), and * start reading input until the next space char is seen */ scanf(“%s”, str2);

17 Reading Strings (cont)
char str2[80]; /* Skips all white space chars (space, tab, newline), and * start reading input until the next space char is seen */ scanf(“%s”, str2); /* If the input is the following: _ is a space char */ _ _xyz123_ _ _45_ _67 scanf will skip 2 white spaces and start filling str2 with “xyz123” Then it will encounter a white space and stop reading input The next scanf(“%s”, …) will skip these white spaces and return “45”

18 Reading Strings (cont)
You can write your own function to read a string until the end of the line is encountered char *ReadLine(char *str){ char ch; char *p = str; while((ch=getchar()) != ‘\n’) *p++=ch; *p = ‘\0’; /* Mark the end of the string with NULL char */ return str; } /* end-ReadLine */ main(){ char str[80]; ReadLine(str); printf(“Line of input = <%s>\n”, str); } /* end-main */

19 Reading Strings (cont)
Another version would be to read until the end of the line is seen OR until “n” chars are read char *ReadNLine(char *str, int n){ char ch; char *p = str; while (n-- > 0){ if ((ch = getchar()) == ‘\n’) break; *p++ = ch; } /* end-while */ *p = ‘\0’; /* Mark the end of the string with NULL char */ return str; } /* end-ReadNLine */ main(){ char str[80]; char *p = NULL; p = ReadNLine(str, 79); /* Can store at most 79 chars */ printf(“Line of input = <%s>\n”, p); } /* end-main */

20 String Operations: strlen
C standard library provides many functions to manipulate strings You need to include <string.h> to use these functions Here are some of the important functions strlen(const char *str); strcpy(char *str1, const char *str2); strcat(char *str1, const char *str2); strcmp(const char *str1, const char *str2); Here are the implementations for these functions just for illustration

21 String Operations: strlen, strcpy
int strlen(const char *str){ int len = 0; while (*str++) len++; return len; } /* end-strlen */ char *strcpy(char *str1, const char *str2){ char *p = str1; while (*str2) *p++ = *str2++; *p = ‘\0’; return str1; } /* end-strcpy */

22 String Operations: strcat, strcmp
char *strcat(char *str1, const char *str2){ char *p = str1; while(*p) p++; while (*str2) *p++ = *str2++; *p = ‘\0’; return str1; } /* end-strcat */ int strcmp(const char *str1, const char *str2){ while (*str1 && *str2 && *str1 == *str2){ str1++; str2++; } /* end-while */ return *str1-*str2; } /* end-strcmp */

23 Array of Strings How do we store an array of strings?
Here is one way to declare an array of strings char planets[][8] = { “Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”, “Pluto”}; We are allowed to omit the number of rows Since that is obvious from the number of elements C requires that we specify the number of columns

24 Array of Strings Layout of this declaration would be as follows:
1 2 3 4 5 6 7 M e r c u r y \0 1 V e n u s \0 \0 \0 2 E a r t h \0 \0 \0 3 M a r s \0 \0 \0 \0 4 J u p i t e r \0 5 S a t u r n \0 \0 6 U r a n u s \0 \0 7 N e p t u n e \0 8 P l u t o \0 \0 \0 Each row has the same length (8 bytes) although only 3 planes have names long enough to fill the entire row This leads to wasted space

25 Array of Strings The alternative is to make each row to be of different length, just long enough to hold the string The trick is to create an array of pointer to strings char *planets[] = { “Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”, “Pluto”};

26 Array of Strings Here is how this alternative array of strings looks like in memory planets M e r c u r y \0 V e n u s \0 1 2 E a r t h \0 3 M a r s \0 4 5 J u p i t e r \0 6 S a t u r n \0 7 8 U r a n u s \0 N e p t u n e \0 P l u t o \0 Each string requires just enough space required to store the string: No wasted space

27 Array of Strings We can print the array of strings as follows
char *planets[] = { “Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”, “Pluto”}; for (i=0; i<=8; i++){ printf(“Planet[%d]: <%s>\n”, i, planets[i]); } /* end-for */

28 Command Line Arguments
When we run a program, we will often need to supply it with info from the command line All Unix commands work this way You supply command line arguments to determine the command’s behavior Here are a couple of examples % ls % ls –l % ls –a –l % ls –al /usr/include

29 Command Line Arguments
Command line arguments are available to your programs too They are passed to “main” function as arguments by the operating system Here is how you access the command line arguments in your C programs int main(int argc, char *argv[]){ int i; for (i=0; i<argc; i++) printf(“Argument[%d]: %s\n”, i, argv[i]); } /* end-main */

30 Command Line Arguments
If the name of the executable of your program is “a.out” and you execute it as follows % a.out arg1 xyz arg3 argc will be 4 and argv will be an array of strings pointing to “a.out” “arg1” “xyz” “arg3” argv a . o u t \0 1 a r g 1 \0 2 x y z \0 3 4 NULL a r g 3 \0


Download ppt "Today’s Material Strings Definition Representation Initialization"

Similar presentations


Ads by Google