Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Programming CE00312-1 Lecture 13 Strings in C.

Similar presentations


Presentation on theme: "Introduction to C Programming CE00312-1 Lecture 13 Strings in C."— Presentation transcript:

1 Introduction to C Programming CE00312-1 Lecture 13 Strings in C

2 A string is an array of characters terminated by a ‘\0’ character and is limited to the size of the array. e.g. char name[10]; can represent a string of maximum 9 characters (10 including ‘\0’) Initialisation: char word[15] = “example”; // ‘\0’ in addition, rest undefined

3 An array name or string name is a reference to an array of elements (called a pointer in C). Using a string as an entity (ie with a ‘\0’), as opposed to an array of individual characters, allows us to use the string facilities of C without thinking about the ‘\0’

4 Input and Output of Strings Output: use s format in printf e.g. printf(“%s”, word); Input: Two forms: 1)s format can be used in scanf egscanf(“%s”, name); // no ‘&’ required for a string reads all characters up to space or newline.

5 Input 2)gets reads a whole line as a string and then we can extract the data in the line individually using sscanf (note the extra ‘s’). gets returns NULL if there is no more data in the input (for the keyboard when control and ‘d’ are pressed together).

6 Example of using gets char line[81]; // maximum 80 chars + ‘\0’ char name[21]; int age; gets(line);// stores all characters // replaces ‘\n’ by ‘\0’ sscanf(line, “%s%d”, name, &age); scans the string, line, stores a string (terminated by a space) in name, and an integer in age, thus for the following input: Bailey 60 string name contains “Bailey” and age contains 60. Subsequent reading starts on the next line.

7 Standard String Functions #include “string.h” // required for using string functions strlen(s)// returns length of string, s - // not counting ‘\0’ For example: printf(“string %s is of length %d”, word, strlen(word));

8 More String Functions strcpy(s1, s2)// copies string s2 to s1, // but s1 = s2 is incorrect in C For example strcpy(word, name); strcat(s1,s2)// appends s2 on to end of s1, // but s1 must be big enough For example strcat(word, name); // word now contains “exampleBailey”

9 Note: strcpy and strcat also return s1 For example printf(“concatenation of %s and %s is %s”, word, name, strcat(word, name)); produces concatenation of example and Bailey is exampleBailey

10 Comparing Strings strcmp(s1, s2) // returns value of 0 if s1 the same as s2, // returns -ve value if s1 less than s2, // returns +ve value if s1more than s2, // we can’t use ==, For example if (strcmp(name, word) < 0) { printf(“%s is less than %s”, name, word); }

11 String example No. 1 Input 10 strings (each of at most 20 characters) into an array, sort them into alphabetical order and output them. #include “stdio.h” #include “string.h” // prototypes void read_strings(char [][21], int); void print_strings(char [][21], int); void sort(char [][21], int);

12 Main Function Array of 10 strings, list, must be declared in main. Array name, list, and its size, 10, must be passed to each function that uses it. int main(void) { char list[10][21]; // 20 chars each + ‘\0’ read_strings(list, 10); print_strings(list, 10); sort(list, 10); print_strings(list, 10); return 0; }// end main

13 read_strings function Array, s, is 2-dimensional array (only no. of columns, ie 21, declared) - each row is a string. Function, gets, reads a line (ie string) into row s[i]. void read_strings(char s[][21], int n) { int i; printf(“\ntype one string per line\n”); for (i=0; i<n; i++) {// read each line into row s[i] gets(s[i]); } }// end read strings

14 print_strings function Print each row of array, list, as a string using s format. void print_strings(char list[][21], int n) { int i; printf(“\nlist of strings is:\n\n”); for (i=0; i<n; i++) {// print each row list[i] as string printf(“%s\n”, list[i]); } }// end print strings

15 void sort(char a[][21], int n) {// Array, a, of strings int i, j; char temp[21];// temp string for (j = n-1; j > 0; j--) {// each pass of j comparisons for (i = 0; i < j; i++) {// each comparison if (strcmp(a[i], a[i+1]) > 0) // a[i] > a[i+1] {// swap the 2 strings strcpy(temp, a[i]); strcpy(a[i], a[i+1]); strcpy(a[i+1], temp); }// else no swap }// end of each pass }// end of all passes }// end sort


Download ppt "Introduction to C Programming CE00312-1 Lecture 13 Strings in C."

Similar presentations


Ads by Google