String is a collection of characters or array of characters and it is
terminated by \0 (null character).
<string.h> header file is used in
String C programming.
There are two
ways to declare string in c, they are
1. By char array
2. By string literal
1)
string by char array
char str[20]={'H', 'E', 'L', 'L','O','\0'};
Array index starts from 0.So it is represented like this
Example:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<conio.h>
void main(){
char str[20]={'H', 'E', 'L', 'L','O','\0'};
printf("Char Array Value is: %s\n",str);
getch();
}
Output:
Char Array Value is: HELLO
2) string By string literal:
char str[20]="HELLO";
Example:
#include<stdio.h>
#include <string.h>
#include<conio.h>
void main(){
char str[20]="HELLO";
printf("String Literal Value is: %s\n", str);
getch();
}
Output:
String Literal Value is: HELLO
gets() and puts() functions
gets() function reads string from user(input)
and puts() function prints the string(output).
Example:
#include<stdio.h>
#include <string.h>
#include<conio.h>
void main(){
char name[30];
printf("Enter name: ");
gets(name); //input
printf("Name is: ");
puts(name);
//displays or print string
getch();
}
Output:
Enter name: suryosnata
Name is: suryosnata
strlen() function
strlen()
function returns the length of the given string. It doesn't count null
character '\0'.
Example:
#include<stdio.h>
#include
<string.h>
#include<conio.h>
void
main(){
char
str[20]={'s', 'u', 'r', 'y', 'o', 's', 'n', 'a', 't', 'a', '\0'};
printf("Length of string is:
%d",strlen(str));
getch();
}
Output:
Length of string is: 10
Copy
String:strcpy()
strcpy()
function copies the source string in destination.
Syntax:
strcpy(destination,
source)
Example:
#include<stdio.h>
#include <string.h>
#include<conio.h>
void main(){
char str[20]={'s', 'u', 'r',
'y', 'o', 's', 'n', 'a', 't', 'a', '\0'};
char str2[20];
strcpy(str2,str);
printf("Copied string is:
%s",str2);
getch();
}
Output:
Copied string is: suryosnata
String Concatenation:strcat()
strcat()function
concatenates two strings .
Syntax:
strcat(first_string, second_string);
Example:
#include<stdio.h>
#include <string.h>
#include<conio.h>
void main(){
char str[20]={'s', 'u', 'r', 'y', 'o', 's',
'n', 'a', 't', 'a', '\0'};
char
str2[10]={'B','e','h','e','r','a','\0'};
strcat(str,str2);
printf("New String
is:%s",str);
getch();
}
Output:
New String is:suryosnataBehera
Compare String:strcmp()
strcmp()
function compares two string and returns 0 if both strings are equal.
Syntax:
strcmp(first_string, second_string)
Example:
#include<stdio.h>
#include <string.h>
#include<conio.h>
void main(){
char str1[20],str2[20];
printf("Enter 1st string: ");
gets(str1);
printf("Enter 2nd string: ");
gets(str2);
if(strcmp(str1,str2)==0)
{
printf("Strings are
equal");
}
else
{
printf("Strings are not
equal");
}
getch();
}
Output:
Enter 1st string: suryosnata
Enter 2nd string: suryosnata
Strings are equal
Reverse String:strrev()
strrev(string)
function returns reverse of the given string.
Example:
#include<stdio.h>
#include <string.h>
#include<conio.h>
void main(){
char str[20];
printf("Enter string: ");
gets(str);
printf("String is: %s",str);
printf("\nReverse String is:
%s",strrev(str));
getch();
}
Output:
Enter string:suryosnata
String is: suryosnata
Reverse String is: atansoyrus
String Lowercase:strlwr()
strlwr(string)
function returns string characters in lowercase.
Example:
#include<stdio.h>
#include <string.h>
#include<conio.h>
void main(){
char str[20];
printf("Enter string: ");
gets(str);
printf("String is: %s",str);
printf("\nLower String is:
%s",strlwr(str));
getch();
}
Output:
Enter string: SURYOsnata
String is: SURYOsnata
Lower String is:suryosnata
String Uppercase:strupr()
strupr(string) function returns string
characters in uppercase.
Example:
#include<stdio.h>
#include <string.h>
#include<conio.h>
void main(){
char str[20];
printf("Enter string: ");
gets(str);
printf("String is: %s",str);
printf("\nLower String is:
%s",strupr(str));
getch();
}
Output:
Enter string: SURYOsnata
String is: SURYOsnata
Lower String is:SURYOSNATA
No comments:
Post a Comment