Strings in C
1.What is Strings in C ?
Ans : A String in C programming is a sequence of characters terminated with a null character ‘\0’. The C String is stored as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character ‘\0’.
1. String Deceleration
Syntax : char str_name[size];
Example : char str_name[10]
2. Initialization of string
Syntax : char str_name[size]={elements of string separated}
Example : char str[7]={'A','V','E','N','G','E','R'} or char str [7]="AVENGERS"
Functions of String : There are total 7 functions in string.
1. strlen : This function is used to determine the length of a string.
Syntax : strlen(str_name);
Note : The length of the string will be in int form.
Example :
#include<stdio.h>
#include<string.h>
int main ()
{
char a[]="sanskar";
strlen(a); //function of string
printf("The length of the name is %d",strlen(a));
}
Output :
2.strupr : This function is used to convert the lowercase sentence to upper case.
Example :
#include<stdio.h>
#include<string.h>
int main ()
{
char a[]="sanskar";
strupr(a); //function of string
printf("The name or sentence convrted into uppercase %s ",strupr(a));
}
Output :
3. strlwr : This function is used to convert the uppercase sentence to lowercase.
Example :
#include<stdio.h>
#include<string.h>
int main ()
{
char a[]="SANSKAR";
strlwr(a); //function of string
printf("The name converted into lowercase is %s",a);
}
Output :
4. strrev : This function is used to reverse the sentence or a name.
Example :
#include<stdio.h>
#include<string.h>
int main ()
{
char a[]="sanskar";
strrev(a); //function of string
printf("The Name return in reverse is %s",a);
}
Output :
5. strcmp : This function is used to compare two strings.
Note : When we compare two strings we get value in integer. If the two stings are equal the output will be 0.If the strings are not equal the output will be -1 and if both the strings are same but one is lowercase and one is in uppercase we will get output 1.
Example :
a. When strings are Equal :
#include<stdio.h>
#include<string.h>
int main ()
{
char a[]="SANSKAR";
char b[]="SANSKAR";
strcmp(a,b); //function of string
printf("%d",strcmp(a,b));
}
Output :
b. When strings are Not Equal :
#include<stdio.h>
#include<string.h>
int main ()
{
char a[]="sanskar";
char b[]="suraj";
strcmp(a,b); //function of string
printf("%d",strcmp(a,b));
}
Output :
c. When both the strings are equal but one is in uppercase and lowercase.
#include<stdio.h>
#include<string.h>
int main ()
{
char a[]="sanskar";
char b[]="SANSKAR";
strcmp(a,b); //function of string
printf("%d",strcmp(a,b));
}
Output :
6. strcat : This function is used to to conjoin (concatenation) two stings.
Example :
#include<stdio.h>
#include<string.h>
int main ()
{
char a[]="Snow";
char b[]="ball";
strcat(a,b); //funtion of string
printf("%s",a);
}
Output :
7. strcpy : This function is used to copy one string to another string.
Example :
#include<stdio.h>
#include<string.h>
int main ()
{
char a[]="suraj";
char b[]="Sanskar";
strcpy(a,b); //funtion of string
printf("%s",a);
}
Output :
0 Comments