Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.

Similar presentations


Presentation on theme: "CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004."— Presentation transcript:

1 CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004

2 Introduction to Strings strings are how we store and manipulate text in C strings are basically arrays of char strings are not a data type in C but C does have some library functions for manipulating strings you can declare an array of chars like this: char carray[27]; just like any array, we can initialise an array of chars: char carray[27] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’};

3 Initialisation - Array Notation We have already seen how to initialise arrays. Let us re-examine this in more detail. Rules of array initialisation we can have as many initialisers as we have array elements we can have fewer, but not more if we have fewer, the remaining elements get set to 0 So for example double values[4] = {1.1, 2.2}; will initialise an array of four doubles with the values 1.1, 2.2, 0.0 and 0.0

4 String Literals C has a convention about how strings are stored. An extra character is added to the end of the string - the null character – ‘\0’. “Hello” is stored as ‘H’,’e’,’l’,’l’,’o’, and ’\0’ String literals, sometimes called string constants, are a sequence of zero or more characters enclosed in double quotes. – like this: “xyz” or “Hello, world!\n” we’ve already used these a lot printf(“Hello, world!\n”);

5 Pointer Notation C gives us two ways to declare strings: char astring[] = “Hello”; or char *pstring = “Hello”; The second version is the most common. A pointer to a char is declared and intiialised with a string literal. This can subsequently be processed as an array or pointer, e.g. to access the second element ‘e’ you can use pstring[1] or *++pstring.

6 Array of Strings We can have an array of strings: char *dayName[] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}; this creates an array of 7 elements of type char* each element is initialized with a pointer to a string literal here’s an example printf (“%s\n”, dayName[0]); which displays Sunday

7 The strlen() function Very often you need to find the length of a string. There is a function strlen() declared in string.h that can do this: #define MAXSTRING 80 char string[MAXSTRING]; int length; printf(“Enter a string: “); scanf(“%s”, string); length = strlen(string); printf(“You entered %d characters.\n”, length);

8 String functions (1) Calculate string length, strlen(s1) Counts characters before the ‘\0’ Concatenate two strings, strcat(s1, s2) Result stored in s1 Copy a string, strcpy(s1, s2) Result stored in s1 Concatenate two strings (char limit), strncat(s1, s2, x) Concatenates at most x characters - result stored in s1 Copy a string (char limit), strncpy(s1, string, x) Copy at most x characters - result stored in s1

9 String functions (2) Compare two strings, strcmp(s1, s2) Returns number less than 0 if s1 s1, returns 0 if s1 is equal to s2 Compare two strings (max char), strncmp(s1, s2, x) Compares first x characters, otherwise as strcmp() Search for a character in a string, strchr(s1, c) Searches for the first occurrence of c in s1. Starts on the left. If found, returns address. If not found, returns NULL Search for a string within a string, strstr(s1, s2) Searches for the first occurrence of s2 in s1 Starts on the left. If found, returns address. If not found, returns NULL

10 Example: Array of Strings int FindMonthNum (char *monthName){ char *month[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”}; for (i = 0; i < 12; ++i) if (strcmp (monthName, month[i]) == 0) return i+1; return 0; }

11 The null string The null string is legal C char *c = ""; And it's also quite useful, e.g. in a program dealing with plurals: char *plural_ending; int num_frogs;... plural_ending = (num_frogs==1)? "": "s"; printf("We found %d frog%s in the pond", num_frogs, plural_ending);


Download ppt "CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004."

Similar presentations


Ads by Google