Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 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 C-strings

2 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 string “Hi Mom!”, it will be stored as:

3 String Initialization Can initialize string: char myMessage[20] = "C is fun!"; Need not fill the entire array Initialization places '\0' at the end

4 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'};

5 Accessing elements of a string A string IS an array; therefore, Can access indexed variables of: char ourString[7] = "Hello"; ourString[0] is 'H' ourString[1] is 'e' ourString[2] is 'l' ourString[3] is 'l' ourString[4] is 'o'; ourString[5] is ‘\0’ ourString[6] is unknown

6 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!

7 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);

8 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);

9 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 Use of pointers in processing C-strings.

10 = 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"); Built-in function (in ) Sets value of aString equal to "Hello" NO checks for size! – Up to programmer, just like other arrays!

11 == 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");

12 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

13 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!

14 string Functions In the following example, we will see how strcpy might be implemented. Its prototype is shown below. char *strcpy(char *s1, const char *s2);

15 The name zstrcpy s used to avoid potential nastygrams and name conflicts with the “real” strcpy. /* zstrcpy.c */ #include /* 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. */ An implementation of strcpy

16 char *zstrcpy(char *dst, char *src) { /* pointer to destination string */ /* 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 */ An Implementation of strcpy

17 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 /* 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. */ An alternative implementation

18 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 */ An alternative implementation (cont'd)

19 We will explore the use of separately compiled modules later in the semester, but for now we’ll consider some simple cases. The nice thing about functions like zstrcpy() is that they can be used by multiple programs. For this to be convenient, we do not want to have to include the source code for zstrcpy() in every program we write that will use it. /* zstrtest.c */ #include Separately compiled modules

20 /* Prototype statement for zstrcpy() */ char *zstrcpy(char *s1, char *s2); /* Test program */ int main() { char *string1 = "This is a test string"; char *string2; string2 = (char *) malloc(strlen(string1)+1); zstrcpy(string2, string1); printf("Copied string: \"%s\"\n", string2); exit(0); } Separately compiled modules

21 Note that this program calls the zstrcpy() function. The prototype statement at the head of the main program (highlighted in red) is provided so that the compiler will know what the type of the zstrcpy() function is and what its parameters are. One way to compile and run this program is the following command line: gcc –o zstrtest –g –Wall zstrtest.c zstrcpy.c Note that both source files are specified, which means that both source files will be compiled to build the final executable program “zstrtest”. Separately compiled modules

22 Also note in this program the use of the statement #include This was necessary because the main program calls the string function "strlen()", and its prototype statement is in the include "string.h". For any given C function you can determine which include files are needed by entering a command, e.g. man strlen. This produces the "man" page description of the function which includes the list of include files that must be specified. Separately compiled modules


Download ppt "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,"

Similar presentations


Ads by Google