Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings, Pointers and Tools

Similar presentations


Presentation on theme: "Strings, Pointers and Tools"— Presentation transcript:

1 Strings, Pointers and Tools
Programming Lab (Lab5) Strings, Pointers and Tools

2 Overview A string is a sequence of characters. It is a kind of data.
A literal string is a sequence of characters enclosed by a pair of double quotes " ". A string is stored as an array of char. Strings are often processed using pointers. The C standard library provides many useful string handling functions.

3 Basic String Concept: Literal String
#include <stdio.h> int main(void) { printf("Hello, %s, morning!\n", "C Program"); return 0; } Hello, C Program, morning! NULL terminator is added automatically at the end of the literal string. Address 200 201 202 203 204 205 206 207 208 209 Position 1 2 3 4 5 6 7 8 9 Character C P r o g a m \0 Note Characters in the literal string NULL

4 Basic String Concept: Literal String
A literal string is in fact a constant array of constant characters. The location (address) as well as the content of the array is fixed, i.e. “hello”. This is similar to constant like 123. Modifying the content of a literal string in run-time will CRASH the program! (See the exercise follows…)

5 Creation of Literal String
When we write a C program, a pair of bare double quotes represents a literal string, e.g., “hello”. The compiler and the linker allocate memory for storing the literal string automatically , excluding the double quotes (optional). Following the rule, there is an extra character reserved automatically and put right after the literal string content, the NULL (ASCII code 0) character. The NULL terminator in fact signals the end of the string since C does NOT keep the length of the string! (strlen())

6 Revision: char Array A char array can be initialized with
a sequence of characters (in pairs of single quotes); a sequence of ASCII codes. The array size can be specified explicitly OR implicitly. The characters are stored consecutively. The array content can be modified in run-time.

7 Basic String Concept: char Array
NULL terminator is added manually. #include <stdio.h> int main(void) { char name[15] = {'C',' ','P','r','o','g','r','a','m','\0'}; char greet[] = {109, 111, 114, 110, 105, 110, 103, 0}; printf("Hello, %s, %s!\n", name, greet); return 0; } Hello, C Program, morning! NULL terminator is added manually. Address 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 name[i] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Character C P r o g a m \0 ? ASCII code 67 32 80 114 111 103 97 109 Note Characters in the string (char array) name NULL Unused space in the array

8 Char Array Usage Tips The array size should be large enough for storing the character content (trivial!). E. g., char a[2]=“hello”; We should reserve space and put the NULL terminator at the end of the string. E.g., manually: char name[15] = {'C',‘ ','P','r','o','g','r','a','m','\0'}; Drawback: there may be unused space left if the declared array is too large.

9 Initializing a char Array with a Literal
Initializing a char array (string variable) with a literal string (constant). #include <stdio.h> int main(void) { char name[15] = "C Program"; printf("Hello, %s, morning!\n", name); return 0; } Hello, C Program, morning! Note that there is already a NULL terminator in the literal string. Address 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 name[i] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Character C P r o g a m \0 ? Note Characters in the string (char array) name NULL Unused space in the array

10 Initializing a char Array with a Literal
It looks like "String Copy." This is in fact initialization of an array during variable declaration! We CANNOT do assignment with an array.

11 Exercise #include <stdio.h> int main(void) {
char name[15] = "C Program"; name[1] = '-'; name[6] = 0; printf("Hello, %s, morning!\n", name); return 0; } Hello, C-Prog, morning! // Modify the content of the array name

12 Exercise #include <stdio.h> int main(void) {
char myName[15] = "C Program"; char yourName[30]; yourName = myName; printf("Hello, %s, morning!\n", yourName); return 0; } SYNTAX ERROR!!! // Whole Array Assignment!

13 Exercise #include <stdio.h> int main(void) {
char * ptr = "C Program"; ptr[1] = '-'; ptr[6] = 0; printf("Hello, %s, morning!\n", ptr); return 0; } CRASH!!! // Modify the content of the literal string which is constant!

14 Exercise #include <stdio.h> int main(void) { char name[15] = "C Program"; char * ptr; ptr = name; // Equivalent: ptr = &name[0] // i.e. ptr points to name[0] ptr[1] = '-'; ptr[6] = 0; printf("Hello, %s, morning!\n", name); printf("Bye bye, %s!\n", ptr); return 0; } Hello, C-Prog, morning! Bye bye, C-Prog!

15 String Handling Functions
The standard library contains many useful string handling functions. They all require that strings passed as arguments be NULL-terminated. and well-allocated. Their function prototypes are given in the header file <string.h>.

16 Determining String Length
size_t strlen( const char s[] ) Returns the number of characters (not including the terminating NULL character '\0') in the string s char str1[10] = "ABC"; char str2[] = "This is a string!"; int len; len = strlen(str1); // coerce from size_t to int printf("%d\n", len); // prints 3 printf("%d\n", strlen(str2)); // prints 17 printf("%d\n", strlen("Hello")); // prints 5

17 Displaying Strings – puts()
int puts( char s[] ) Print the string s followed by a newline character. What's the return value of puts()? char s[] = "This is a string!"; puts(s); // Same as printf("%s\n", s);

18 Getting Strings – scanf()
#include <stdio.h> int main(void) { char s1[20], s2[20]; scanf("%s", s1); printf("s1=%s\n", s1); printf("\n"); scanf("%s", s2); printf("s2=%s\n", s2); return 0; } this-is-first-string. s1=this-is-first-string. this is second string. s2=this & When inputting a string using scanf(), the argument(s) does NOT need to be preceded by '&'. W H Y ? White spaces (such as space, tab) are regarded as delimiters by scanf(). &

19 Getting Strings – gets()
char * gets( char s[] ) Input characters from the standard input into the array s until a newline or the End-Of-File character is encountered. A terminating null character is appended to the array and the array s (i.e., the address of s[0]) is returned. char name[20]; printf("Your name? "); gets(name); // we ignore the return value printf("Hello %s!\n", name); Your name? C Program Hello C Program!

20 Copying strings Copies s2 (including the NULL character) into s1
char * strcpy( char s1[], const char s2[] ) Copies s2 (including the NULL character) into s1 We should ensure s1 be large enough to store s2 (including the NULL character) String s1 (i.e. the address of s1[0]) is returned char s1[7] = "123456", s2[7] = "ABC"; strcpy(s1, s2); // Output ABC in s1 printf("%s\n", s1); s1 1 2 3 4 5 6 '\0' s2 A B C '\0' ? s1 becomes A B C '\0' 5 6 Copy up to the NULL character. The remaining characters in s1 are untouched.

21 Copying strings (with an optional size)
char * strncpy( char s1[], const char s2[], size_t n ) If s2 has n or more characters, the function copies the first n characters from s2 into s1. NO NULL character is appended to s1. If s2 has less than n characters, the function behaves like strcpy(). That is, it copies up to the NULL character.

22 strncpy() (Examples) char s1[7] = "123456", s2[7] = "ABC";
strncpy(s1, s2, 2); // output AB3456 printf("%s\n", s1); strncpy(s1, s2, 10); // output ABC strncpy(s2, "12345", 4); // Warning! s2 may NOT be // NULL terminated! s1 1 2 3 4 5 6 '\0' s2 A B C '\0' ? s1 becomes A B 3 4 5 6 '\0' s1 becomes A B C '\0' 5 6 s2 becomes 1 2 3 4 ?

23 Concatenating strings
char * strcat( char s1[], const char s2[] ) Appends s2 to s1. First character of s2 replaces the terminating NULL character of s1. We should ensure s1 is large enough to store the concatenated string and a NULL character. s1 is returned, s2 is untouched. char s1[10] = "ABC"; char s2[20] = "123456"; strcat(s1, s2); // s1 becomes "ABC123456" strcat(s2, "789"); // s2 becomes " "

24 Comparing strings Zero if both strings are equal
int strcmp( const char s1[], const char s2[] ) Compares s1 and s2 character-by-character (up to the NULL character) based on their ASCII values Returns Zero if both strings are equal A negative integer if s1 precedes (less than) s2 A positive integer if s1 succeeds (greater than) s2 char s1[10], s2[20]; scanf("%s %s", s1, s2); if ( strcmp(s1, s2) == 0 ) printf("Both strings are identical");

25 Comparing strings s1 s2 strcmp(s1,s2) 0, -ve, or +ve? "ABCD" "ABC"
"" "123" "0178" s1 A B C D '\0' s2 A B C '\0' ? s1 A B C '\0' ? s2 a b c '\0' ? s1 A B ' ' C '\0' s2 A B C '\0' ? s1 '\0' ? s2 A B C '\0' ? s1 1 2 3 '\0' ? s2 1 7 8 '\0'

26 Other Useful Functions
#include <stdlib.h> int atoi( const char str[] ) long atol( const char str[] ) The functions retrieve the longest leading substring in str that represents a valid integer, and returns the equivalent value as a value of type int/ long. Leading white-space characters are ignored. int a; a = atoi("1234"); // a becomes 1234 a = atoi(" \n\n3123"); // a becomes 3123 a = atoi("ABC10"); // a becomes 0 a = atoi("-1+1"); // a becomes -1 a = atoi("99.8e3"); // a becomes 99

27 Other Useful Functions
#include <stdlib.h> double atof( const char str[] ) The function retrieves the longest leading substring in str that represents a valid floating point number, and returns the corresponding value as a double. Leading white-space characters are ignored. double x; x = atof("3.14"); // x becomes 3.14 x = atof(" \n\n3.4"); // x becomes 3.4 x = atof("ABC10"); // x becomes 0.0 x = atof("-.344"); // x becomes x = atof("1.2e3"); // x becomes x = atof("1.4.4"); // x becomes 1.4

28 Remark There are many other string handling functions in the standard library. Check the documentation to see if an existing function suits your need before attempting to write your own string handling functions. Remember, browsing through reference manuals is the essential technique that every C programmer should equip with.

29 Strings and Pointers #include <stdio.h>
#include <string.h> int main(void) { char message[] = "Welcome to C!"; char *cptr; cptr = message + 11; printf("%s\n", cptr ); printf("%s\n", &message[11] ); return 0; } C!

30 Memory Demonstration message cptr cptr 1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 message w e l c o m t C ! \0 ? cptr cptr C ! \0 ?

31 Practice 1--Common Array
Two arrays are identical if they have the same elements in the same order. Algorithm for testing if two arrays, A and B, are identical: If both arrays have different number of elements, then the array are different. Otherwise, compare elements between A and B one-by-one. If one of the elements is different, then the arrays are different. If none of the elements between A and B are different, then the arrays are identical.

32 // Suppose A and B are two arrays of the same type
// with n and m elements respectively. int isIdentical = 1; // Assume A and B are identical // this is a boolean FLAG (T/F) if (n != m) isIdentical = 0; // set the flag to FALSE else { int i; for (i = 0; i < n; i++) if (A[i] != B[i]) { isIdentical = 0; // set the flag to FALSE break; // stop the loop, save time! } if (isIdentical) printf("A and B are identical.\n");

33 Practice 2– Substring Search
Locate a substring (s1) in a string (s2). i.e., At which location does s1 first appear in s2? 1 2 3 4 5 6 7 8 s1 B C '\0' s2 A B C D E F '\0' ? 1st iteration s1 B C '\0' s2 A B C D E F '\0' ? 2nd iteration s1 matches a substring starts at 1 s1 B C '\0' s2 A B C D E F '\0' ? =

34 Substring Search // Returns the location of “substr" in "str".
// Returns -1 if "str" does not contain “substr". int locate( const char str[], const char substr[] ) { int i, j, len1, len2; len1 = strlen(str), len2 = strlen(substr); // For all possible starting locations for (i = 0; i <= (len1 – len2); i++) { int isIdentical = 1; // For each substring started at location i for (j = 0; j < len2; j++) if ( substr[j] != str[i+j] ) { isIdentical = 0; break; } if (isIdentical) // Return the 1st matching substr return i; return -1; Version 1 Substring Search

35 Substring Search using strncmp()
// Returns the location of “substr" in "str". // Returns -1 if "str" does not contain “substr". int locate( const char str[], const char substr[] ) { int i, j, len1, len2; len1 = strlen(str), len2 = strlen(substr); // For all possible starting locations for (i = 0; i <= (len1 – len2); i++) { if ( strncmp(substr, &str[i], len2) == 0 ) return i; } return -1; Substring Search using strncmp() Version 2 // Returns the address of the first occurrence of the // substring in the text. Returns NULL on not found. char * p; p = strstr("This is a text.", "is"); => p ≡ "is is a text." Substring Search using strstr() Version 3

36 Challenging Yourselves Now
Today you face a white board but you have the key parts. No templates today !!!


Download ppt "Strings, Pointers and Tools"

Similar presentations


Ads by Google