University of Scranton



C Tutorial - Strings2. C Strings2.1. As arrays of characterschar progName[8]; An array of 8 characters. Each C string must end with a '\0', which is used to indicate the end of a string. Thus, this array can only take up a string of 7 characters. The array is not initialized, so it may contain anything. char dog[6] = {'D', 'O', 'G', '\0'};An array of 6 characters with the four characters initialized with DOG\0. The two cells are not initialized. So, the last cells could have anything. char greeting[] = "Hello";The size of the array was not specified. Its size is determined by the string on the right side. An array of 6 characters with the 5 characters of “Hello” and the last cell of '\0'. In other words, the C language adds the special character automatically. You cannot assign one string to another, and you cannot compare two arrays of characters in C. greeting = dog; // illegalif (greeting == dog) // illegal2.2. Input and output of character arraysscanf(“%s%s%s”, progName, dog, greeting);Input format specifier %s is for string input. It reads a word. Words in the are separated by white spaces (space, tab, carriage return/next line). Thus, input of “mkdir cat john” will be put into progName, dog, and greeting arrays, respectively. It is the programmer’s responsibility to not enter a word which is longer than the size of the array minus 1. If the input for array dog is “elephant”, scanf will no generate any error, but it will overwrite the 6 bytes after dog with “phant\0”.Note: We did not use the address-of operator in front of those array variables. Because in C, array variable name is “equivalent” to a pointer. Note: Do NOT leave spaces between the input format specifiers. printf(“%s\t%s---%s”, progName, dog, greeting);Output format specifier %s is for string output. The printf function requires every string end with the special character \0. Basically, the function prints every byte until it finds \0. If a string does not end with the special character, you will likely get a segmentation fault exception. If the scanf of the first bullet was executed, then this printf should display “mkdir[TAB]cat---john”.printf(“%c--%c--%c”, dog[0], dog[1], dog[2]);Output format specifier %c is for displaying a single character. You can display a string as individual characters. This printf should display “cat”.2.3. String pointerschar *ptr1; char *ptr2 = greeting; ptr1 = progName;The first line declares a variable of a pointer ptr1 pointing to a character and It is not initialized. The second line declares a char pointer ptr2 pointing to the array of greeting. The third line make ptr1 point to the array of progName. printf(“%s-%s---%s%-s”, ptr1, progName, ptr2, greeting);(ptr1 == &progName[0]) ptr2[0] = ‘h’;The printf would display “mkdir-mkdir---Hello-Hello” since ptr1 and progName all point to the same string and so do ptr2 and greeting. The expression on the second line is true. In other words, the value of ptr1 is the address of progName[0]. The third line shows that you may use array index notation with a pointer. After it is executed, greeting will hello with a lower case of letter H. While you cannot compare two arrays of characters, you may compare two string pointers. (ptr1 == ptr2) would be true if ptr1 and ptr2 all point to the same string. If they point to two separate strings, the expression is false even if the two strings have the same contents. This is because the expression compares the values of the two variables. If you want to compare if two strings have the same contents, you need to use the strcmp function, which will be introduced later. 2.4. Dynamically Allocating Spaces for Stringchar *ptr3 = (char *)malloc(25 * sizeof(char)); scanf(“%s”, ptr3); printf(“The input was %s\n”, ptr3);free(ptr3);The first line allocates space for 25 characters and assign the address to ptr3. Note that the space is allocated on the heap, not the stack, which implies it must be freed. The second line reads a string from keyboard (the string cannot be longer than 24 characters, why?) to the allocated space. You can print the string and finally you need to call free() when the space is no longer needed. 2.5. String Manipulation Functionsint strcmp(const char *s1, const char *s2);Compares two strings s1 and s2. Returns an integer: 0: if s1 and s2 are equal; <0: if s1 is smaller than s2 >0: if s1 is larger than s2char *strcat(char *dest, const char *src);Appends string src to the string dest. The space of the first string must be big enough to contain both strings. E.g., char dest[20] =”Hello”;char src[] = “World”;strcat(dest, src);Then dest contains “HelloWorld\0”. Note that the original \0 at the end of dest was not kept in the result of strcat. char *strcpy(char *dest, const char *src);Copy the string src to string dest. After the function, strcmp(dest, src) returns ). The space of dest must be at least the size of src. The string src must end with \0.int strlen(char *s);Returns the number of characters before \0 in string s. . ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download