Pps ch5
Pps ch5
Pps ch5
int mark[5];
[0] [1] [2] [3] [4] [0] [1] [2] [3] [4]
Float Array
integer float
float avg[5];
INITIALING AND ACCESSING AN ARRAY
Declaring, initializing and accessing single integer variable
int mark=90; //variable mark is initialized with value 90
printf("%d",mark); //mark value printed
Declaration
int data[3][3]; //This array can hold 9 elements
Each character in the array occupies one byte of memory, and the last character must
always be null('\0').
The termination character ('\0') is important in a string to identify where the string
ends.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
name[10] D A R S H A N \0
DECLARING & INITIALIZING STRING
Declaration
char name[10];
Initialization method 1:
char name[10]={'D','A','R','S','H','A','N','\0'};
Initialization method 2:
char name[10]="DARSHAN";
//'\
0' will be automatically inserted at the end in this type of declaration.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
name[10] D A R S H A N \0
READ STRING: SCANF()
There is no need to use address of (&) operator in scanf to store a string.
As string name is an array of characters and the name of the array, i.e., name indicates the
base address of the string (character array).
scanf() terminates its input on the first whitespace(space, tab, newline etc.)
encountered. Program
1 void main()
2 {
3 char name[10];
4 printf("Enter name:");
5 scanf("%s",name);
6 printf("Name=%s",name);
7 }
Output
Enter name: Darshan
Name=Darshan
Output
Enter name: CE Darshan
Name=CE
READ STRING: GETS()
gets(): Reads characters from the standard input and stores them as a string.
puts(): Prints characters from the standard.
scanf(): Reads input until it encounters whitespace, newline or End Of File(EOF)
whereas gets() reads input until it encounters newline or End Of File(EOF).
gets(): Does not stop reading input when it encounters whitespace instead it takes
whitespace as a string.
Program
1 #include<stdio.h>
2 void main()
3 {
4 char name[10];
5 printf("Enter name:"); Output
6 gets(name); //read string including white spaces Enter name:Darshan Institute
7 printf("Name=%s",name); Name=Darshan Institute
8 }
STRING HANDLING FUNCTIONS : STRLEN()
C has several inbuilt functions to operate on string. These functions are known as string
handling functions.
strlen(s1): returns length of a string in integer
Program
1 #include <stdio.h> Output
2 #include <string.h> //header file for string functions Enter string: CE Darshan
3 void main() 10
4 {
5 char s1[10];
6 printf("Enter string:");
7 gets(s1);
8 printf("%d",strlen(s1)); // returns length of s1 in integer
9 }
STRING HANDLING FUNCTIONS: STRCMP()
strcmp(s1,s2): Returns 0 if s1 and s2 are the same.
Returns less than 0 if s1<s2.
Returns greater than 0 if s1>s2.
Program
1 void main() Output
2 { Enter string-1:Computer
3 char s1[10],s2[10]; Enter string-2:Computer
4 printf("Enter string-1:"); Strings are same
5 gets(s1);
6 printf("Enter string-2:"); Output
7 gets(s2); Enter string-1:Computer
8 if(strcmp(s1,s2)==0) Enter string-2:Computer
9 printf("Strings are same"); Strings are same
10 else
11 printf("Strings are not same");
12 }
STRING HANDLING FUNCTIONS
For examples consider: char s1[]="Their",s2[]="There";
Syntax Description
strcpy(s1,s2) Copies 2nd string to 1st string.
strcpy(s1,s2) copies the string s2 in to string s1 so s1 is now “There”. s2
remains unchanged.
strchr(s1,c) Returns a pointer to the first occurrence of a given character in the string s1.
printf("%s",strchr(s1,'i'));
Output : ir
strstr(s1,s2) Returns a pointer to the first occurrence of a given string s2 in string s1.
printf("%s",strstr(s1,"he"));
Output : heir
STRING HANDLING FUNCTIONS (CONT…)
For examples consider: char s1[]="Their",s2[]="There";
Syntax Description
strrev(s1) Reverses given string.
strrev(s1); makes string s1 to “riehT”
strlwr(s1) Converts string s1 to lower case.
printf("%s",strlwr(s1)); Output : their
strupr(s1) Converts string s1 to upper case.
printf("%s",strupr(s1)); Output : THEIR
strncpy(s1,s2,n) Copies first n character of string s2 to string s1
s1=""; s2="There";
strncpy(s1,s2,2);
printf("%s",s1); Output : Th