�Dr. S. Sridevy
Associate Professor (CS)
Dr. M. Ramanan
Teaching Assistant (CS)
Dept. of PS&IT, AEC&RI, TNAU, Coimbatore -3.
String: Reading and Writing Strings - String handling functions
COM 102 Theory Schedule 10
Concept of String
S. Sridevy
7/4/2022
Concept of String
Eg: 1) “Man is obviously made to think”
If we want to include a double quote in a string, then we may use it with the back slash.
Eg: printf(“\”well done!\””);
will output
“well done!”
S. Sridevy
7/4/2022
Character String - Operations
S. Sridevy
7/4/2022
Declaration of a String Variable
char string_name[size];
Eg: char city[10];
char name[30];
S. Sridevy
7/4/2022
Declaration of a String Variable
S. Sridevy
7/4/2022
Initialization of a String Variable
static char city[9] = “NEW YORK”;
static char city[9] = {‘N’, ‘E’, ‘W’, ‘ ‘, ‘Y’, ‘O’, ‘R’, ‘K’, ‘\0’};
Note : When we initialize a character array by
listing its elements, we must supply
explicitly the null terminator (‘\0’).
S. Sridevy
7/4/2022
Reading a word
Eg1: char address[15];
scanf(“%s”,address);
Note: ‘&’ is used to get the address of the variable. C does not have a string type, String is just an array of characters and an array variable stores the address of the first index location.
S. Sridevy
7/4/2022
Reading a Line of Text
Eg1: char address[15];
scanf(“%s”,address);
getchar ( )
It is used to read a single character from the terminal.
ch = getchar ( );
S. Sridevy
7/4/2022
Example
/*Program to read a line of text from terminal*/
#include<stdio.h>
void main()
{ char line[81],character; int c; c = 0;
printf(“Enter text. Press<Return>at end \n”);
do {
character = getchar();
line[c] = character;
c++;
} while(character != ‘\n’);
c = c-1;
line[c] = ‘\0’;
printf(“\n %s \n”,line);
}
S. Sridevy
7/4/2022
OUTPUT
Enter text. Press<Return>at end
Programming in C is interesting
Programming in C is interesting
Writing Strings
Eg. printf(“%s”, name);
Arithmetic Operations on Characters
Eg 1:
x = ‘a’;
printf(“%d \n”,x);
will display the number 97 on the screen.
S. Sridevy
7/4/2022
Eg 2:
x = ‘z’ – 1;
output :
x = 121
(ASCII of ‘z’ ie 122 – 1)
atoi ( )
Syntax
x = atoi(string)
Eg:
static char ch[ ] = “1987”;
int y;
y = atoi(ch);
S. Sridevy
7/4/2022
String - Handling Functions
S. Sridevy
7/4/2022
Function | Action |
strcat( ) | Concatenates two strings |
strcmp( ) | Compares two strings |
strcpy( ) | Copies one string over another |
strlen( ) | Finds the length of the string |
strcat( ) function
Syntax
strcat( string1,string2);
S. Sridevy
7/4/2022
strcat( ) function
Eg 2:
static char st1[30] = “First”;
static char st2[10] = “B.Tech”;
static char st3[10] = “FoT”;
strcat(strcat(st1,st2),st3);
printf(“%s”,st1);
printf(“%s”,st2);
printf(“%s”,st3);
S. Sridevy
output :
First B.Tech FoT
B.Tech
FoT
strcmp( ) function
Syntax
strcmp( string1,string2);
S. Sridevy
7/4/2022
strcmp( ) function
Eg: 1) strcmp(name1,name2);
2) strcmp(name1,”john”);
3) strcmp(“ram”, “raj”);
In Eg 3 , the result will be 3 . That is , ASCII value of “m” minus ASCII value of “j” (109 – 106).
S. Sridevy
strcpy( ) function
strcpy( string1,string2);
S. Sridevy
7/4/2022
strcpy( ) function
Eg: 1) strcpy(grade, “First Class”);
2) strcpy(watercon, “GOOD”);
S. Sridevy
strlen( ) function
Syntax :
n = strlen(string);
Eg:
n = strlen(“INDIA”);
printf(“%d”,n);
S. Sridevy
7/4/2022