1. an array is a collection of a fixed number of components wherein all of the components are of the...

43
EKT120 COMPUTER PROGRAMMING Arrays & Strings (Part II) Dr. Nik Adilah Hanin Bt. Zahri [email protected] 1

Upload: osborne-briggs

Post on 25-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

  • Slide 1
  • 1
  • Slide 2
  • An array is a collection of a fixed number of components wherein all of the components are of the same type Example: Suppose that there is a list of five integers: 5, 10, 15, 20, and 25 Previously we would declare five variables: int iNum1,iNum2,iNum3,iNum4,iNum5; By using array, since they are all of the same data type, we could just write: int aiNum[5]; 2
  • Slide 3
  • 5 10 15 20 25 3 aiNum aiNum[0] aiNum[1] aiNum[2] aiNum[3] aiNum[4] int aiNum[5]; 5 components or elements in this array Elements are referred to index Element aiNum[2] has index 2 and value 15
  • Slide 4
  • Column 0 1 0 1 2 3 4 aiValue[0][0]aiValue[0][1] aiValue[1][0]aiValue[1][1] aiValue[2][0]aiValue[2][1] aiValue[3][0]aiValue[3][1] Row int aiValue[4][2]; aiValue[2][1]=5;
  • Slide 5
  • Column 0 1 0 1 2 3 5 aiValue[0][0]aiValue[0][1] aiValue[1][0]aiValue[1][1] aiValue[2][0]5 aiValue[3][0]aiValue[3][1] Row int aiValue[4][2]; aiValue[2][1]=5;
  • Slide 6
  • 1. Introduction to Strings 2. Declaration of Strings 3. Fundamentals of Strings & Characters 4. Initialization of Strings 5. Assigning Values to Strings 6. Character and String Manipulations 7. Strings Conversion Functions 8. ASCII Table 6
  • Slide 7
  • A string is a series of characters treated as a single unit. Also known as character array Strings can be treated as array of type char used to store names of people, places, or anything that involves a combination of letters. A string may include letters, digits and various special characters such as +, -, *, ? and $. String literals, or string constants, in C are written in double quotation marks ( ) as follows: Example: John Doe(a name) 99999 Main Street (a street address) Kangar, Perlis(a city and a state) (012) 123-8755(a telephone number) 7
  • Slide 8
  • The data type string is a programmer-defined and is not part of the C language A string with no characters is called a null or empty string. is the empty string. Every character in a string has a relative position in the string. The position of the first character is 0, position of the second is 1, and so on. The length of a string is the number of character in it. of a string is the address of its first character. 8
  • Slide 9
  • StringPosition of a CharacterLength of the String in the String William JacobPosition of W is 013 Position of the first i is 1 Position of (the space) is 7 Position of J is 8 Position of b is 12 MickeyPosition of M is 06 Position of i is 1 Position of c is 2 Position of k is 3 Position of e is 4 Position of y is 5 9
  • Slide 10
  • Declaration of string/character array char variable_name[length]; An example of declaration of an array (or string of characters): char name[10]; //declaration can use to store William, John Doe etc and all strings that shorter than defined length It is not necessary that this max size of 10 characters should at all the times fully used The last value in the string will be a null character (\0). 10 name char name[10]; can store a string up to 10 characters long, and may visualize it as below
  • Slide 11
  • name is an array of 10 elements of type char, could be represented by storing the strings of characters, e.g. William and John Doe in the following way: 11 W name illiam\0 JeoDnho to indicate end of string indefinite values
  • Slide 12
  • Also can be declared as a character array or a variable of type char * char name[] = William"; char *name = William"; 12
  • Slide 13
  • Formatted input Use scanf scanf("%s", name); Copies input into name[] Does not need & (because a string is a pointer) Remember to leave room in the array for the null character ('\0) Formatted output Use printf printf(%s,name); 13
  • Slide 14
  • #include int main() { char name[30]; printf("Enter name: "); scanf(%s,name); //read string from user printf(Hi %s\n,name); //display string return 0; } Input and output characters using scanf() and printf() Example program: 14
  • Slide 15
  • Similar to array, but each character is enclosed in or . Example: char newString[]={W, e, l, c, o, m, e, \0}; char newString[]= Welcome; \0 is automatically inserted The difference is that single quotes () are used to specify single character constants and null character must be added at the end of the sentence. char newString[]= {W,e,l,c,o,m,e,\0}; 15 Single quotes null char must be added
  • Slide 16
  • On the other hand, double quotes () are constant that specify sequence of characters and always have a null character (\0) automatically inserted at the end. char newString[] = Welcome; 16 Double quotes null char automatically inserted
  • Slide 17
  • The examples below are NOT VALID for string / characters array. newString = Welcome; //no [] and data type newString [] =Welcome; //no data type newString = {W,e,l,c,o,m,e,\0}; //no [] and data type 17
  • Slide 18
  • The left hand side value of an assignation can only be array items and not the entire array, a possible way to assign a string of characters to an array of char can be shown as: newString[0] = W; newString[1] = e; newString[2] = l; newString[3] = c; newString[4] = o; newString[5] = m; newString[6] = e; newString[7] = \0; 18
  • Slide 19
  • A program may need to verify/perform, e.g. Calculation of the string size Copy one string to another Appends one string to another Built-in functions available makes it easier. Standard input/output library General utilities library Character handling library String handling library 19
  • Slide 20
  • #include int main() { char name[30]; printf("Enter name: "); gets(name); //Function to read string from user printf(Hi "); puts(name); //Function to display string return 0; } Input and output characters using gets() puts() function from Example program: 20
  • Slide 21
  • #include int main() { char name[30]; printf("Enter name: "); gets(name); //Function to read string from user printf(Hi "); puts(name); //Function to display string return 0; } Input and output characters using gets() puts() function from Example program: 21 Enter name : John Hi John
  • Slide 22
  • #include int main() { char name[3][30]; printf("Enter 3 names:\n"); for(i=0;i
  • #include int main() { char acString1[20], acString2[20]; //declaration int iResult; printf( "Enter two strings: " ); scanf( "%s %s", acString1, acString2 ); iResult = strcmp( acString1, acString2 );//comparing acString1 and acString2 if ( iResult > 0 ) printf( "\"%s\" is greater than \"%s\"\n",acString1, acString2 ); else if ( iResult == 0 ) printf( "\"%s\" is equal to \"%s\"\n",acString1, acString2 ); else printf( "\"%s\" is less than \"%s\"\n",acString1, acString2 ); return 0; } 39 Enter two strings: computer programming "computer" is less than "programming" Enter two strings: programming computer "programming" is greater than "computer"
  • Slide 40 0 ) printf( "\"%s\" is greater than \"%s\" up to %d characters\n", acString1, acString2, iCompareCount ); else if ( iResult == 0 ) printf( "\"%s\" is equal to \"%s\" up to %d characters\n", acString1, acString2, iCompareCount ); else printf( "\"%s\" is less than \"%s\" up to %d characters\n", acString1, acString2, iCompareCount ); return 0; } 40">
  • #include int main() { char acString1[ 20 ], acString2[ 20 ]; int iResult, iCompareCount; printf( "Enter two strings: " ); scanf( "%s %s", acString1, acString2 ); printf( "How many characters should be compared: " ); scanf( "%d", &iCompareCount ); iResult = strncmp( acString1, acString2, iCompareCount ); if (iResult > 0 ) printf( "\"%s\" is greater than \"%s\" up to %d characters\n", acString1, acString2, iCompareCount ); else if ( iResult == 0 ) printf( "\"%s\" is equal to \"%s\" up to %d characters\n", acString1, acString2, iCompareCount ); else printf( "\"%s\" is less than \"%s\" up to %d characters\n", acString1, acString2, iCompareCount ); return 0; } 40
  • Slide 41 0 ) printf( "\"%s\" is greater than \"%s\" up to %d characters\n", acString1, acString2, iCompareCount ); else if ( iResult == 0 ) printf( "\"%s\" is equal to \"%s\" up to %d characters\n", acString1, acString2, iCompareCount ); else printf( "\"%s\" is less than \"%s\" up to %d characters\n", acString1, acString2, iCompareCount ); return 0; } Enter two strings: computer programming How many characters should be compared: 7 "computer" is less than "programming" up to 7 characters Enter two strings: programming computer How many characters should be compared: 7 "programming" is greater than "computer" up to 7 characters">
  • 41 #include int main() { char acString1[ 20 ], acString2[ 20 ]; int iResult, iCompareCount; printf( "Enter two strings: " ); scanf( "%s %s", acString1, acString2 ); printf( "How many characters should be compared: " ); scanf( "%d", &iCompareCount ); iResult = strncmp( acString1, acString2, iCompareCount ); if (iResult > 0 ) printf( "\"%s\" is greater than \"%s\" up to %d characters\n", acString1, acString2, iCompareCount ); else if ( iResult == 0 ) printf( "\"%s\" is equal to \"%s\" up to %d characters\n", acString1, acString2, iCompareCount ); else printf( "\"%s\" is less than \"%s\" up to %d characters\n", acString1, acString2, iCompareCount ); return 0; } Enter two strings: computer programming How many characters should be compared: 7 "computer" is less than "programming" up to 7 characters Enter two strings: programming computer How many characters should be compared: 7 "programming" is greater than "computer" up to 7 characters
  • Slide 42
  • strcat Appends a string strchr Finds first occurrence of a given character strcmp Compares two strings strcmpi Compares two strings, non-case sensitive strcpy Copies one string to another strlen Finds length of a string strlwr Converts a string to lowercase strncatAppends n characters of string strncmp Compares n characters of two strings strncpy Copies n characters of one string to another strnset Sets n characters of string to a given character strrchr Finds last occurrence of given character in string strrev Reverses string strset Sets all characters of string to a given character strspn Finds first substring from given character set in string strupr Converts string to uppercase 42
  • Slide 43
  • Q & A! 43