Download presentation
Presentation is loading. Please wait.
1
Lecture 10: Strings B Burlingame 4 April 2018
2
Announcements Midterm due next week Read chapter 9
You may work on your midterm projects in lab, this week. No new lab will be assigned No group discussion and only one person per PC Remember: the midterm must be individual work. Any duplicate work will be harshly dealt with.
3
Recall: What is an Array?
So far we've dealt with scalar variables contain just one value Arrays are collections of data of the same type that occupy contiguous memory locations Individual values in the collection are called elements of the array Need to declare before use: #include <stdio.h> int main() { int i=0; double test[4] = {0}; test[0]=95.5; test[1]=74.0; test[2]=88.5; test[3]=91.0; for(i=0; i<4; i++) printf("test[%d]=%lf",i,test[i]); 0x%p\n",&test[i]); } return 0; Run array_practice2.c in ChIDE. Format (1D array) type array_name [num_elements]; Ex. Array of 4 doubles named, 'test'
4
What is an array? - 2 int nums [10]; 10 element array of integers
Element no. 3 is accessed by: nums [2] because indexing begins at 0
5
Accessing Array Elements
#include <stdio.h> int main() { int i=0; doube test[4]={0}; test[0]=95.5; test[1]=74.0; test[2]=82.75; test[3]=91.5; for(i=0; i<4; i++) printf("test[%d]=%lf",i,test[i]); 0x%p\n",&test[i]); } return 0; Individual elements of an array are accessed by their index number ** Important Note** Array indexing starts at zero (take note of this, it is easy to forget!) char test[4]; the first element is test [0] the fourth element is test [3] Be careful! The C compiler may not stop you from indexing beyond the boundary of your array. (What could happen?) What is test[4]? array_practice2.c
6
Initializing Array Elements
/* array_practice3.c */ #include <stdio.h> int main() { int i=0; int nums[5]={0,1,2,3,4}; for(i=0; i<5; i++) printf("nums[%d]=%d",i,nums[i]); 0x%p\n",&nums[i]); } return 0; Use braces to enclose the elements Separate elements by commas Can set number of elements in declaration: Explicit: int nums[5] Implicit: int imp[ ] = {1, 2}; Run array_practice3.c in ChIDE. Point out addresses for int array elements. Four bytes each! Run array_practice4.c in ChIDE to show my version of keyboard entry.
7
Initializing Array Elements
/* array_practice3.c */ #include <stdio.h> const int BIG_INDEX = 5000; int main() { int i=0; int nums[BIG_INDEX]={0}; for(i=0; i < BIG_INDEX; i++) nums[i] = 34; } return 0; For long, non-zero initializations, for loop is usually more efficient to program Note the use of a variable index Run array_practice3.c in ChIDE. Point out addresses for int array elements. Four bytes each! Run array_practice4.c in ChIDE to show my version of keyboard entry.
8
Strings Strings are encoded arrays of characters designed to hold language Recall that all characters are encoded as numeric values Most modern systems use a variant of ASCII or Unicode We’ll assume ASCII
9
ASCII Table
10
Strings By convention, strings in C are a null terminated array of characters There is no intrinsic string datatype Null equals character value 0 i.e. char z = 0; z has a null character written \0 Declaration: char string[] = “Hello world”; Stored: Hello World\0 (ie 12 characters) All string handling expects this
11
Working with strings Much like working with pointers and arrays
Many helper routines in string.h strcmp – tests strings for equivalence strcat – concatenates two strings strstr – locates a string within a string Etc. char string[] = “Hello World”; for( int i = 0; string[i] != 0 ; ++i ) { if( string[i] >= ‘A’ && string[i] <= ‘Z’ ) string[i] = string[i] + (97 – 65); }
12
Formatted Input (1) We use two commands to properly obtain input from the keyboard fgets & sscanf fgets places data collected form the keyboard into a buffer stored as a character string up to and including a carriage return (when the user presses return or enter) fgets( buffer, sizeof(buffer), stdin );
13
Formatted Input (2) sscanf splits the input into variables
Space delimited Uses conversion metasymbols like printf sscanf( buffer, “%d %f”, &x, &y); Note the ambersands (&) & is commonly required for input & is not required for strings sscanf( buffer, “%s %s”, &city, &state);
14
Formatted Input (3) #include <stdio.h>
#define BUFF_SIZE 100 //const int BUFF_SIZE = 100; int main( void ) { float x = 0; float y = 0; char buffer[BUFF_SIZE] = { 0 }; printf( "Enter two floats(x y): "); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%f %f", &x, &y );
15
Formatted Input (4) #include <stdio.h> #define BUFF_SIZE 100
#define NUM_SIZE 100 int main( void ) { float x[NUM_SIZE] = { 0 }; char buffer[BUFF_SIZE] = { 0 }; for( int i=0; i < NUM_SIZE; ++i ) printf("Enter entry %d: ", i ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%f", &x[i]); }
16
Formatted Input (5) #include <stdio.h> #define BUFF_SIZE 100
#define NAME_SIZE 100 int main( void ) { char name[NAME_SIZE] = { 0 }; char buffer[BUFF_SIZE] = { 0 }; printf("Enter name:” ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%s", name); // note, no &
17
Formatted Input (5) #include <stdio.h> #define BUFF_SIZE 100
#define NAME_SIZE 100 #define NAME_QTY 5 int main( void ) { char names[NAME_QTY][NAME_SIZE] = { 0 }; char buffer[BUFF_SIZE] = { 0 }; for( int i=0; i < NAME_QTY; ++i ) printf("Enter entry %d: ", i ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%s", names[i]); }
19
Comparing strings #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { char name[100] = "Falken"; // String literal initialization char input_name[100] = ""; // Empty string initialization char buff[100] = { 0 }; // All null initialization fgets(buff, sizeof(input_name), stdin); sscanf(buff, "%s", input_name); // Note the lack of & if (strcmp(input_name, name) == 0) // strcmp is in string.h, unusually { // strcmp returns <0, 0, or >0. // 0 means "no difference" printf("Hello Professor %s\n", input_name); } else printf("Hello %s, would you like to play a game?\n", input_name); return(EXIT_SUCCESS);
20
References Darnell, P. A. & Margolis, P. E. (1996) C, a software engineering approach, 3rd ed., Springer, New York, p. 327. Visited 23OCT2010.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.