Strings Notes
Strings Notes
The group of characters, digits, and symbols enclosed with in double quotation marks are
called as strings.
Every string terminates with “\0‟ (NULL) character. The decimal equivalent value of null
is zero. We use strings generally to manipulate text such as words and sentences. The
common operations that we can perform on strings are:
The last character of string is always “\0‟ (NULL). But it is not necessary to write “\0‟
character at the end of the string. The compiler automatically puts “\0” at the end of the
string / character array.
The characters or elements of the string are stored in contiguous memory locations.
H E L L O \0
The size of the character array: The argument / size of the character array or string =
Number of characters in the string + NULL character.
Note:
1. If NULL character is not taken into account then the successive string followed by the
first string will be displayed.
2. If we declare size of a string equal to the number of characters. i.e., without taking the
NULL character in to account and print will display some garbage value followed by the
string.
To read strings from terminal we can use scanf ( ) function with “%s‟ format specifier.
scanf(“%s”, name);
Limitation: If we use scanf ( ) function for reading string in runtime it terminates its
input on the first white space it finds. The scanf ( ) function automatically appends NULL
character at the end of the input string, so we have to declare a (size of) character array which
can accommodate input string and NULL character.
(White space: blanks, tab space, carriage returns, new lines etc.)
We can use different format specifications to display strings in various formats according to
requirements.
Using puts ( ) function we can display the string without using any format specifier. This
function is available in the <stdio.h> header file in C library.
Using putchar ( ) function we can display one character at a time. So, by using this function
repeatedly with the help of a loop statement we can display total string.
for (i=0;i<5;i++)
putchar (name[i]);
String standard functions
The following table provides frequently used string handling functions supported by C
compiler.
FUNCTIONS DESCRIPTION
strlen ( ) Determines Length of a string
strcpy ( ) Copies a string from source to destination
strncpy ( ) Copies specified number of characters of a string to another string
strcmp ( ) Compares two strings (Discriminates between small and capital letters)
Compares two strings (Doesn‟t Discriminates between small and capital
stricmp ( )
letters)
strncmp ( ) Compares characters of two strings upto the specified length
Compares characters of two strings upto the specified length. Ignores
strnicmp ( )
case.
strlwr ( ) Converts upper case characters of a string to lower case
strupr ( ) Converts lower case characters of a string to upper case
strdup ( ) Duplicates a string
Determines first occurrence of a given character
strchr ( )
in a string
Determines last occurrence of a given character
strrchr ( )
in a string
strstr ( ) Determines first occurrence of a given string in another string
strcat ( ) Appends source string to destination string
strncat ( ) Appends source string to destination string upto specified length
strrev ( ) Reverses all characters of a string
strset ( ) Sets all characters of string with a given argument or symbol
Sets specified number of characters of string with a given argument or
strnset ( )
Symbol
strspn ( ) Finds up to what length two strings are identical
Searches the first occurrence of the character in a given string and then
strpbrk ( )
it display the string from that character.
1. strlen ( ) function: This function counts the number of characters in a given string.
Syntax:strlen(string); (or) strlen(“ string”);
void main( )
{
char a[80];
int len;
clrscr( );
printf(“enter string:”);
scanf(“%s”,a); // (or) get (a);
len=strlen(a);
printf(“\n Name = %s”, a);
printf(“\n Total no of char‟s = %d”, len);
getch( );
}
(OR )
void main()
{
int s;
clrscr( );
s=strlen(“KMIT”);
Output: Output:
2. strcpy ( ) function: This function copies the content of one string to another. Here
string1 is source string and string2 destination string. String1 and string2 are character
arrays. String1 is copied into string2.
void main( )
clrscr( );
strcpy(s2,s1);
getch( );
}
Output:-
given string =sachin copied string =sachin
3. strncpy ( ) function: This function performs the same task as strcpy ( ). The
difference between them is that the strcpy( ) function copies the whole string to
destination string. Whereas strncpy( ) function copies specified length of characters from
source to destination string.
strcpy(destination, source,n);
KMIT Page 77
Example Program: To copy source string to destination string up to a specified length.
void main()
int n;
clrscr( );
Output:
Destination string=wonderful
KMIT Page 78
4. strcmp ( ) function: This function is used to compare two strings identified by the
arguments and it returns a value “0‟ if they are equal. If they are not equal it will return
numeric difference (ASCII) between the first non-matching characters.
void main( )
Output:-
-1
KMIT Page 79
printf(“\n\n \t %d”, m);
if(m==0)
Output:-
void main( )
}
puts(“The two strings are Different”);
}
Output:-
Enter Source String: HELLO
Enter Target String: hello
The two strings are Identical.
6. strncmp ( ) function: This function is used to compare two strings. This function is same as strcmp( )
but it compares the character of the string to a specified length.
Example program:
void main( )
{
char sr[30], tr[30];
int s,n;
clrscr( );
printf (“\nEnter Source String:”);
gets(sr);
printf (“\nEnter Target String:”);
gets(tr);
Output:-
Enter
KMITSource String: goodmorning Enter Target String: goODNIGHT Page 81
Enter length up to which comparison is to be made:2 The two strings are Identical up to 2 characters.
7. strnicmp ( ) function: This function is used to compare two strings. This function is
same as strcmp( ) but it compares the character of the string to a specified length.
{
int s,n;
clrscr( );
gets(sr);
gets(tr);
scanf(“%d”,&n);
s=strnicmp(sr,tr,n);
if(s==0)
Output:-
8. strlwr ( ) function: This function can be used to convert any string to a lower
case. When you are passing any uppercase string to this function it converts into lower
Syntax: strlwr(string);
case.
strupr ( ) function: This function is the same as strlwr( ) but the difference is that
strupr( ) converts lower case strings to upper case.
Syntax: strupr(string);
void main ( )
clrscr( );
Output:-
SACHIN Karan
sachin KARAN
9. strdup( ) function: This function is used for duplicating a given string at the allocated
memory which is pointed by a pointer variable.
KMIT Page 83
Syntax: string2 = strdup(string1);
void main( )
clrscr( );
printf(“Enter text:”);
gets(s1);
s2 = strdup(s1);
getch();
Output:
void main( )
printf(“Enter text:”);
gets(s);
ch = getchar( );
chp = strchr(string,ch);
if(chp)
else
getch( );}
Output:
Character to find: r
11. strrchr( ) function: In place of strchr( ) one can use strrchr( ). The difference
between them is that the strchr( ) searches occurrence of character from the beginning of
the string where as strrchr( ) searches occurrence of character from the end (reverse).
Syntax: chp = strrchr(string, ch);
12. strstr( ) function: This function finds second string in the first string. It returns the
pointer location from where the second string starts in the first string. In case the first
occurrence in the string is not observed, the function returns a NULL character.
Syntax: strstr(string1,string2);
KMIT Page 85
void main( )
clrscr( );
printf(“Enter text:”);
gets(s1); printf(“\
nEnter text:”);
gets(s2);
chp = strstr(s1,s2);
if(chp)
#include<string.h>
KMIT Page 86
void main ( )
clrscr( );
getch( ); }
14. strncat( ) function: This function is the same as that of strcat( ) function. The difference between
them is that the former does the concatenation of two strings with another up to the specified length.
Here, n is the number of characters to append.
Example Program: To append 2nd string with specified no. of characters at the end of the string using
strncat( ) function.
#include<string.h>
void main ( )
{
int n;
clrscr( );
scanf("%d",&n);
strcat(s," ");
KMIT Page 87
strncat(s,a,n);
getch( );
}
Output:-
s= Hello a = KMIT
Syntax:strrev(string);
#include<string.h>
{
char s[30]=”hello”; clrscr( );
strrev(s);
getch( );
}
Output:-
s1=hello
s2=olleh
KMIT Page 88
void main( )
char t[30];
clrscr( );
printf(“enter string:”); scanf(“%s”, &t);// gets(t); printf(“\n\n given string = %s”,t); strrev(t);
printf (“\n\n reversed string = %s”,t); getch( );
}
Output:
enter string: abcdefgh given string = abcdefgh
16. strset( ) function: This function replaces every character of a string with the
symbol given by the programmer i.e. the elements of the strings are replaced with the
arguments given by the programmer.
Syntax: strset(string,symbol);
Example:
void main( )
clrscr( );
KMIT Page 89
puts(“Enter string:”);
gets(st);
Output:
17. strnset( ) function: This function is the same as that of strset( ). Here the
specified length is provided. Where, n is the no. of characters to be replaced.
Syntax: strnset(string,symbol,n);
Example:
void main( )
{
int n;
clrscr( );
puts(“Enter string:”);
gets(st);
KMIT Page 90
18. strpbrk( ) function: This function searches the first occurrence of the
character in a given string and then it display the string starting from that character.
Syntax: strpbrk(string1, string2);
Example Program: To print given string from first occurrence of given character.
void main( )
char *ptr;
KMIT Page 91
clrscr( );
Output:
Enter a character: d
char x = “a‟;
printf (“%d”,x);
KMIT Page 92
The above statement will display integer value 97 on the screen even
though x is a character variable, because when a character variable or
character constant is used in an expression, it is automatically converted
in to integer value by the system. The integer value is equivalent to
ASCII code.
Ex: int x;
x=‟z‟-1;
printf(“%d”,x);
The above statement is valid statement and that will display 121 one as result, because ASCII value of “z‟ is
atoi ( ) function:
This library function converts a string of digits into their integer values.
Ex: int x;
char no[5] = “2012”; x = atoi(no); printf(“%d”,x);
Array of Strings in C
Syntax:
Char variable_name[r]={list of strings};
KMIT Page 93
where
var_name is the name of the variable in C.
r is the maximum number of string values that can be stored in a string array.
c is the maximum number of character values that can be stored in each string array.
Example
int main()
{
char arr[3][10] = {"kmit", "college", "narayanguda"};
printf("String array Elements are:\n");
KMIT Page 94