INTRODUCTION
Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a unique character '\0' (null pointer : represent end of string) .String is a sequence of characters stored in a character array. It is a text enclosed in double quotation mark.
Declaration
- char str[ ]={'A','B','C','D','\0'};
- char str[ ]="ABCD";
- String operation performed using "string.h" header file.
- String I/O = puts(instead of printf) : ouput , gets(instead of scanf) : input
|
FUNCTION |
DESCRIPTION |
|
Strlen() |
Can compute the length of the string |
|
Strcpy() |
Can copy the content of a string to another |
|
Strcat() |
Is used to concatenate or join two strings |
|
Strcmp() |
Can compare two strings |
|
Strlwr() |
Can convert the string to lowercase |
|
Strupr() |
Is used to convert the letter of string to uppercase |
|
Strrev() |
Is used to reverse the string |
code :
#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int a;
printf("enter a string : \n");
scanf("%s",str);
a=strlen(str);
printf("%d \n",a);
printf("%d ",sizeof(str));
}
output :
enter a string :
1234 //I entered value 1234
4 //length of string
20 //sizeof of the string which is mentiond in str[]
#include<stdio.h>
#include<string.h>
int main()
{
char str1[21];
char str2[12];
printf("enter string \n");
scanf("%s",str1);
strcpy(str2,str1);
printf("%s",str2);
}
output :
enter string
1245 //entered string one
1245 //which is stored in string two
3)
code :
#include<stdio.h>
#include<string.h>
int main()
{
char str1[21];
char str2[30];
printf("enter str1 \n");
scanf("%s",str1);
printf("enter str2 \n");
scanf("%s",str2);
strcat(str1,str2);
printf("%s",str1);
}
output :
enter str1
1234
enter str2
2345
12342345 //string one and two are joint
4)
code :
#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
char string[12];
int a;
printf("enter any str \n");
scanf("%s",str);
printf("enter any string \n");
scanf("%s",string);
a=strcmp(str,string);
printf("%d %d \n",sizeof(str),sizeof(string));
printf("%d",a);
}
output :
enter any str
1234 //string one length is 4
enter any string
123456678 //string two length is 9
20 12 //sizeof of two strings
-1 //when string 2 > string 1 it will show -1 & string 1>string 2 it will show 1
5)
code :
#include<stdio.h>
#include<string.h>
int main()
{
char str[21];
printf("enter string \n");
scanf("%s",str);
printf("conversion into lowercase \n");
strlwr(str);
printf("%s",str);
}
output :
enter string
ABC
conversion into lowercase
abc
6)
code :
#include<stdio.h>
#include<string.h>
int main()
{
char str[21];
printf("enter string \n");
scanf("%s",str);
printf("conversion into uppercase \n");
strupr(str);
printf("%s",str);
}
output :
enter string
india
conversion into uppercase
INDIA
7)
code :
#include<stdio.h>
#include<string.h>
int main()
{
char str[21];
printf("enter string :\n");
scanf("%s",str);
strrev(str);
printf("reverse string :\n");
printf("%s",str);
}
outpur :
enter string :
abcdef
reverse string :
fedcba
No comments:
Post a Comment