Arrays and Strings
Arrays and Strings
& Strings
ARRAYS
An array is a contiguous blocks of memory that holds similar kind of values. In some other way,
an array is a sequence of homogeneous or similar kind of items. An array can be defined as a
group of similar kind of items referred by a common name. A specific element in the array is
located or identified by its index value. C supports different types of Arrays. They are:
a Array name
10 15 12 18 20 25 Array values
0 1 2 3 4 5 Index positions
In memory, all the elements of above array are placed in continuous locations. Suppose the
starting is at 100th location then the remaining locations are 102, 104, 106, 108, 110. Here 100 is
the base address of array ‘a’. Index values always start from 0. Each element is referred as a[0],
a[1], a[2]…… Index values should not be negative values & the size of the array should not be a
value, which is less than 0.
Ex: #include<stdio.h>
#include<conio.h>
void main()
{
int a[6] ={11,12,14,45,43,56};
int I;
clrscr();
for(I=0;I<6;I++)
printf(“%d”,a[I]);
getch();
}
Ex:
Int a[4][4];// an integer array with 4 rows & 4 columns
Char str[5][15]; //a char. Array with 5 rows & 15 columns (or) a set of 5 names
// with 15 characters in each name.
0 1 2 3
0 11 55 44 33
1 77 99 88 67
2 22 25 78 87
Here
a[0][0] = 11 , a[0][1] = 55, a[0][2] = 44, a[0][3] = 33
a[1][0] = 77, a[1][1] = 99 ………………………..
a[2][0] = 22, a[2][1] = 25 …………………………
The for loop causes the process of asking for and receiving a list of 10 numbers from the
user. The first time through the loop, i has a value 0, so the scanf() function will cause the
value typed to be stored in the array element num[0], the first element of the array. This
process will be repeated until i become 9. This is last time through the loop, which is a
good thing, because there is no array element like num[10].
Disadvantages of arrays:
1. As the array size is fixed, we cannot grew or shrink the size. Hence it may lead to
wastage of memory.
2. Insertions & deletions are difficult to perform.
3. Unable to hold different values.
Muti-dimensional arrays:
The other way of representing arrays is multi-dimensional arrays. In this category, there may be
any number of subscripts in the array. The general form follows:
STRINGS
A string is a collection of character data.
Note:
A string with size 8 is able to hold at most 7 characters. The 8th character will be ‘\0’.
In order to perform input/output operations on strings, C provides different functions.
They are called as unformatted I/O.
strlen() Function :
This function counts the number of characters present in a string.
Syntax:
int strlen(char *str );
The strlen() function calculates the length, in bytes, of str. This calculation
does not include the null terminating character. strlen() function returns
integer value.
Ex: main()
{
char s[20];
strcpy() Function :
This function copies the contents of one sting into another. The base address of the
source and target strings should be supplied to this function.
Syntax:
char *strcpy (char *str1, char *str2);
The strcpy function copies characters from str2 to str1 and including the
terminating null character. The str2 should be a variable and str1 can
either be a string constant or a variable.
The strcpy function returns a string str2.
Ex:
main()
{
char a[20],b[20];
int i;
clrscr();
printf("\n Enter any string : ");
gets(a);
strcpy(b,a);
printf("\n Copied string is : ");
puts(b);
getch();
}
String Copy Without Using String Handling Functions:
main()
{
strrev() Function :
This function reverses the string.
Ex:
main()
{
char a[20],b[20];
int i,stringlength=0;
clrscr();
printf("\n Enter any string : ");
gets(a);
strrev(a);
printf("\n Reversed string is : ");
puts(a);
}
String Reverse without using String Handling Functions:
#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
char a[20],b[20];
int i,stringlength=0;
clrscr();
printf("\n Enter any string : ");
gets(a);
for(i=0;a[i]!='\0';i++)
{
stringlength++;
}
for(i=0;i<stringlength;i++)
{
b[i]=a[stringlength-(i+1)];
strcat() Function :
This function concatenates the source string into target string. It is necessary to place ‘\
0’ into the target string, to make its end.
The strcat() function concatenates or appends str2 to str1. All characters from
str2 are copied including the terminating null character.
The str1 should be a variable and str2 can either be a string constant or a
variable. The strcat() function returns the string str1.
Ex:
main()
{
char a[20],b[20],c[40];
int i,j;
clrscr();
printf("\n Enter 1st string:");
gets(a);
printf("\n Enter 2nd string :");
gets(b);
strcpy(c,a);
strcat(c,b);
printf("\n concatenated string is ");
puts(c);
}
String Concatenation without using String Handling Functions:
#include <stdio.h>
#include <conio.h>
main()
{
char a[20],b[20],c[40]; int i,j;
clrscr();
printf("\n Enter 1st string:"); gets(a);
printf("\n Enter 2nd string :"); gets(b);
for(i=0;a[i]!='\0';i++)
{
c[i]=a[i];
}
for(j=0;b[j]!='\0';j++)
{
c[i+j]=b[j];
getch();
}
Program to find count of words in a string.
main()
{
char s[200];
int i,wordcount=0,wordflag = 0,slen;
clrscr();
printf("\n enter any string:");
gets(s);
slen = strlen(s);
for(i=0;i<=slen;i++)
{
if ((s[i] >= 65 && s[i] <= 91) || (s[i] >= 97 && s[i] <= 122) || (s[i] >= 48 &&
s[i] <= 57))
{
wordflag = 1;
continue;
}
if (wordflag == 1)
#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
char s[10];
int i,j;
clrscr();
printf("\n Enter a string : ");
gets(s);
for(i=0;s[i] != '\0';i++)
{
for(j=0;j<=i;j++)
{
printf("\t%c",s[j]);
}
printf("\n");
}
getch();
}
#include <stdio.h>
#include <conio.h>
Character I/O in C.
FUNCTION PURPOSE
getchar() To accept a single character from the keyboard; it returns the same
character
gets() To accept a string from the keyboard
getch() To accept a single character from the keyboard; it returns the ASCII
value of the character. It doesn’t echo the character on the screen
getche() To accept a single character from the keyboard; it returns the ASCII
value of the character. It echoes the character on the screen
Putchar() To prints a given character on the screen
Puts To print a given string on the screen
Isalpha() To check whether the given character is alphabet or not
Isdigit() To check whether the given character is digit or not
Isalnum() To check whether the given character is alphanumeric or not
Isspace() To check whether the given character is a space or not
Islower() To check whether the given character is alphabet and is in lower case
or not
Isupper() To check whether the given character is alphabet and is in upper case
or not
isalpha():
The isalpha( ) function returns nonzero if ch is an alphabet; otherwise zero is returns
isalnum():
The isalnum( ) function returns nonzero if its argument is either a letter of the alphabet
or a digit. If the character is not alphanumeric, zero is returned
int isalnum(char ch);
ispunct():
isspace():
The function returns nonzero if c is space otherwise this will return zero which will be
equivalent to false
int isspace(char ch);
Ex: void main()
{
char ch;
clrscr();
printf(“\n Enter Any Character:”);
scanf(“%c”,&ch);
if(isspace(ch))
printf(“\n Given Character is space”);
else
printf(“\n Given Character is not a space”);
getch();
}
isdigit():
The isdigit( ) function returns nonzero if ch is a digit, that is, 0 through 9. Otherwise
zero is returned.
int isdigit(int ch);
Ex: void main()
{
char ch;
clrscr();
printf(“\n Enter Any Character:”);
scanf(“%c”,&ch);
if(isdigit(ch))
printf(“\n Given Character is digit”);
else
isxdigit():
The isxdigit( ) function returns nonzero if ch is a hexadecimal digit; otherwise zero is
returned. A hexadecimal digit will be in one of these ranges: A–F, a–f, or 0–9.
int isxdigit(char ch);
Ex:
void main()
{
char ch;
clrscr();
printf(“\n Enter Any Character:”);
scanf(“%c”,&ch);
if(isdigit(ch))
printf(“\n Given Character is Hexa Decimal Digit”);
else
printf(“\n Given Character is not a Hexa Decimal digit”);
getch();
}
islower():
The islower() function returns nonzero if ch is a lowercase letter; otherwise zero is
returned.
int islower(char ch);
Ex:
void main()
{
char ch;
clrscr();
printf(“\n Enter Any Character:”);
scanf(“%c”,&ch);
if(islower(ch))
printf(“\n Given Character is in lower case”);
else
printf(“\n Given Character is not in a lower case ”);
getch();
}
isupper();
The isupper( ) function returns nonzero if ch is an uppercase letter; otherwise zero is
returned.
int isupper(char ch);
Ex:
tolower():
The tolower( ) function returns the lowercase equivalent of ch, if ch is a upper case
character; otherwise ch is returned unchanged.
int tolower(char ch);
Ex:
#include <stdio.h>
void main()
{
clrscr();
printf( "Lower case of R is %c\n",
tolower('R'));
printf( "Lower case of 4 is %c\n",
tolower('4'));
printf( "Lower case of a is %c\n",
tolower('a'));
getch();
}
toupper():
The toupper( ) function returns the uppercase equivalent of ch, if ch is a lower
case letter; otherwise ch is returned unchanged..
int toupper(int c);
#include <stdio.h>
void main()
{
clrscr();
printf( "Upper case of m is %c\n", tolower('m'));
printf( "Upper case of 6 is %c\n", tolower('6'));
printf( "Upper case of u is %c\n", tolower('u'));
getch();