Title: Introduction to C Programming CE003121
1Introduction to C ProgrammingCE00312-1
2Strings in C
- A string is an array of characters terminated by
a \0 character and is limited to the size of
the array. - e.g. char name10
- can represent a string of maximum 9 characters
- (10 including \0)
- Initialisation
- char word15 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
4Input 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
- eg scanf(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).
6Example of using gets
- char line81 // maximum 80 chars \0
- char name21
- int age
- gets(line) // stores all characters
- // replaces \n by \0
- sscanf(line, sd, 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.
7Standard 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))
8More 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
10Comparing 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 cant use , lt , gt
- For example
- if (strcmp(name, word) lt 0)
-
- printf(s is less than s, name, word)
-
11String 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)
12Main 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 list1021 // 20 chars each \0
- read_strings(list, 10)
- print_strings(list, 10)
- sort(list, 10)
- print_strings(list, 10)
- return 0
- // end main
13read_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
si. - void read_strings(char s21, int n)
-
- int i
- printf(\ntype one string per line\n)
- for (i0 iltn i)
- // read each line into row si
- gets(si)
-
- // end read strings
14print_strings function
- Print each row of array, list, as a string using
s format. - void print_strings(char list21, int n)
-
- int i
- printf(\nlist of strings is\n\n)
- for (i0 iltn i)
- // print each row listi as string
- printf(s\n, listi)
-
- // end print strings
15void sort(char a21, int n) // Array, a,
of strings int i, j char temp21 // temp
string for (j n-1 j gt 0 j--) // each
pass of j comparisons for (i 0 i lt j
i) // each comparison if (strcmp(ai,
ai1) gt 0) // ai gt ai1 // swap
the 2 strings strcpy(temp, ai)
strcpy(ai, ai1)
strcpy(ai1, temp) // else no
swap // end of each pass // end of all
passes // end sort