0% found this document useful (0 votes)
8 views

1 ICS 2175 Lecture 5 Strings and Arrays

Uploaded by

fasteluv19
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

1 ICS 2175 Lecture 5 Strings and Arrays

Uploaded by

fasteluv19
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

ICS 2175: Programming

Arrays and Strings in C


Arrays
 An array is an identifier that refers to a collection of data
items that all have the same name.
 The data items must be of the same type (e.g. all
integers or all characters).
 The individual array elements are distinguished from one
another by the value that is assigned to a subscript.
Arrays
 When you declare an array variable, you state the type of its
elements and its size.
 Below is a representation of an array with 5 character
elements. Each element can be accessed by a combination of
the arrayName and its position in the array (subscript).

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;

 We created an array called names, which has space for four


integer variables.
 You may also see that we stored 0 in the last space of the
array. This is a common technique used by C programmers to
signify the end of an array.
Array syntax
 Arrays have the following syntax, using square brackets to access
each indexed value (called an element).
x[i]
so that x[5] refers to the sixth element in an array called x. In C, array
elements start with 0. Assigning values to array elements is done by,
x[10] = g;
and assigning array elements to a variable is done by,
g = x[10];
Array example
 In the following example, a character based array named
word is declared, and each element is assigned a
character.
 The last element is filled with a zero value, to signify the
end of the character string (in C, there is no string type,
so character based arrays are used to hold strings).
 A printf statement is then used to print out all elements of
the array.
Array example

#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

 Arrays may consist of any of the valid data types.


 Arrays are declared along with all other variables in the declaration section
of the program.

#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

 Single operations which involve entire arrays are not


permitted in C.
 Thus if array1 and array2 are similar arrays (i.e. same
data type, same dimensionality and same size),
assignment operations, comparison operations, etc. must
be carried out on an element by element basis.
 This is usually accomplished within a loop, where each
pass through the loop is used to process one array
element.
Multi dimensioned arrays
 Multi-dimensioned arrays have two or more index values
which specify the element in the array.
multi[i][j]
 In the above example, the first index value i specifies a
row index, whilst j specifies a column index.
Declaration and calculations
 int m1[10][10];

 static int m2[2][2] = { {0,1}, {2,3} };

 sum = m1[i][j] + m2[k][l];


Strings

 String data types are represented by an array of characters.


 They are used to store data such as names.

#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

– 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


Built in functions for string handling

 strncat Appends 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


strupr()
#include <stdio.h>
#include <string.h>

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()

 Consider the following program, which initialises the contents of


the character based array word during the program, using the
function strcpy, which necessitates using the include file string.h.
#include <stdio.h>
#include <string.h>
main()
{
char word[20];
strcpy( word, "hi there." );
printf("%s\n", word );
}

You might also like