Characters and Strings
Characters and Strings
Initializing Strings
Following the discussion on characters arrays, the initialization of a string must the following form which
is simpler to one dimension array.
char month1[ ]={‘j’,’a’,’n’,’u’,’a’,’r’,’y’};
Then the string month is initializing to January. This is perfectly valid but C offers a special way to
initialize strings. The above string can be initialized char month1[]=”January”; The characters of the
string are enclosed within a part of double quotes. The compiler takes care of string enclosed within a
pair of a double quotes. The compiler takes care of storing the ASCII codes of characters of the string
in the memory and also stores the null terminator in the end.
/*String.c string variable*/
#include < stdio.h >
main()
{
char month[15];
printf (“Enter the string”);
gets (month);
printf (“The string entered is %s”, month);
}
In this example string is stored in the character variable month the string is displayed in the statement.
printf(“The string entered is %s”, month”);
It is one dimension array.
A string variable is any valid C variable name & is always declared as an array. The general form of
declaration of a string variable is
char string_name[size];
The size of the array should be one byte more than the actual space occupied by the string since the
compiler appends a null character at the end of the string.
Scanf statement has a drawback it just terminates the statement as soon as it finds a blank space,
suppose if we type the string new york then only the string new will be read and since there is a blank
space after word “new” it will terminate the string.
Note that we can use the scanf without the ampersand symbol before the variable name.
In many applications it is required to process text by reading an entire line of text from the terminal.
The function getchar can be used repeatedly to read a sequence of successive single characters and
store it in the array.
We cannot manipulate strings since C does not provide any operators for string. For instance we cannot
assign one string to another directly.
For example:
String=”xyz”;
String1=string2;
Are not valid. To copy the chars in one string to another string we may do so on a character to
character basis.
strlen() function:
This function counts and returns the number of characters in a string. The length does not include a null
character.
Syntax n=strlen(string);
Where n is integer variable. Which receives the value of length of the string.
Example
length=strlen(“Hollywood”);
The function will assign number of characters 9 in the string to a integer variable length.
/*write a c program to find the length of the string using strlen() function*/
strcmp function:
In c you cannot directly compare the value of 2 strings in a condition like if(string1==string2)
Most libraries however contain the strcmp() function, which returns a zero if 2 strings are equal, or a
non zero number if the strings are not the same. The syntax of strcmp() is given below:
Strcmp(string1,string2)
String1 & string2 may be string variables or string constants. String1, & string2 may be string variables
or string constants some computers return a negative if the string1 is alphabetically less than the second
and a positive number if the string is greater than the second.
Example:
strcmp(“Newyork”,”Newyork”) will return zero because 2 strings are equal.
strcmp(“their”,”there”) will return a 9 which is the numeric difference between ASCII ‘i’ and ASCII ’r’.
strcmp(“The”, “the”) will return 32 which is the numeric difference between ASCII “T” & ASCII “t”.
strcmpi() function
This function is same as strcmp() which compares 2 strings but not case sensitive.
Example
strcmpi(“THE”,”the”); will return 0.
strcpy() function:
C does not allow you to assign the characters to a string directly as in the statement name=”Robert”;
Instead use the strcpy(0 function found in most compilers the syntax of the function is illustrated below.
strcpy(string1,string2);
Strcpy function assigns the contents of string2 to string1. string2 may be a character array variable or a
string constant.
strcpy(Name,”Robert”);
In the above example Robert is assigned to the string called name.
strlwr () function:
This function converts all characters in a string from uppercase to lowercase.
syntax
strlwr(string);
For example:
strlwr(“EXFORSYS”) converts to Exforsys.
strrev() function:
This function reverses the characters in a string.
Syntax
strrev(string);
For ex strrev(“program”) reverses the characters in a string into “margrop”.1
strupr() function:
This function converts all characters in a string from lower case to uppercase.
Syntax
strupr(string);
For example strupr(“exforsys”) will convert the string to EXFORSYS.
/* Example program to use string functions*/
#include < stdio.h >
#include < string.h >
void main()
{
char s1[20],s2[20],s3[20];
int x,l1,l2,l3;
printf(“Enter the strings”);
scanf(“%s%s”,s1,s2);
x=strcmp(s1,s2);
if(x!=0)
{printf(“\nStrings are not equal\n”);
strcat(s1,s2);
}
else
printf(“\nStrings are equal”);
strcpy(s3,s1);
l1=strlen(s1);
1
Department of Computer Science C Programming
l2=strlen(s2);
l3=strlen(s3);
printf(“\ns1=%s\t length=%d characters\n”,s1,l1);
printf(“\ns2=%s\t length=%d characters\n”,s2,l2);
printf(“\ns3=%s\t length=%d characters\n”,s3,l3);
}
End