C Programming Language - Strings
C Programming Language - Strings
Computer Programming
Strings
Introduction
String: group of characters
Implemented as arrays of char Essential for several applications manipulating textual data
Word processing Databases Scientific computing (Ex: DNA sequence, chemical compounds)
String Variables
char string_var[30];
String variables can be initialized char string_var[30] = initial value; char str[] = initial value; What is the size of str? The part of array after null character is ignored
GIT Computer Engineering Department
Arrays of Strings
Input/Output of Strings
Input/Output of Strings
Place holder: %s scanf can used to input strings scanf(%s, string_var);
Remember string_var is an array
scanf
skips leading whitespace characters copies subsequent characters in memory cells copying stops when a whitespace character is seen places a null character at the end of string
Input/Output of Strings
Input/Output of Strings
Input/Output of Strings
String Assignment
strcpy
strcpy(str, test value);
Safer to use
strncpy(str, A very long string test value, 19); str[19] = \0;
GIT Computer Engineering Department
Substring copy
char result[10], s1[15] = Jan. 30, 1996; strncpy(result, s1, 9);
Substring copy
char result[10], s1[15] = Jan. 30, 1996;
strncpy(result, &s1[5], 2);
Substring copy
char result[10], s1[15] = Jan. 30, 1996;
strcpy(result, &s1[9]);
Use strncpy
to copy parts of compound names into elemental components
Use strlen
To check termination of the loop
Concatenation
Add a string at the end of the other string strcat and strncat
Assumes sufficient space available for the concatenated string
char f[15] = Fatih , m[15] = Erdogan , l[15] = Sevilgen; strcat(f, l); strcat(m, l); printf(%d %d \n, strlen(m), strlen(l)) strncat(m, l, 5); printf(%d \n, strncat(m, l, 15 - strlen(m) - 1));
GIT Computer Engineering Department
Do not stop at space or tab characters Do not store end-of-line (new-line, return, enter) character char line[80]; gets(line);
Ex: Scans a data file and create a new doublespaced version with line numbers
String Comparison
Arrays of Pointers
Three copy operations per exchange
Previous example requires a lot of copying of characters to sort a list of strings Alternative approach: use arrays of pointers
Pointers to strings (arrays) Sort the pointers not the strings Saves the original order as well.
Arrays of Pointers
char orignal[5][10]; for(i = 0; i < 5; ++i) printf(%s\n, alphap[i]); for(i = 0; i < 5; ++i) printf(%s\n, original[i]);
Arrays of Pointers
Arrays of pointers has several advantages
Can represents many orderings
All refers to the same string One corrected all corrected
Arrays of Pointers
EX: Input a list of names and access it in sorted order and original order.
Character Operations
Include ctype.h
Strings processing usually requires character manipulation Character library provides several functions
ch = getchar();
scanf(%c,&ch);
getc: get a single character from a file putchar and putc are used to display a character
putchar(a); putc(a,outp);
GIT Computer Engineering Department
String-Number Conversion
String-Number Conversion
String-Number Conversion
string containing day month name and year three integers (day month year)
(12 6 1968)
Convert a string representation of date to three integer representation and vice versa
Analysis:
Keep the source line to edit Get the operation until it is Q
Data Requirements
source array command