Download presentation
Presentation is loading. Please wait.
Published byBethanie Sullivan Modified over 8 years ago
1
STRINGS STRINGS IN C FUNCTIONS DEALING WITH CHARACTERS FUNCTIONS DEALING WITH STRINGS
2
Strings There is no string type in C. Instead, to approximate strings, we will use arrays of characters. To transform a simple array of characters into a string, it must contain the '\0‘ character in the last cell. Note that '\0' is one character. It is called the null character or the end-of-string character.
3
STRINGS IN C String and array Similarities: Both represent a collection of a fixed number of elements of the same type Elements of both array & string are located consecutively in the memory Name of the array is a pointer that points to the first element of the array Name of the string is a pointer that points to the first element of the string
4
STRINGS IN C String and array Differences: Array represents a collection of numerical values, Data types include int, float, double String represents a collection of characters Data type include only characters
5
String & Character: Character: a byte of data (ASCII CODE) char Letter='B';/* Letter is a variable of data type char */ String: a collection of characters char Str[ ]="BIRDS";/* Str is a string */ NOTE: 'A':a single character "A":a string containing 2 characters, 'A' and '\0'
6
How to Declare a String? 1. Need to have a name for the string 2. Need to know the length of the string 3. Actual memory space needed is length of the string + 1 (to take care of the NULL character) 4. Data type is char Example: char Str[20];
7
To declare a string and initialize it we could do it the same way as seen before: char city[ ] = {'T', 'o', 'r', 'o', 'n', 't', 'o', '\0'}; Notice that the size of the array can be omitted here, since the computer can deduce it. However, declaring the size (in this case 8), is always good form. But there is a simpler way: char city[ ] = “Toronto”; If you specify the size here, do not forget the invisible '\0'! Size must be 8 not 7! The result is exactly the same. By using the simpler way, the '\0' is added automatically at the end. 'T' ' o' 'r' 'o' 'n' 't' 'o' '\0‘ 0 1 2 3 4 5 6 7
8
“U" and ‘U' Double quotes " " represent a string, A single quote ' ', one character. ‘U' is the single character U But “U" is an array size 2 containing the ‘U' character and the '\0' character. All string constants are represented in double quotes (remember "This is my first C program." ?).
9
How to Initialize a String? /*PROGRAM # 94*/ /*DEMONSTRATES INITIALIZING STRING*/ /* length of the string is 5, need 5+1 memory space */ /* char Str[6]=”BIRDS”; */ #include main( ){ /* Declare and array with type char */ char str[ ] = "BIRDS"; printf("The string is %s.", str); } Note: The format specifier for a string variable is %s. char str [ ]="BIRDS": declare an array named str capable of storing characters initialize it to "BIRDS"
10
Inside the Memory char Str[6]=”BIRDS”; char Str[ ]=”BIRDS”; These 2 statements are equal.
11
Memory Map – Placement of a string within the memory AddressmemoryArray element 2400‘B’Str[0] 2401‘I’Str[1] 2402‘R’Str[2] 2403‘D’Str[3] 2404‘S’Str[4] 2405‘\0’Str[5]
12
NOTES!!! Str and &str[0] both refer to the top of the string (ie, address 2400). ‘\0’ is NULL character. ‘\0’ lets ‘C’ knows where the string ends. When we calculate the length of the string, '\0' is NOT counted. The length of the string is 5 but we need 6 bytes for the string (one extra for ‘\0’). Therefore, when declaring the string we need (LengthOfString+1) bytes.
13
Inside the Memory char Str[100]="BIRDS"; is acceptable, it means that we set aside 100 bytes in the memory for Str. Right now we only initialize the first 6 bytes but we expect that later on there will be some usage for the rest of them. Furthermore, we do not have any idea about the content of the Str[6] to str[99].
14
Placement of a string within the memory AddressmemoryArray element 2400‘B’Str[0] 2401‘I’Str[1] 2402‘R’Str[2] 2403‘D’Str[3] 2404‘S’Str[4] 2405‘\0’Str[5] 2406?Str[6] 2407?Str[7] 2408?Str[8] ………
15
/*DEMONSTRATES DISPLAYING STRING*/ /* PROGRAM # 95 */ #include #define ssize 6 main( ){ char str[ ] = "BIRDS"; int i; printf("Our string is %s. \n", str); /*Display characters string contains*/ puts(“Our string is made out of the following characters:”); for(i=0; i<ssize; i++) printf("%c\n ", str[i]); puts(“”); } AFTER EXECUTION: Our string is BIRDS. Our string is made out of the following characters: B I R D S
16
STRINGS STRINGS IN C FUNCTIONS DEALING WITH CHARACTERS FUNCTIONS DEALING WITH STRINGS
17
How to Declare a String? 1. Need to have a name for the string 2. Need to know the length of the string 3. Actual memory space needed is length of the string + 1 (to take care of the NULL character) 4. Data type is char Example: char Str[20];
18
Arrays of strings It is also possible to have arrays of strings. Since strings are themselves arrays, then we need to add a second size. An array of strings is in fact an array with two dimensions, width (columns) and height (rows). char list_cities[100][15]; will be able to contain 100 city names with a maximum of 14 letters per city. Why not 15?
19
Arrays of strings (cont.) Example: An array containing the names of the months. char months [12][10] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
20
Arrays of strings (cont.) Example: An array containing the names of the months. char months [12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; Can you find the value of months[3]? How about months [7][3]?
21
Using Pointers for String Manipulation /* PROGRAM # 96 */ /* DEMONSTRATES DISPLAYING STRING USING POINTER*/ #include #define ssize 5 main( ){ char *ptr, str[ ] = "BIRDS"; int i; ptr = str; printf("Our string is %s.\n", str); /*Display character string contains, using pointer */ puts(“Our string is made out of the following characters:”); for(i=0; i<ssize; i++) printf("%c ", *(ptr+i)); puts(“Our string is made out of the following characters:”); for(i=0; i<ssize; i++) printf("%c ", str[i]); }
22
Memory Map – Usage of pointer vs. index of string AddressmemoryPointer UsageString element 2400‘B’*(ptr+0)Str[0] 2401‘I’*(ptr+1)Str[1] 2402‘R’ *(ptr+2)Str[2] 2403‘D’*(ptr+3)Str[3] 2404‘S’*(ptr+4)Str[4] 2405‘\0’*(ptr+5)Str[5]
23
Initializing Strings Different ways: char str1 [ ]="BIRDS"; char str2 [6]="BIRDS"; char str3[ ]={'B', 'I', 'R', 'D', 'S', ‘\0' }; Manual initialization: str1[0]='B'; str1[1]='I'; str1[2]='R'; str1[3]='D'; str1[4]='S'; str1[5]='\0'; Necessary to indicate where string ends Input by user [scanf, gets] NOTE: Length of str1, str2, str3 are 5. NULL character does NOT count as part of string's length.
24
Getting a string from the user /* PROGRAM # 97 */ /* USE OF SCANF( ) TO READ STRINGS FROM USER */ #include main( ){ /*Declare a character array to hold input */ char str1[20]; printf("Enter a string: "); /*Read the string inputted*/ scanf("%s", str1); printf(“You just entered this string: %s”, str1); }
25
Filling a string We have already seen how to fill a string by initializing it in a declarative statement. Now how to do it in an executable statement. To fill a string from standard input (scanf) or file (fscanf), we use a special placeholder for strings %s. scanf ("%s", city); (Notice the absence of &. It is not necessary here since city is already an address, the same as &city[0], remember?)
26
Filling a string There is one problem with the %s placeholder when used with scanf (or fscanf). It considers the space as a delimiter. Therefore, if for a city name you enter Los Angeles, you will get only Los stored in the string. The solution is to use a function named gets (or fgets if you read from a file). That function only considers the new line character as the delimiter, not the space. Instead of scanf ("%s", city); you use gets (city); instead of fscanf (in, "%s", city); you use fgets(city, size, in);
27
Printing a string The space problem does not occur with printf. If the city is "Los Angeles" then a printf ("%s", city); will print out the entire city name. The puts function can also be used as an alternative to printf but its use is less necessary than the gets function.
28
Q? Ask the user to enter two different capital cities for two countries.
29
Q? /* Reading a string with scanf */ #include int main (void) { char city[20], city2[20]; printf ("What is the capital of Canada? "); /* the string is read with the %s placeholder */ /* do not use & or use &city[0] */ scanf ("%s", city); printf ("What is the capital of Russia? "); scanf ("%s", city2); /* here is the report */ printf ("\nThe capital of Canada is: %s.", city); printf ("\nThe capital of Russia is: %s.", city2); return (0); }
30
Note!!! 1. Size of the string MUST be indicated since we only declare the string 2.The absence of & in scanf should be noted (The string name represents the Starting address of the string)
31
Getting Strings Using gets( ) Function - /* PROGRAM # 98*/ /* USE OF SCANF( ) TO READ STRINGS FROM USER */ #include main(){ char str1[20]; puts("Please enter this string\”West By NorthWest!\”: "); scanf("%s", str1); printf(“You just entered this string: %s”, str1); }
32
Explanation: scanf( ) sees the blank character between 3 words, only gets the first word. Solution: Use gets( ) function
33
/* USE OF GETS( ) TO READ STRINGS FROM USER */ /* PROGRAM # 99 */ #include main(){ char str1[20]; printf("Enter a string: "); gets(str1); printf(“You just entered this string: %s”, str1); } After execution: Enter a string: North by NorthWest! You just entered this string: North by NorthWest!
34
The gets controversy Many experienced programmers will argue that gets is a dangerous function because you have to check manually that you don't run outside the array limits. For example, if you declare a string as char x[10];, use gets (x) and enter a sentence of 100 characters, it will accept it and write the extra characters into memory cells outside the array declaration paving the way for access violation and crashes. A safer way would be to use fgets all the time (use stdin as the file variable for keyboard input). Ex: fgets (x, sizeof(x), stdin); or fgets (x,10,stdin); Note that by using a size larger than the actual input string, a newline character will be part of the string. That does not happen with gets or scanf.
35
/* Reading a string from a file */ #include int main (void) { char sentence[200]; FILE *input; /* file is opened */ input = fopen("phrase.txt", "r"); /* the string is read from the file */ fgets (sentence, sizeof(sentence), input); /* sentence is echoed on the screen */ /* \" to display a " (double quotes) */ printf ("The sentence is:\n\"%s\"\n\n\n", sentence); /* file is closed */ fclose (input); return (0); }
36
Functions Dealing With Strings Defined in header Finding the length of a string[strlen] Copying a string to another one[strcpy]
37
strcpy strcpy: the function to transfer a string into a variable. Equivalent to the assignation operator. With strings you cannot do city="Toronto"; in an executable statement. To do an assignment we use strcpy. char city[10], city2[10]; strcpy (city, "Toronto"); /* places “Toronto” into city /* strcpy (city2, city); /* places “Toronto” into city2 /* ‘T’‘o’‘r’‘o’‘n’‘t’‘o’‘\0’ city
38
Copying A String to Another One o Copying A String to Another One o Strcpy(String1, string2) o Copies String2 into String1 o /* PROGRAM # 106 */ o /* USE OF STRCPY - STRNCPY*/ o #include o main( ){ o /* Declare character arrays */ o char S1[100], S2[100]; o char S3[100]=”These two strings will have to be the same!”; o /* Copy S3 into S1 */ o strcpy(S1, S3); o printf(“This is the 1st string \”%s\”.\n”, S1); o printf(“This is the 2nd string \”%s\”.\n”, S3); o puts(“”); o /* Copy the S3 into S2 */ o strcpy(S2, S3); o printf(“This is the 1st string \”%s\”.\n”, S2); o printf(“This is the 2nd string \”%s\”.\n”, S3); o }
39
Copying A String to Another One /* OUR VERSION OF STRCPY */ … for(i=0; i<strlen(S2); i++) S1[i]=S2[i]; S1[i]=NULL; … OR … i=0; while( S2[i] != ‘\0’) S1[i]=S2[i]; i++; } S1[i]=NULL; …
40
Finding the Length of the String The following are string functions strlen(string) returns an integer as the length of a string. Note!!!: NULL character not counted. strlen: the strlen function returns the length of the string not counting the null character. char city[10]; strcpy (city, “Toronto”); /* places “Toronto” into city */ printf (“%d”, strlen (city)); /* displays 7 on the screen */
41
/* USE OF STRLEN */ /* PROGRAM # 105 */ /* USE OF STRLEN */ #include main( ){ char Str[ ] = "I wonder how strlen function works?!"; int Slen; /* Call the function strlen( ), to assign the length of the string */ Slen=strlen(Str); printf( "Your string is %s and it has %d characters.", Str,Slen); } /* Our version of STRLEN */ … i=0; while( Str[i] != ‘\0’) i++; …
42
String function: strcpy There are many useful functions to manipulate strings in the string library (need #include ). strcpy: the function to transfer a string into a variable. Equivalent to the assignation operator. With strings you cannot do city="Toronto"; in an executable statement. To do an assignment operation we must use strcpy. char city[10], city2[10]; strcpy (city, "Toronto"); /* places "Toronto" into city /* strcpy (city2, city); /* places "Toronto" into city2 /* 'T''o''r''o''n''t''o''\0'?? city2 0 1 2 3 4 5 6 7 8 9
43
String function: strlen strlen: the strlen function returns the length of the string not counting the null character. char city[10]; strcpy (city, "Toronto"); /* places "Toronto" into city */ printf ("%d", strlen (city)); /* displays 7 on the screen */
44
Having an array as a “result” An array can never be a result as it represents multiple values. As with function with multiple “results”, we will have to use pointers. void addarrays (const int a[], const int b[], int c[], int size) { int i; for (i=0; i<size; ++i) c[i] = a[i] + b[i]; } The const option means that the specified array cannot be modified by the function.
45
Dynamic allocation of arrays Arrays, as we know them now, can only be of a fixed size. int x[100]; declares an array size 100, no less no more. It is illegal to do the following. Do you know why? printf ("Enter the size of the array: "); scanf ("%d", &size); int x[size];
46
C’s Dynamic allocation functions Pointers provide necessary support for C’s dynamic allocation system. Dynamic allocation is the means by which a program can obtain memory while it is running. memory allocated by C’s dynamic allocation functions is obtained from the heap. the heap is free memory that is not used by your program, the operating system, or any other program running in the computer. The size of the heap cannot usually be known in advance, but it typically contains a fairly large amount of free memory.The core of C’s allocation system consist of the functions: (Memory allocation) malloc () allocates memory free() realease memory (contiguous allocation) calloc() allocate memory
47
Heap memory It is impossible to have arrays of varying sizes because that makes programming inefficient. C is very strict with arrays but it is also the most efficient of all the high-level languages. There is a way to have dynamic allocation in C. The solution is to use “heap” memory instead of the usual memory we have been using so far. Heap memory: slower, less-structured,dynamic memory. The heap is a region of free memory that your program can use via C’s dynamic memory allocation functions.
48
DYNAMIC ARRAYS IDEA: Create an array at run time Declare a pointer by using either calloc( )[void*calloc( arg1, arg2)] arg1: # of elements in an array, arg2: bytes needed for one element (both functions are part of )
49
Note!!! The calloc() function returns a pointer to the first byte of the allocated region.
50
void *calloc(size_var1, size_var2) Returns a pointer that points to a top of the array whose number of elements is the first argument and the size of each element is the second argument. All elements are initialized to zero. calloc() allocate memory the size of which is equal to num*size. allocate sufficient memory for an array of num objects of size. All bits in the allocated memory are initially set to zero. Void free(void *var) Free a memory block whose starting address is the argument. This block should have been previously allocated by calloc.
51
Using a dynamic array int size; /* 1. declare a pointer to the array */ double *array; printf (“Enter the size of the array: “); scanf ("%d", &size); /* 2. allocate the array in the heap */ array = (double *) calloc (size, sizeof(double)); /* 3. use the array normally */... /* 4. free the heap memory */ free (array);
52
Dynamic allocation of arrays Arrays, as we know them now, can only be of a fixed size. int x[100]; declares an array size 100, no less no more. It is illegal to do the following. Do you know why? printf ("Enter the size of the array: "); scanf ("%d", &size); int x[size];
53
Dynamic Allocation of Arrays It is impossible to have arrays of varying sizes because that makes programming inefficient. C is very strict with arrays but it is also the most efficient of all the high-level languages. There is a way to have dynamic allocation in C. The solution is to use “heap” memory instead of the usual memory we have been using so far. Heap memory: slower, less-structured, dynamic memory.
54
Using a dynamic array int size; /* 1. declare a pointer to the array */ double *array; printf ("Enter the size of the array: "); scanf ("%d", &size); /* 2. allocate the array in the heap */ array = (double *) calloc (size, sizeof(double)); /* 3. use the array normally */... /* 4. free the heap memory */ free (array); See the complete program at c.ihypress.ca/07.php (program #9)
55
USE OF CALLOC( ) Same thing can be achieved using another standard memory function, i.e., calloc( ): … int *arr; arr = (int *) calloc ( array_size, sizeof(int) ); … NOTE: If the requested space is not available, it returns NULL.
56
/* A dynamically-allocated 1-D array */ #include int main (void) { double *x; /* declare a pointer only */ int i, size; /* ask user for size of array */ printf ("How large do you want your array? "); scanf ("%d", &size); /* allocate the array in the heap */ x = (double *) calloc (size, sizeof(double)); /* printing the array for verification surprise! a dynamic array is automatically initialized with zeros! */ for (i = 0; i < size; ++i) printf ("%.1f ", x[i]); /* freeing the memory allocation */ free (x); return (0); }
57
DYNAMIC MEMORY ALLOCATION – HOMEWORK!!! WRITE A C PROGRAM THAT ASK THE USER FOR THE SIZE OF AN INT ARRAY DYNAMICALLY CREATE THE ARRAY GET VALUES FOR THE ARRAY CALCULATES THE MINIMUM, MAXIMUM CALCULATES THE AVERAGE PRINT THE RESULTS
58
Q?HOMEWORK!!! Write a program to create an array of integers (LIST) dynamically. User provides the size of the array.
59
Passing Strings between Functions Same way that we used arrays as function arguments /* PROGRAM # 100 */ /*GETS STRING FROM THE USER, RETURN THE STRING*/ #include void GetName(char str[ ]); void PrintName(char str[ ]); main( ){ /* declare a character array to hold input */ char name[20]; /*Call functions to input the character from the user and display it */ GetName(name); PrintName(name); } /* To get the string */ void GetName(char str[ ]){ printf("What is your name (max 20): "); gets(str); } /* To print string */ void PrintName(char str[ ]){ printf("Your name is %s.", str); }
60
Passing a string to a function Passing a string to a function is the same thing as passing an array of numerals. We pass only the pointer to its first cell. On the next slide is a function that takes in a string and counts the number of vowels (a,e,i,o,u) in it. Notice that contrary to numerical arrays, we don't need to pass the size because for strings it is easy to determine its size from inside the function (which is not possible for arrays of numerals).
61
Passing a string to a function int count_vowels (char string[]) { int nv=0, i; for (i=0; i<strlen(string); ++i) if (string[i]=='a'||string[i]=='e'|| string[i]=='i'||string[i]=='o'|| string[i]=='u') nv = nv + 1; return (nv); }
62
Passing a string to a function The main program would look like this: int main (void) { char s[100]; int vowels; printf ("Enter a sentence: "); gets (s); vowels = count_vowels (s); printf ("The sentence contains %d vowels.\n", vowels); return (0); }
63
2D Array Applications 1D arrays can be viewed as VECTORS. Similarly, 2D arrays can be viewed as MATRICES. As such, when we are dealing with 2D arrays we really trying to manipulate Matrices including: Matrix addition Matrix subtraction Matrix multiplication Matrix transposition Matrix inversion Solving for systems of linear equations Linear transformations
64
Regualr Array – Example Write a program that gets a vector from the user. Assume that the elements of the vector are float and the max size of the vector is 3.
65
/* PROGRAM # 900 */ #include void GetVector(float A[3]); void PrintVector(float A[3]); main( ){ float Vector[3]; GetVector(Vector); PrintVector(Vector); } /* To input a Vector with max size 3 from the user*/ void GetVector(float A[3]){ int i; for(i=0; i<3; i++){ printf("Please enter A[%d]", i); scanf("%f", &A[i]); } /* To display the 3 Vector */ void PrintVector(float A[3]){ int i; for(i=0; i<3; i++){ printf("%5.2f", A[i]); printf("\n"); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.