Professional Documents
Culture Documents
Datatype Arrayname (Index) - Example: - Int Rose
Datatype Arrayname (Index) - Example: - Int Rose
– Graphical representation
Cont…
Individual value of variable rose[4].
rose[0]=10;
rose[1]=20;
rose[2]=30;
rose[3]=40;
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
• For example
• strcpy(str, “hello world”); assigns “hello
world” to the string str
Example with strcpy
#include <stdio.h>
#include <string.h>
main()
{
char x[] = “Example with strcpy”;
char y[25];
printf(“The string in array x is %s \n “, x);
strcpy(y,x);
printf(“The string in array y is %s \n “, y);
}
strcat
• strcat(destinationstring, sourcestring)
}
strcmp
• strcmp(stringa, stringb)
x,z);
if (strcmp(x,z) < 0)
printf(“The string in array x %s precedes that in z %s \n “, x,z);
else
printf(“The string in array z %s precedes that in x %s \n “, z,x);
}
else
printf( “they are equal”);
}
strlen
• strlen(str) returns length of string excluding
null character
• strlen(“ttttt”) = 4 not 5 since \0 not counted
Example with strlen
#include <stdio.h>
#include <string.h>
main()
{
int i, count;
char x[] = “tommy tucket took a tiny ticket ”;
count = 0;
for (i = 0; i < strlen(x);i++)
{
if (x[i] == ‘t’) count++;
}
printf(“The number of t’s in %s is %d \n “, x,count);
}
No of space in the sentence
#include <stdio.h>
#include <string.h>
main()
{
int i,j, count;
char x[] = “tommy tucket took a tiny ticket ”;
count = 0;
for (i = 0; i < strlen(x);i++)
{
if ((x[i] == ‘ ‘)
{ count++;
for(j=i;x[j] != ‘ ‘;j++);
i = j;
}
}
printf(“The number of words’s in %s is %d \n “, x,count+1);
}
Input output functions of characters
and strings
• getchar() reads a character from the
screen in a non-interactive environment
• getche() like getchar() except interactive
• putchar(int ch) outputs a character to
screen
• gets(str) gets a string from the keyboard
• puts(str) outputs string to screen
Characters are at the heart of
strings
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 “);
}
}
Some Useful C Character
Functions
• 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);
•
}
•
}