Download presentation
Presentation is loading. Please wait.
Published byΑμάλθεια Πολίτης Modified over 6 years ago
1
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length chunk of data. C uses a delimiter (‘\0’, the null character) to designate the end of a string; hence, A C string is a variable-length null-terminated character array
2
C-strings Array with base type char One character per indexed variable
One extra character: '\0' Called ‘null character’ Delimiter of the string To declare a string, declare a char array, e.g. char s[10]; Declares a c-string variable to hold up to 9 characters + one null character
3
String Variable Typically ‘partially-filled’ array
Declare large enough to hold max-size string, including the null character. A standard array: char s[10]; If s contains the string “Hi Mom!”, it will be stored as:
4
String Initialization
Can initialize string: char myMessage[20] = "C is fun!"; Need not fill the entire array Initialization places '\0' at the end
5
String Initialization
Can omit array-size: char shortString[] = "abc"; Automatically makes size one more than the length of the quoted string NOT the same as: char shortString[] = {'a', 'b', 'c'}; IS the same as: char shortString[] = {'a', 'b', 'c', '\0'};
6
Strings and the assignment operator
Recall that the name of a string is a pointer constant; it points to the first character of the string. Given char str1[6] = “Hello”; char str2[6]; str2 = str1; // compiler error
7
Accessing elements of a string
A string IS an array; therefore, Can access indexed variables of: char str[7] = "Hello"; str[0] is 'H' str[1] is 'e' str[2] is 'l' str[3] is 'l' str[4] is 'o'; str[5] is ‘\0’ str[6] is unknown
8
String Index Manipulation
Can manipulate indexed variables char happyString[7] = "DoBeDo"; happyString[6] = 'Z'; Be careful! Here, ‘\0’ (null) was overwritten by a ‘Z’! If null is overwritten, string no longer ‘acts’ like a string! Unpredictable results!
9
String Input - fgets char *fgets (char * strPtr, int size, FILE *fp)
Inputs characters from the specified file pointer through \n or until specifed size is reached Puts newline (\n) in the string if size not reached!!! Appends \0 at the end of the string If successful, returns the string Example for stdin: const int MAX_LINE = 100; char lineIn[MAX_LINE+2]; // allow for \n and \0 fgets(lineIn, MAX_LINE, stdin);
10
String Output - fputs int fputs (const char *strPtr, FILE *fp)
Takes a null-terminated string from memory and writes it to the specified file pointer Drops \0 Programmer's responsibility: Make sure the newline is present at the appropriate place(s) Example for stdout: char lineout[100] = "Hello!\n"; fputs(lineout, stdout);
11
Use of pointers in processing C-strings.
Recall that a "C-string" is an array of characters whose logical end is denoted by a zero valued byte. The C standard library has a number of functions designed to work with C strings. The strtol() function is one of them. You can see most of them on a Linux system via the command: man string stpcpy, strcasecmp, strcat, strchr, strcmp, strcoll, strcpy, strcspn, strdup, strfry, strlen, strncat, strncmp, strncpy, strncasecmp, strpbrk, strrchr, strsep, strspn, strstr, strtok, strxfrm, index, rindex - string operations
12
String manipulation functions
Must #include <string.h>
13
String manipulation functions (cont'd)
14
= with strings strings are not like other variables
Cannot assign or compare: char aString[10]; aString = "Hello"; // ILLEGAL Can ONLY use ‘=‘ at declaration of string! Must use library function for assignment: strcpy(astring, "Hello");
15
= with strings strcpy(aString, "Hello")
Sets value of aString equal to “Hello” NO checks for size! Up to programmer to verify size of aString is large enough to hold all characters of the source, just like other arrays!
16
== with strings Cannot use operator == char aString[10] = "Hello";
char anotherString[10] = "Goodbye"; if(aString == another String) // NOT allowed Must use strcmp library function to compare: if(strcmp(aString, another String)) printf("Strings NOT the same.\n"); else printf("Strings are the same.\n");
17
string Functions: strlen()
String length Often useful to know the length of a string: char myString[10] = "dobedo"; printf("length = %d\n", strlen(myString)) Returns number of characters Not including null character Result here: 6
18
string Functions: strcat()
String concatenate char stringVar[20] = "The rain"; strcat(stringVar, "in Spain"); Note result: stringVar now contains The rainin Spain Be careful! Incorporate spaces as needed!
19
string Functions In the following example, we will see how strcpy might be implemented. Its prototype is shown below. char *strcpy(char *destination, const char *source);
20
An implementation of strcpy
The name zstrcpy is used to avoid potential nastygrams and name conflicts with the “real” strcpy. /* zstrcpy.c */ #include <stdio.h> #include <stdlib.h> /* Prototype: char *zstrcpy(char *dst, char *src); where "dst" is a pointer to the location that the string is to be copied to, "src" is a pointer to the location that the string is copied from. */
21
An Implementation of strcpy
char *zstrcpy(char *dst, char *src) { /* dst - pointer to destination string */ /* src - pointer to source string */ char *dststart = dst; /* Remember start of destination string */ /* Copy characters from source to destination */ while (*src != '\0') { *dst = *src; dst++; src++; } *dst = *src; /* Copy null character */ return(dststart); } /* End zstrcpy */
22
An alternative implementation
It is possible to shrink and obfuscate the code in an attempt to demonstrate ones C language machismo. This approach produces code that is difficult to read, painful to maintain, but may (or may not) produce a trivial improvement in performance . When tempted, just say no! /* zstrcpy.c */ #include <stdio.h> #include <stdlib.h> /* Prototype: Short, less readable version. char *zstrcpy(char *dst, char *src); where "dst" is a pointer to the location that the string is to be copied to, "src" is a pointer to the location that the string is copied from. */
23
An alternative implementation (cont'd)
char *zstrcpy(char *dst, char *src) { /* dst - pointer to destination string */ /* src - pointer to source string */ char *dststart = dst; /* Copy characters from source to destination */ while (*src++ = *dst++) != '\0') { } return(dststart); } /* End zstrcpy */
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.