Graphical representation
Cont
Individual value of variable rose[4].
rose[0]=10;
rose[1]=20;
rose[2]=30;
rose[3]=40;
other examples are:
Char name[10];
Float[3];
Two-dimensional array
Two dimensional array syntax
datatype arrayname[row][column];
Ex:
int score[2][3];
Graphical representation
Cont
score[0][0]=10;
score[0][1]=20;
score[0][2]=30;
score[1][0]=40;
score[1][1]=50;
score[1][2]=80;
Example1:
int c[]={2,3,4};
printf("%d",c[2]);
EXAMPLE2
int c[]={2,3,4},a;
for(a=0;a<3;a++)
{
printf("a [%d]=%d\n",a,c[a]);
}
Cont
int grade[10],i,n=3,sum=0;
for(i=0;i<n;++i){
printf("Enter the grades for grade [%d]: ",i);
scanf("%d",&grade[i]);
}
for(i=0;i<n;++i){
printf("\ngrade [%d]=%d",i,grade[i]);
}
Strings
A special kind of array is an array of characters
ending in the null character \0 called string arrays
A string is declared as an array of characters
char s[10]
char p[30]
When declaring a string dont forget to leave a
space for the null character which is also known
as the string terminator character
strcpy
strcpy(destinationstring, sourcestring)
Copies sourcestring into destinationstring
For example
strcpy(str, hello world); assigns hello
world to the string str
strcat
strcat(destinationstring, sourcestring)
appends sourcestring to right hand side of
destinationstring
For example if str had value a big
strcat(str, hello world); appends hello world to
the string a big to get
a big hello world
strcmp
strcmp(stringa, stringb)
Compares stringa and stringb alphabetically
Returns a negative value if stringa precedes
stringb alphabetically
Returns a positive value if stringb precedes
stringa alphabetically
Returns 0 if they are equal
Note lowercase characters are greater than
Uppercase
continued
if (strcmp(x,z) != 0)
strlen
strlen(str) returns length of string
excluding null character
strlen(ttttt) = 4 not 5 since \0 not counted
Exercise 1
Output
1
12
123
1234
.
1 2 3 4 5 6 7 8 9 10
Exercise 1
#include <stdio.h>
main()
{
int i,j;
for(j = 1; j <= 10; j++)
{
for(i=1;i <= j;i++)
{
printf(%d ,i);
}
printf(\n);
}
}
Exercise 2
Output
*
**
***
****
.
**********
Exercise 2
#include <stdio.h>
main()
{
int i,j;
for(j = 1; j <= 10; j++)
{
for(i=1;i <= j;i++)
{
printf(* );
}
printf(\n);
}
}
Exercise 3
Output
***********
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
***********
#include <stdio.h>
main()
{
int i,j;
for(j = 1; j <= 10; j++)
{
printf(* );
for(i=1;i <= 8;i++)
{
if ((j==1) || (j==10)) printf(* );
else
printf( );
}
printf(* \n );
}
}
Functions
Function
int isalpha(c);
int isupper(c);
int islower(c);
int isdigit(c);
Return true if
c is a letter.
c is an upper case
letter.
c is a lower case letter.
c is a digit [0-9].
More Functions
Function
Return true if
int isxdigit(c); c is a hexadecimal digit
[0-9A-Fa-f].
int isalnum(c); c is an alphanumeric character (c
is a letter or a digit);
int isspace(c); c is a SPACE, TAB,
RETURN,
NEWLINE, FORMFEED,
or vertical tab character.
int isprint(c);
int iscntrl(c);
Return true if
c is a punctuation
character (neither
control nor
alphanumeric).
c is a printing character.
c is a delete character
or ordinary control
character.
Return true if
c is an ASCII character,
codeless than 0200.
int toupper(int c); convert character c to
upper case (leave it
alone if not lower)
int tolower(int c); convert character c to
lower case (leave it
alone if not upper)
void main() {
char *str;
char a[]="aeiouAEIOU";
int i,j,count=0;
clrscr();
printf("\nEnter the string\n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
for(j=0;a[j]!='\0';j++)
if(a[j] == str[i]
{
count++;
break;
}
printf("\nNo. of vowels = %d",count);
}
}