Array and String
Array and String
Array and String
Strings
Need of Array Variable
🞂 Suppose we need to store rollno of the student in the integer variable.
Declaration
int rollno;
float
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
🞂 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] c h i r a g b \0
Declaring & Initializing String
Declaration
char name[10];
Initialization method 1:
char name[10]={'c','h','i','r','a','g','b','\0'};
Initialization method 2:
char name[10]="chiragb";
//'\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] c h i r a g b \0
Read String: scanf()
Program
Output
1 void main()
2 { Enter name: chirag
3 char name[10]; Name=chirag
4 printf("Enter name:"); Output
5 scanf("%s",name);
Enter name: city ahmedabad
6 printf("Name=%s",name);
Name=city
7 }
🞂 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.
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:chirag name
3 void main() 11
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:Computera
8 if(strcmp(s1,s2)==0) Enter string-2:Computerb
9 printf("Strings are same"); Strings are not 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.
strcat(s1,s2) Appends 2nd string at the end of 1st string.
strcat(s1,s2); a copy of string s2 is appended at the end of string s1.
Now s1 becomes “TheirThere”
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
strncat(s1,s2,n) Appends first n character of string s2 at the end of string s1.
strncat(s1,s2,2);
printf("%s", s1); Output : TheirTh
String Handling Functions (Cont…)
For examples consider: char s1[]="Their",s2[]="There";
Syntax Description
strncmp(s1,s2,n) Compares first n character of string s1 and s2 and returns similar result as
strcmp() function.
printf("%d",strcmp(s1,s2,3)); Output : 0
strrchr(s1,c) Returns the last occurrence of a given character in a string s1.
printf("%s",strrchr(s2,'r')); Output : re
✓
Thank you