This document introduces arrays and strings in C programming. It discusses one-dimensional and multi-dimensional arrays, including syntax, initialization, and reading/writing data. Strings are defined as arrays of characters terminated by a null character. Common string functions like strcpy(), strcat(), strcmp(), and strlen() are presented, along with input/output functions scanf(), gets(), printf(), and puts().
3. Array
An array is a collection of one or more values of the
same type. Each value is called an element of
the array. The elements of the array share the same
variable name but each element has its own unique
index number (also known as a subscript). An
array can be of any type:
For example: int , float , char etc.
6. Advantage of Array
• Huge amount of data can be stored under single variable name.
• Searching of data item is faster.
• 2 dimension arrays are used to represent the matrices.
• It is helpful in implementing other data structure like linked list,
queue, stack.
8. One Dimensional Array
A one-dimensional
array (or single dimension
array) is a type of
linear array. Accessing its
elements involves
a single subscript which can
either represent a row or
column index.
SYNTAX: data-type name[index];
EXAMPLE: int num[10];
10. Reading Data from User
• for loop is used to read data from the user.
for(i=0;i<10;i++)
{
scanf(“%d”,&num[i]);
}
11. MULTI DIMENTIONAL ARRAY
The two dimensional (2D) array
in C programming is also
known as matrix. A matrix
can be represented as a
table of rows and columns
SYNTAX: data-type name[row-size][column-size];
EXAMPLE: int a[3][4];
15. WHAT IS STRING ?
A group of characters A string constant is a one-
dimensional array
of characters terminated by a null ( ‘0’ ).
declaration of Character:
• char mychar;
declaration of String:
• char myString[10];
The characters after the null character are ignored.
16. WHAT IS NULL char ?
• Null ‘0’ is a terminator which terminates the string in an array
17. WHY NULL char ?
When declaring a string don’t forget to leave a space for the null
character which is also known as the string terminator character
only way the functions that work with a string can know where the
string ends.
18. INPUT FUNCTION
• Input Function :
The scanf() Function
• header file stdio.h
• Syntax:
• char mystring[100];
• scanf(“%s”, mystring);
The name of a string is a pointer constant to the first character in the character
array.
Problem:
terminates its input on the first white space it finds.
white space includes blanks, tabs, carriage returns(CR), form feeds & new line.
20. INPUT FUNCTION
The gets() Function:
Header file stdio.h
takes a string from standard input and assigns it to a character array.
It replaces the n with 0.
Syntax:
• char mystring[100];
• gets(myString);
• fgets() it keeps the n and includes it as part of the string.
22. OUTPUT FUNCTION
The printf () function
header file: stdio.h
The puts() function
header file: stdio.h
PROGRAM
OUTPUT
23. STRING OPERATION(string.h)
Four main library function which is define in
• string.h header file
• strcpy() - copy one string into another
• strcat() - append one string onto the right side of
• the other
• strcmp() – compare alphabetic order of two
• strings
• strlen() – return the length of a string
28. MORE FUNCTIONS
• strlwr() : converts a string to lowercase
• Strupr() : converts a string to uppercase
• Strncat() : Appends first n characters of a string at the end of
another
• Strncmp() :Compares first n characters of two strings
• Strcmpi():Compares two strings without regard to case ("i" denotes
that this function ignores Case)
• [Note: There are more library functions…..]