1
Strings in C
What are strings?
In simple language STRING'S are nothing but the character array. The declaration
of string(character array) is much similar to normal array declaration. Each string
is terminated by '\0' as indication of string termination. So obviously you will
require an extra byte of memory for storing '\0'.'\0' is single character whose ASCII
(American Standard Code for Information Interchange) value is 0.
A string is an array of characters terminated by a special character '\0' (null
character). This null character marks the end of the string.
Declaration- char string_name[size];
In the above syntax string_name is any name given to the string variable and size is
used to define the length of the string, i.e the number of characters strings will
store.
There is an extra terminating character which is the Null character (‘\0’) used to
indicate the termination of a string that differs strings from normal character
arrays.
char s[5];
In this example we have declared a string of 5 characters.
Initialization-
1. Assigning a string literal without size
char greeting[] = “Table”;
2. Assigning a string literal with a predefined size
char name[15]= “Programming”;
3. Assigning character by character with size
char site[7]={'g','o',o','g','l','e',’\0’};
4. Assigning character by character without size
char site[]={'g','o',o','g','l','e',’\0’};
char site[]={'g','o',o','g','l','e'};
2
How to take Input as string and print Output that string?
way 1:
void main()
{
char name[30];
printf("Enter your name");
scanf("%s", name); //format specifier
printf("%s", name);
}
way 2:
#include<string.h>
void main()
{
char name[30];
printf("Enter your name");
gets(name); //in-built function
puts(name);
}
Difference between both ways is that 1st way use's format specifier and second
way uses in-built function.
In 1st way if you Enter something like john karter it will take only john as string
and anything after (space) will be discarded. In second way if you enter any space
it will be accepted. Output of second way would be john karter.
String Input Using scanf("%[^\n]s")
We can also use scanf() to read strings with spaces by utilizing a scanset. We can
use scanf("%[^\n]s") as an alternative. It reads the characters until a newline
character ("\n") is encountered.
int main (){
3
char name[20];
printf("Enter a name: \n");
scanf("%[^\n]s", name);
printf("You entered \n");
printf("%s", name);
return 0;
}
gets()-The gets function is part of the <string.h> header file in C. Function gets
allows space-separated strings to be entered by the user. It waits until the newline
character \n or an end-of-file EOF is reached. The string is then stored in the form
of a character array.
fgets()- In newer versions of C, gets() has been deprecated. It is potentially a
dangerous function because it doesnt perform bound checks and may result in
buffer overflow.
Instead, it is advised to use the fgets() function.
The fgets() function can be used to accept input from any input stream, such as
stdin (keyboard) or FILE (file stream).
int main(){
char name[7];
printf("Enter a name:\n");
fgets(name, 10, stdin);
printf("You entered: \n");
printf("%s", name);
return 0;
}
puts()- The puts function adds a newline character to the end of the specified
argument and publishes it to the output stream.
fputs()-
int main (){
4
char name[20] = "Hello KCMT";
printf("With fputs(): \n");
fputs(name, stdout);
return 0;
}
String Handling Functions in C-
C programming language provides a set of pre-defined functions called string
handling functions to work with string values. The string handling functions are
defined in a header file called string.h. Whenever we want to use any string
handling function we must include the header file called “string.h”.
1) int strlen(char array):This function accepts string as parameter and return
integer i.e the length of String passed to it.
Syntax-
integer_variable = strlen(string or string_variable);
Example
1. #include <stdio.h>
#include <string.h>
void main()
{
char string[]="spark";
int len;
len=strlen(string);
printf("length of %s is %d\t", string, len);
}
Output::length of spark is 5.
Did you notice that strlen() does not include '\0' in string length or else length
would be 6.
2. void main()
{
char str[30];
int len;
5
printf("Enter string:\n");
gets(str);
len = strlen(str);
printf("Length of given string is: %d", len);
2) strcpy (Destination string,source string):This function accepts 2 strings as
parameter,1st one is destination string and 2nd is source string. This function
copies source string to destination string.
Syntax-
strcpy (string2, string1);
Example
1. #include <stdio.h>
#include <string.h>
void main()
{
char src[]="spark", dest[15];
strcpy(dest,src);
printf("%s is copied to dest string\t",dest);
}
Output: spark is copied to dest string.
2. #include<stdio.h>
#include<string.h>
void main()
{
char str1[30], str2[30];
printf("Enter string:\n");
gets(str1);
strcpy(str2, str1);
printf("Copied string is: %s”, str2);
}
6
3) strcat (Destination string,source string): String handling function strcat() is
used to concatenate two strings. Concatenation is the process of merging content of
one string to another string. Function strcat(str1, str2) concatenates content of
string str2 after content of string str1.
Syntax-
strcat( string1, string2);
Example-
1. #include <stdio.h>
#include <string.h>
void main() {
char str1[100] = "This is ", str2[] = "KCMT";
strcat(str1, str2);
puts(str1);
puts(str2);
}
2.
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50], str2[50];
printf("Enter first string:\n");
gets(str1);
printf("Enter second string:\n");
gets(str2);
strcat(str1,str2);
printf("Concatenated string is: %s", str1);
4)int strcmp (string 1, string2): This function compares two strings passed as
parameters and returns either +ve number,0,-ve number.
+ve value indicates string1 > string2.
0 indicates string1 and string2 are equal
7
-ve value indicates string1 < string2.
Syntax-
integer_variable = strcmp( string1, string2);
Example
#include <stdio.h>
#include <string.h>
void main(void)
{
char string1[]="spark",string2[]="programming";
int cmp;
cmp=strcmp(string1,string2);
if(cmp>0)
printf("%s > %s",string1,string2);
else
{
if(cmp<0)
printf("%s < %s",string1,string2);
else
printf("%s = %s",string1,string2);
}
}
Output: spark > programming.
This is because alphabetically p comes first then [Link] the string compare function
returns difference between ascii of s and p which would be +ve.
5) strcmpi (string 1, string2):This function is similar to strcmp().The only
difference is that it ignores the case. Example SparK and spark both are same.
Example
#include <stdio.h>
#include <string.h>
void main(void)
{
char string1[]="spark",string2[]="SPArk";
int cmp;
cmp=strcmpi(string1,string2);
if(cmp>0)
printf("%s > %s",string1,string2);
else
{
if(cmp<0)
printf("%s < %s",string1,string2);
else
8
printf("%s = %s",string1,string2);
}
}
Output: spark = SPArk.