String
String
Definition:
The group of characters, digits, & symbols enclosed within double quotes is called as Strings.
Strings are actually one-dimensional array of characters terminated by a null character '\0'.
Every string is terminated with the NULL (‘\0’) character.
E.g. “INDIA” is a string. Each character of string occupies 1 byte of memory. The last
character is always ‘\0’.
Declaration:
String is always declared as character arrays.
Syntax
char stringname[size];
E.g. char test[20];
Initialization:
We can use 2 ways for initializing.
1. By using string constant
E.g. char test[20]= “Hello”;
2. By using initialisation list
E.g. char test[20]={‘H’, ‘e’, ‘l’, ;l’, ;o’, ‘\0’};
E.g.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Hello";
printf("Length of string str1: %d", strlen(str1));
return 0;
}
Output:
Length of string str1: 5
Strlen() vs sizeof()
Strlen() returns you the length of the string stored in array.
sizeof() returns the total allocated size assigned to the array.
2. strnlen(str,n)
It returns length of the string if it is less than the value specified for maxlen (maximum
length) otherwise it returns maxlen value.
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "welcome";
printf("Length of string str1 when maxlen is 10: %d\n", strnlen(str1, 10));
printf("Length of string str1 when maxlen is 5: %d\n", strnlen(str1, 5));
return 0;
}
Output:
Length of string str1 when maxlen is 10: 7
Length of string str1 when maxlen is 5: 5
3. strcmp() function
It is used to compare 2 strings.
Syntax
temp_varaible=strcmp(string1,
string2);
If the first string is greater than the second string a positive number is returned.
If the first string is less than the second string a negative number is returned.
If the first and the second string are equal 0 is returned.
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "Hello";
char s2[20] = "Hello";
if (strcmp(s1, s2) ==0)
{
printf("string 1 and string 2 are equal");
}
else
{
printf("string 1 and 2 are different");
}
return 0;
}
4. Strncmp(str1,str2)
It compares both the string till n characters or in other words it compares first n
characters of both the strings.
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "Hello";
char s2[20] = "Hello";
if (strncmp(s1, s2,4) ==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different");
}
return 0;
}
5. strcpy() function
It copies the source string to the destination string
Syntax
strcpy(destination,source);
E.g.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[30] = "string 1";
char s2[30] = " New String";
strcpy(s1,s2);
printf("String s1 is: %s", s1);
return 0;
}
6. strncpy(s1,s2)
Case1: If length of str2 > n then it just copies first n characters of str2 into str1.
Case2: If length of str2 < n then it copies all the characters of str2 into str1 and appends
several terminator chars(‘\0’) to accumulate the length of str1 to make it n.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[30] = "Hello";
char s2[30] = "welcome";
strncpy(s1,s2,5);
printf("String s1 is: %s", s1);
return 0;
}
7. strchr(str) / strrchr(str)
It searches string str for character ch.
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30] = "Hai! Hello! Welcome! ";
printf ("%s", strchr(mystr, 'W'));
return 0;
}
8. strstr(str,search)
It is similar to strchr, except that it searches for string srch_term instead of a single char.
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30] = "Hai! Hello! Welcome! ";
printf ("%s", strstr(mystr, "Hello"));
return 0;
}
9. strcat() function
It concatenates a second string to the end of the first string.
Syntax
strcat(firststring, secondstring);
Example
#include <stdio.h>
#include <string.h>
int main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
return 0;
}
10. strlwr() function
It converts all the uppercase characters in that string to lowercase
characters.
Syntax
strlwr(string_name);
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30] = "HAI HELLO WELCOME! ";
printf("%s", strlwr(mystr));
return 0;
}
11. strupr() function
It converts all the lowercase characters in that string to uppercase
characters.
Syntax
strupr(string_name);
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30] = "Hai Hello Welcome! ";
printf("%s", strupr(mystr));
return 0;
}
12. strrev() function
It is used to reverse the string.
Syntax
strrev(string_name);
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30] = "Welcome! ";
printf("%s", strrev(mystr));
return 0;
}
String Arrays
They are used to store multiple strings. 2-D char array is used for string arrays.
Declaration
char arrayname[rowsize][colsize];
E.g.
char s[2][30];
Here, s can store 2 strings of maximum 30 characters each.
Initialization
2 ways
1. Using string constants
char s[2][20]={“Ram”, “Sam”};
2. Using initialization list.
char s[2][20]={ {‘R’, ‘a’, ‘m’, ‘\0’},
{‘S’, ‘a’, ‘m’, ‘\0’}};
E.g. Program
#include<stdio.h>
void main()
{
int i;
char s[3][20];
printf(“Enter Names\n”);
for(i=0;i<3;i++)
scanf(“%s”, s[i]);
printf(“Student Names\n”);
for(i=0;i<3;i++)
printf(“%s”, s[i]);
}