written 5.7 years ago by | • modified 5.7 years ago |
Subject : Structured Programming Approach
Title : Arrays, String, Structures and Union
Difficulty : Medium
written 5.7 years ago by | • modified 5.7 years ago |
Subject : Structured Programming Approach
Title : Arrays, String, Structures and Union
Difficulty : Medium
written 5.7 years ago by | • modified 5.7 years ago |
1) strlen() function
This function returns an integer value that is the length of the string passed to the function.
When returning the length of the string it does not consider the space required for null Character.
Hence it returns the exact length of the string neglecting these space required for null character.
Example:
#include<conio.h>
#include <stdio.h>
#include<string.h>
void main()
{
int l;
char a[100];
clrscr();
printf(“enter a string\n”);
gets(a);
l=strlen(a);
printf(“the length of the entered string is: %d”,l);
getch();
}
Output:
Enter a string
Hello
The length of the entered string is 5
2) strcpy() function
This function copies the second string into the first string passed to the function.The second string remains unchanged.Only the first string is changed and gets a copy of the second string.for e.g. strcpy(str1,str2), will copy the string “str2” into the string “str1”.
Example:
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char a[100],b[100]
;clrscr();printf(“enter a string\n”);
gets(a);
strcpy(b,a);
printf(“the new string is %s”,b);
getch();
}
Output:
Enter a string
Hello,how are you?
The new string is Hello,how are you?
3) Strcmp() function
This function compares the two string variables passed to it.it returns an integer value equal to:
0(zero),if the two strings are equal.
Negative value ,if the first string is smaller than the second string.
Positive value,if the first string is greater than the second string.
Both the strings remain unchanged.
The string is smaller means its alphabetical sequence is smaller.
For Example: ”Hello” is lesser than “Hi”; because the first character “H” is same,but the second character “e” is smaller than “I”.”e” is smaller than “I” because “e” comes before “i”in the alphabets i.e. A,B,C, ......Z. the function compares the ASCII value of the characters.
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100],b[100];
clrscr();
printf(“enter two strings:\n”);
gets(a);
gets(b);
if(strcmp(a,b)==0)
printf(“strings are equal “);
else
printf(“%s string is greater “,a);
elseprintf(“%s string is greater”,b);
getch();
}
Output:
Enter two strings:
Hello
Hi
Hi string is greater
4) strcat() function
This function concatenates (joins) the two string variables passed to it.It returns a string of the combination of the two in the first string variable.
Example:
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char a[100],b[100];
clrscr();
printf(“enter two strings:\n”);
gets(a);
gets(b);
strcat(a,b);
printf(“the concatenated strig is %s”,a);
getch();
}
Output:
Enter two strings:
Mumbai
University
The concatenated string is MumbaiUniversity