1 ICS 2175 Lecture 5 Strings and Arrays
1 ICS 2175 Lecture 5 Strings and Arrays
I T E M S
0 1 2 3 4
Arrays
int names[4];
names[0] = 101;
names[1] = 232;
names[2] = 231;
names[3] = 0;
#include <stdio.h>
main()
{
char word[20];
word[0] = 'H';
word[1] = 'e';
word[2] = 'l';
word[3] = 'l';
word[4] = 'o';
word[5] = 0;
printf("The contents of word[] is -->%s\n", word );
}
Declaring arrays
#include <stdio.h>
main()
{
int numbers[100];
float averages[20];
numbers[2] = 10;
--numbers[2];
printf("The 3rd element of array numbers is %d\n", numbers[2]);
}
Declaring an array
#include<stdio.h>
void main()
{
int ageArray[5]; // an array to store the ages (in years) of five children
//to enter the values of the elements of the array
ageArray[0]=10;
ageArray[1]=11;
ageArray[2]=9;
ageArray[3]=8;
ageArray[4]=12;
//now print the ages of the children
printf("The first child's age is %d years.\n",ageArray[0]);
printf("The second child's age is %d years. \n",ageArray[1]);
printf("The third child's age is %d years. \n",ageArray[2]);
printf("The fourth child's age is %d years. \n",ageArray[3]);
printf("The fifth child's age is %d years. \n",ageArray[4]);
}
Assigning initial values to arrays
The declaration is preceded by the word static. The initial values are
enclosed in braces.
#include <stdio.h>
main()
{
static int values[] = { 1,2,3,4,5,6,7,8,9 };
static char word[5] = { 'H','e','l','l','o' };
printf("%s", word);
}
Processing arrays
#include<stdio.h>
void main()
{
char name[30];
//this states that name will be an array of 30 characters or less.
//now we can enter the name then print it out
printf("enter your first name ::");
scanf("%s",name);
printf("You have indicated that your first name is : %s. ", name);
} //end
Strings
In the example above, scanf reads up to the first white
space character.
You can direct scanf to read all characters of a specific
type. If it finds a character which is not of the specified
type, it stops reading and returns what has been read so
far.
This is useful for instance when you enter to enter both
the first name and surname which are separated by white
spaces and one needs to read them together.
Strings
The characters scanf should read are included in square
brackets e.g. [a-z] means continue reading as long as the
characters are alphabetic(lowercase) for both uppercase
and lowercase use [a-zA-Z].
You can use the character ^(circumflex) to negate the
meaning of the characters in the square brackets.
[^1-9] means read every character except a digit. When
scanf comes across a digit, it stops reading and return
what it as read so far.
Strings
#include<stdio.h>
void main()
{
char name[50];
//this states that a name will be an array if 50 characters or less.
//now we can enter the name then print it out
printf("enter your names ::");
scanf("%[^\n]s",name);
/*scanf continues reading until it comes across an end of line
character. It returns what it has read so far*/
printf("You have indicated that your names are : %s\n",name);
} //end
Null terminated strings
Consider the following program,
#include <stdio.h>
main()
{
static char name1[] = {'H','e','l','l','o'};
static char name2[] = "Hello";
printf("%s\n", name1);
printf("%s\n", name2);
}
Null terminated strings
The difference between the two arrays is that name2 has
a null placed at the end of the string, ie, in name2[5],
whilst name1 has not. To insert a null at the end of the
name1 array, the initialization can be changed to,
static char name1[] = {'H','e','l','l','o','\0'};
Built in functions for string handling
string.h
The following macros are built into the file string.h
– strcat Appends a string
main()
{
char name[80]; /* declare an array of characters 0-79 */
printf("Enter in a name in lowercase\n");
scanf( "%s", name );
strupr( name );
printf("The name is uppercase is %s", name );
}
strcpy()