STRINGS
STRINGS
G N D E C \0
0 1 2 3 4 5
The first element of a string is stored at 0 th position, the second element is stored at 1st
position and so on. In addition, the last character of the sting is stored at n-1 position, where n
is the total number of characters. Now, the difference between a numerical array and a
character array or string is that a string stores a NULL(‘\0’) character at the nth position,
while a numerical array does not store such a character. This means that, a character array or
string stores n+1 element, because the last character of the array is always NULL.
Declaring a string
Syntax:
char
Where, string_name[size];
string_name-> ame given to a string.
size-> size of the string.
Example:
char college[10];
Initializing a string
When you initialize a string variable by assigning character constants individually, you use
single quotes.
Now, when you initialize a string variable by assigning all the characters collectively, you use
double quotes.
Printing a string
It is possible to print a string using printf(). As long as the string is terminated by a NULL
character, the printf() function prints all the characters upto, but not including, the NULL
character. The printf() conversion specification for a string variable is %s.
You can use the printf() function to display the string data in two ways. Either you can pass
the string data directly within the printf() function or you can store the string data in a
character array and pass that array as a parameter in the printf() function.
Example:
It is possible to read a string using scanf() function. The conversion specification for reading
a string using scanf() is %s.
Note that the ‘&’ symbol is not used with the name of a string variable getting a value from
scanf().
The standard library string.h contains many functions for string manipulation. Among them
are the following.
strcpy() – allows us to give a string an initial value as a unit rather than character by
character.
strncmp() – allows us to compare a block of n characters from one string with those in
another.
strcat() – allows us to combine two strings.
The strcpy() function is used to copy the content of first string to second string or to assign a
value to a string. You have to pass two strings as an argument to the strcpy() function to copy
first string to second string. The strcpy() function will copy the entire string, including the
NULL character to the new location.
Syntax:
strcpy(dest,source);
Where,
Example:
char first[20];
char last[20];
strcpy(first,”Sourav Ganguly”);
strcpy(last,first);
strncpy() function
The strncpy() function is used to copy a specified number of characters from one string to
another string. This function takes three parameters, where two parameters are strings, which
have to be copied, and the third parameter is number of characters upto which the second
string is to be copied to the first string.
Syntax: strncpy(dest,source,
Where,
Example:
char first[20]=” Sourav Ganguly”;
char last[20];
strncpy(last,first,6);
The strlen() function can be used to find the length of the string in bytes upto NULL
character and excluding NULL character.
Syntax: length=strlen(str);
Where,
Example:
char college[10]=”GNDEC”;
len=strlen(college);
The sizeof() operator can be used to determine the declared size of a string. It accepts one
operand, a variable or a type, and returns the number of bytes allocated to it.
Syntax:
size=sizeof(str);
or
size=sizeof(typen
ame);
Where,
str -> it is a string.
typename -> any basic data type.
size -> return value. Number of bytes allocated to that type.
Example:
char college[10]=”GNDEC”;
size=sizeof(college);
size2=sizeof(char);
The strcmp() function is used to compare two string data. Strcmp() function takes two
parameter of strings for comparing. It returns an integer value indicating the relationship
between the strings. The returned value can be zero, greater than zero, or less than zero. A
zero value indicates that both strings are equal. A value greater than zero indicates that first
character that does not match has a greater value in string1 than in string2. Finally a value
less than zero indicate that the first character that does not match has lesser value in string1
than in string2.
Syntax:
result=strcmp(string1,string2);
Where,
Example:
1. char str1=”GNDEC”;
char str2=”GNDEC”;
int res;
res=strcmp(str1,str2); //res==0
2. res=strcmp(“cat”,”car”); //res>0
3. res=strcmp(“big”,”little”); //res<0
strncmp() function
The strncmp() function is used to compare two strings upto a specified number of characters.
This fuction takes three parameters, where two parameters are the strings, which have to be
compared, and the third parameter is the number of characters upto which the two strings are
compared.
Syntax:
result=strncmp(string1,string2,
Where,
Example:
char str1=”GNDEC”;
char str2=”GND”;
int res;
res=strncmp(str1,str2,3);
The strcat() function is used to concatenate two strings. The resulting string has only one
NULL character at the end. The strcat() function takes two parameters to add second strig to
the first string. This means that the second string is appended to the fist string and
concatenated string is stored in first string.
Syntax:
strcat(first,second);
Where,
Example:
char str1[20]=”GND”;
char str2[20]=”EC”;
strcat(str1,str2);
Syntax: puts(string);
puts() function takes one parameter string, tha is to be printed. After printing the
string, puts() prints the newline character.
Example:
1. puts(“Welcome to GNDEC”);
2. char str[20]=”GNDEC”;
puts(str);
The gets() function allows reading in an entire line of input, including whitespace characters.
It reads everything upto a newline character.
Syntax:
n=gets(string)
Where,
Example:
gets(str);
}
/*2. C-Pogram to implement strcpy() without using inbuilt function.*/
#include<stdio.h>
Void main()
{
char str1[20]={0},str2[20];
int i=0;
printf("enter a string2\n");
scanf(“%s”,str2);
while(str2[i]!='\0')
{
str1[i]=str2[i];
i++;
}
puts(str1);
puts(str2);
}