Character Array and String
Character Array and String
String
C does not support string as a data type. String is represented as character type
array. It means in C, string variable is any valid C variable name declared as an
array of characters. The general syntax is char string_variable_name[size].
We can also declare the size of the string larger than the string size in the
initialization.
C Programming Page 1
Character Array and String
In the above case a character type arry of size 15 is created. The value “AMIT” is
placed in it and terminated by ‘\0’ character. The remaining elements are too
initialized by ‘\0’ character.
char Name[20];
Name= “AMIT”;
is not allowed and will result in compile time error. Also an array name cannot be
used as an operand on the L.H.S. of an assignment operator. Hence
char name[5];
C Programming Page 2
Character Array and String
The input function scanf() is used along with the format specification “%S” to read
a string of characters.
scanf(“%S”, Name);
Limitation
The major limitation with the function scanf() while reading string of characters
is that as soon as the first white space is encountered, it terminates the reading
process i.e. its input. A white space includes
Blank space
Tab space
Carriage return
Form feeds
New line etc.
If in the above example the keyboard input is AMIT KUMAR SINGH, then only the
string “AMIT” will be read into the array Name as the blank space after the string
“AMIT” is encountered, it will terminate the reading of the string.
If the keyboard input is AMIT KUMAR, then the string “AMIT” will be assigned to
Name1 and “KUMAR” will be assigned to Name2.
C Programming Page 3
Character Array and String
Example
char Line[100];
scanf(“%[^\n]”, Line);
printf(“%S”, Line);
The function getchar() is used to read a single character from keyboard. The
recursive use of the function getchar() allows us to read successive single
characters from input and place them into a character type array.
Example
char ch;
char Name[20];
for(int i=0;i<20,i++)
ch=getchar();
Name[i]=ch;
OR
char Name[20];
C Programming Page 4
Character Array and String
for(int i=0;i<20,i++)
Name[i]=getchar();
The function gets() is used to read a string of text containing white spaces. The
method is defined in the header file “stdio.h”, that has to be included. It accepts
one parameter which is of string type.
Example
char Name[20];
gets(Name);
it will read string of characters into character type array until it encounters a new
line character. Once the new line character is encountered, it appends a null
character is encountered, it appends a null character at the end to mark the “end
of the string”.
Displaying String
The output function printf() is used along with the format specification “%S” to
write a string of characters onto the monitor.
Example
char Name[20];
C Programming Page 5
Character Array and String
Scanf(“%S”, Name);
printf(“%S”,Name);
We can also specify the precision with which the array will be displayed.
Example
The function putchar() is used to output the values of character type variable i.e.
the output of single character onto monitor. The recursive use of the function
putchar() allow us to write successive single characters onto the output screen.
The function putchar() requires one character type parameter.
Example
char Name[20];
for(int i=0;i<20;i++)
Name[i]=getchar();
for(i=0;i<20;i++)
C Programming Page 6
Character Array and String
putchar(Name[i]);
The function puts() is used to print the string values. The header file “stdio.h” has
to be included. It accepts one parameter which is of string type. The general
syntax is puts(Name).
Where Name is the string type parameter/argument passed to the method puts().
It writes the contents of the string variable Name and moves the cursor to the
beginning of the next line on the screen.
Example
char Name[20];
gets(Name);
puts(Name);
C Programming Page 7
Character Array and String
Example
The characters from Name1 and Name2 should be copied into Name one after
the other. The size of 3rd array Name has to be large enough to hold total numbers
of characters from the strings Name1 and Name2.
Example
C Programming Page 8
Character Array and String
char Name3[50];
Name3[i]=Name1[i];
Name3[i+1= “ ”;
Name3[i+j+1]=Name2[j];
puts(Name3);
C does not permit the direct comparison between two strings. It means
if(str1==str2)
OR
C Programming Page 9
Character Array and String
Example
int i=0;
else
strlen()
Determines the length of the string.
The function counts the number of characters in the string passed as
argument. The general syntax is strlen(char *).
#include<string.h>
void main()
{
char str[20];
int len;
clrscr();
printf(“enter a string”);
C Programming Page 10
Character Array and String
gets(str);
len=strlen(str);
printf(“\n The length of the string is %d”, len);
}
strcpy()
Copies a string from the source to destination.
The function copies the contents of one string to the other. Two strings are
passed as argument to it. The contents of second argument (source) is
copied into first argument (destination). The general syntax is strcpy(char
*destination, char *source).
#include<string.h>
void main()
{
char str1[20], str2[20];
chrscr();
printf(“enter a string”);
gets(str1);
strcpy(str2,str1);
puts(str2);
}
strncpy()
Copies characters of a string to another string up to the specified length.
The function strncpy() is very much similar to the function strcpy() but the
difference is it copies specified length of characters from second argument
to first argument. The general syntax is (char *destination, char *source,
int n).
#include<string.h>
void main()
{
char str1[20], str2[20];
chrscr();
printf(“enter a string”);
gets(str1);
C Programming Page 11
Character Array and String
strcpy(str2,str1,10);
puts(str2);
}
strcmp()
Compare characters of two strings. The function is case sensitive.
The function strcmp() is used to compare two strings passed arguments and
returns 0 iff they are equal , otherwise returns a non zero value. This is
being done by comparing the ascii codes of same indexed characters of
both argument strings. If the difference between the two corresponding
ascii codes is 0, the comparison continues, but as soon the difference is non
zero, the comparison stops. If the returned value <0, the string passed as
first argument is alphabetically above the string passed as second
argument otherwise the string passed as second argument is alphabetically
above the string passed as first argument. The general syntax is strcmp(
char *s1, char *s2).
#include<string.h>
void main()
{
char str1[20], str2[20];
int i;
chrscr();
printf(“enter first string”);
gets(str1);
printf(“enter second string”);
gets(str2);
i=strcmp(str2,str1);
printf(“%d”, i);
}
stricmp()
Compares characters of two strings. The function is not case sensitive.
The function stricmp() is used to compare two strings passed arguments.
The function is non case sensitive and returns 0 or + 32 iff they are equal,
otherwise returns a non zero value. This is being done by comparing the
ascii codes of same indexed characters of both argument strings. If the
C Programming Page 12
Character Array and String
strncmp()
Compares characters of two strings up to the specifies length. The function
is case sensitive.
The function strncmp() is used to compare two strings passed arguments
up to specified length, and returns 0 iff they are equal , otherwise returns
a non zero value. This is being done by comparing the ascii codes of same
indexed characters of both argument strings. If the difference between the
two corresponding ascii codes is 0, the comparison continues, but as soon
the difference is non zero, the comparison stops. If the returned value <0
(!= -32), the string passed as first argument is alphabetically above the
string passed as second argument otherwise (!= +32) the string passed as
second argument is alphabetically above the string passed as first
argument. The general syntax is strncmp( char *s1, char *s2, int n).
#include<string.h>
void main()
C Programming Page 13
Character Array and String
{
char str1[20], str2[20];
int i;
chrscr();
printf(“enter first string”);
gets(str1);
printf(“enter second string”);
gets(str2);
i=strncmp(str2,str1,5);
printf(“%d”, i);
}
strnicmp()
Compares characters of two strings up to the specified length by ignoring
the case sensitivity.
The function strnicmp() is used to compare two strings passed arguments
up to specified length by ignoring the case sensitivity. The general syntax is
strnicmp( char *s1, char *s2, int n).
#include<string.h>
void main()
{
char str1[20], str2[20];
int i;
chrscr();
printf(“enter first string”);
gets(str1);
printf(“enter second string”);
gets(str2);
i=strnicmp(str2,str1,5);
printf(“%d”, i);
}
strupr()
Converts lowercase characters of a string to uppercase.
strlwr()
C Programming Page 14
Character Array and String
strdup()
Duplicates the string.
The function strdup() is used for duplicating a given string at the allocated
memory which is pointed by the pointer variable. The general syntax is char
*s2=strdup(char *s1).
#include<string.h>
void main()
{
char str1[20], str2[20];
int i;
chrscr();
printf(“enter the string”);
gets(str1);
str2=strdup(str1);
printf(“the duplicate string is:”);
puts(str2);
}
strchr()
Determine the first occurrence of a given character in a string.
The function returns the pointer position to the first occurrence of the
character passed as second argument within the string passed as first
argument. The general syntax is strchr(char *s, char ch).
#include<string.h>
void main()
{
char str1[20], ch;
int i;
chrscr();
printf(“enter the string”);
gets(str1);
i=strchr(str1,ch);
printf(“the required position is %d:i”);
}
C Programming Page 15
Character Array and String
Strrchar()
Determines the last occurrence of a given character in a string.
The function returns the pointer position to the last occurrence of the
character passed as second argument within the string passed as first
argument. The general syntax is strrchr(char *s, char ch).
#include<string.h>
void main()
{
char str1[20], ch;
int i;
chrscr();
printf(“enter the string”);
gets(str1);
i=strrchr(str1,ch);
printf(“the required position is %d:i”);
}
strstr()
Determines the first occurrence of a given string in a string.
The function returns the pointer position to the first occurrence of the
string passed as second argument within the string passed as first
argument. The general syntax is strstr(char *s1, char *s2).
#include<string.h>
void main()
{
char str1[20], str2[10];
int i;
chrscr();
printf(“enter the first string”);
gets(str1);
printf(“enter the second string”);
gets(str2);
i=strstr(str1,str2);
printf(“the required position is %d:i”);
}
C Programming Page 16
Character Array and String
strcat()
Appends source string to the destination string.
The function strcat() is used to join or concatenate two strings. The string
passed as second argument is appended to the string passed as first
argument. This is being done by removing the null character at the end of
the string passed as first argument and placing the string passed as second
argument. The general syntax is strcat(char *s1, char *s2).
#include<string.h>
void main()
{
char str1[20], str2[10];
chrscr();
printf(“enter the first string”);
gets(str1);
printf(“enter the second string”);
gets(str2);
strcat(str1,str2);
puts(str1);
}
strncat()
Appends source string to the destination string up to specified length.
The function strncat() is used to join or concatenate two strings. The
n(passed as third argument) characters of string passed as second
argument is appended to the string passed as first argument. This is being
done by removing the null character at the end of the string passed as first
argument and placing the string passed as second argument. The general
syntax is strcat(char *s1, char *s2, int n).
#include<string.h>
void main()
{
char str1[20], str2[10];
chrscr();
printf(“enter the first string”);
gets(str1);
C Programming Page 17
Character Array and String
strrev()
Reverses all the characters of a string.
The function strrev() is used to reverse the string passed as argument.
#include<string.h>
void main()
{
char str1[20];
chrscr();
printf(“enter the string”);
gets(str1);
puts(“the reverse of the string is”);
puts(strrev(str1));
}
strset()
Sets all characters of a string with a given argument or symbol.
The function strset() is used to replace every characters of the string passed
as first argument with the symbol passed as second argument. The general
syntax is strset(char *s, char symbol).
#include<string.h>
void main()
{
char str1[20];
char symbol;
chrscr();
printf(“enter the string”);
gets(str1);
printf(“enter the symbol”);
scanf(“%c”, &symbol);
puts(srtset(str1,symbol));
C Programming Page 18
Character Array and String
strnset()
Sets specified number of characters of a given string with given argument
or symbol.
The function strset() is used to replace specified number (passed as third
argument) characters of the string passed as first argument with the
symbol passed as second argument. The general syntax is strset(char *s,
char symbol, int n).
#include<string.h>
void main()
{
char str1[20];
char symbol;
chrscr();
printf(“enter the string”);
gets(str1);
printf(“enter the symbol”);
scanf(“%c”, &symbol);
puts(srtset(str1,symbol,5));
}
strspn()
Finds up to what length two strings are equal.
The function strspn() returns the position of the string from where source
string(passed as first argument) is not matching with destination
string(passed as second argument). The general syntax is strspn(char *s1,
char *s2).
#include<string.h>
void main()
{
char str1[20], str2[10];
int pos;
chrscr();
printf(“enter the first string”);
gets(str1);
C Programming Page 19
Character Array and String
strpbrk()
Searches the first occurrence of the character in a given string and then
displays the string starting from that character.
The function strpbrk() is used to search the first occurrence of the character
passed as second argument in the string passed as first argument and then
displays the string from that character.
#include<string.h>
void main()
{
char str1[20], ch, *str2;
chrscr();
printf(“enter the first string”);
gets(str1);
printf(“enter the character”);
gets(str2);
str2=strpbrk(str1,ch);
printf(str2);
}
C Programming Page 20
Character Array and String
printf(“%g”, d);
}
The string “101.12345” is passed as an argument to the function atof(),
which converts string to double. The converted double value is assigned to
the double type variable d and is then displayed.
C Programming Page 21
Character Array and String
{
const char *str=”101.12345 is the value”;
char *strp;
double d;
d=strtod(str,&strp);
printf(“%g”,d);
printf(“\n%s”, strp);
}
Memory Functions
memcpy()
copies n number of characters from one string to another.
The function memcpy() is used to copy specified number of characters from
source string (string passed as second argument) to destination string
(string passed as first argument). The number of characters is specified by
third argument. The general syntax is mewmcpy( char *s2, char *s1, int n).
void main()
{
char str1[20],str2[20];
printf(“\n Enter the string”);
gets(str1);
memcpy(str2,str1,10);
puts(str2);
}
memove()
moves a specified range of char from one place to another.
The function memove() is used to move specified range of characters from
given location.The general syntax is memove( char *s1, char *s2, int n). the
first argument is the string, second argument is the specified range of
characters(substring) to be moves and the third argument is the desired
location,
C Programming Page 22
Character Array and String
void main()
{
char str1[20];
printf(“\n Enter the string”);
gets(str1);
memove(str1,&str1[5],10);
puts(str1);
}
memchr()
searches for the first occurrence of the given character.
The function memchr() is used to search for the first occurrence of the
specified character within the given string. If the first occurrence of the
specified character is found, the remaining string is displayed.
void main()
{
char str[20],ch;
printf(“\n enter the string”);
gets(str);
printd(“\n enter the character to be searched”);
scanf(“%c”, &ch);
printf(memchr(str, ch,10);
}
memcmp()
compares the contents of memory.
The function memcmp() is used to compare the specified range of two
strings and the difference between the same indexed characters in both
strings is returned.
void main()
{
char str1[20];
char str2[20];
printf(“enter first string”);
gets(str1);
printf(“\nenter second string”);
C Programming Page 23
Character Array and String
gets(str2);
printf(“%d”, memcmp(str1,str2,10);
}
C Programming Page 24