Array Strings in Programiing
Array Strings in Programiing
Dr. Tatwadarshi P. N.
One Dimensional Array (Contd…)
• A list of items can be given one variable name using only one subscript and such a
variable is called a Single-Subscripted variable or a One-Dimensional array.
• In mathematics, we often deal with variables that are single-subscripted. For
instance, we use the equation.
• Ex:
✔ float height[50];
✔ int group[10];
✔ char name[10];
Dr. Tatwadarshi P. N.
Initialisation of One Dimensional Array
• After an array is declared it has to be initialised. Otherwise, they will contain garbage
values.
• The values in the list are separated by commas. For example, the statement
int number[3] = { 0,0,0 };
Compile time Initialisation of Array
• float total[5] = {0.0,15.75,–10};
First three elements are initialised remaining 2 will be zero.
• The size may be omitted. In such cases, the compiler allocates enough space for all
initialized elements.
• For example, the statement
int counter[ ] = {1,1,1,1};
Run time Initialisation of Array
• An array can be explicitly initialized at run time.
• This approach is usually applied for initializing large arrays
• We can think of the table as a matrix consisting of four rows and three columns.
Declaration Two Dimensional Array
• Two-dimensional arrays are declared as follows:
• When all the elements are to be initialized to zero, the following short-cut method may
be used.
• int m[3][5] = { {0}, {0}, {0}};
Strings
Strings
• A string is a sequence of characters that is treated as a single data item.
• Any group of characters (except double quote sign) defined between double quotation
marks is a string constant.
• Example:
“Man is obviously made to think.”
• Character strings are often used to build meaningful and readable programs. The
common operations performed on character strings include:
reading and writing
Combining strings together.
Copying one string to another.
Comparing strings for equality.
Extracting a portion of a string.
Declaring and Initialising String variables
• In C the string are represented as character arrays.
• So, a string variable is any valid C variable name and is always declared as an array of
characters.
• The size determines the number of characters in the string_name.Some examples are:
char city[10];
char name[30];
Declaring and Initialising String variables
(Contd…)
• Like numeric arrays, character arrays can be initialised when they are declared.
• C permits a character array to be initialised in either of the following two forms:
char city [9] = “ NEW YORK ”;
char city [9]={‘N’,‘E’,‘W’,‘ ‘,‘Y’,‘O’,‘R’,‘K’,‘\0’};
Declaring and Initialising String variables
(Contd…)
• We can also initialize the character array without specifying the size of the array.
• Example:
char address[10]
scanf(“%s”, address);
NEW YORK
Taking String from the user (Contd…)
• We can read a single character from the terminal using the function getchar.
• We can use this function repeatedly to read successive single characters from the
input and place them into an array.
• char ch;
• ch = getchar( );
• The drawback is we’ll have to use looping for taking the string
Taking String from the user (Contd…)
• The more convinent way is using the library function gets available in the <stdio.h>
header file
• It is a simple function with one string parameter and called as under:
gets (str);
• str is a string variable declared properly.
• It reads characters into str from the keyboard until a new line character is encountered
and then appends a null character to the string.
• The strln function counts and returns the number of characters in a string. It takes
the form
n = strlen(string);
• Where n is an integer variable , which receives the value of the length of the
string.