c Programming-chapter 07
c Programming-chapter 07
PRESENTED BY
SHEYONA G
CHAPTER-07
Handling of Character Strings
A string is a sequence of character enclosed between double quotation marks is known as string constant
or string literal.
Ex:
char city[10];
char name[20]
When compiler assigns a character string to a character array, it automatically supplies a null character (‘\
0’) at the end of the string.
INITIALIZING STRING VARIABLES:
char city[10]={ ‘N’, ‘e’, ‘w’, ‘ ’, ‘Y’, ‘o’, ‘r’, ‘k’, ‘\0’ };
the size of the string city is 9 bytes and the string “New York” contains 8 elements and one space is
provided for NULL(‘\0’) character.
char str[10] = “GOOD”;
the computer creates a character array of size 10, places the value “GOOD” in it, terminates with the
null character, and initializes all other elements to NULL.
READING STRINGS FROM TERMINAL
Using scanf() function :
The input function scanf() can be used with %s format specification to read in a string of
characters.
For example,
char address[10];
scanf(“%s”,address);
Char[5]=“NEW YORK”
N E W \0
If we want to read the entire line “NEW YORK”, it is better to use two arrays of appropriate sizes.
char a[5],b[5];
scanf(“%s%s”,a,b);
will assign the string “NEW” to a and “YORK” to b.
Otherwise use gets() function to read the line of text
WRITING STRINGS TO SCREEN
Otherwise we can use puts() for displaying the string into the screen.
String-handling functions
1. Strlen():
This function is used to find the length of the string.
int n=strlen(string);
Ex:
#include<stdio.h>
#include<string.h> Output:
void main() Enter any string:
{ System
char a[10]; int n; The length of the string is 6
printf("Enter any string:\n");
gets(a);
n=strlen(a);
printf("The length of the string is %d",n);
}
2. Strrev():
This function is used to reverse the given string and the result string will be stored in the same
string variable.
strrev(string);
Example:
#include<stdio.h> Output:
#include<string.h> Enter any string
void main() BCP
{ The reversed string is PCB
char a[10],b[10];
printf("Enter any string\n");
gets(a);
strrev(a);
printf("The reversed string is "); puts(a);
}
3. Strlwr():
This function is used to convert the string from upper case to lower case.
strlwr(string);
Example:
#include<stdio.h> Output:
#include<string.h> Enter any string
void main() BcP
{ The string in lower case is bcp
char a[10],b[10];
printf("Enter any string\n");
gets(a);
strlwr(a);
printf("The string in lower case is "); puts(a);
}
4. Strupr():
This function is used to convert the string from lower case to upper case.
strupr(string);
#include<stdio.h>
#include<string.h>
void main() Enter any string
{ bCp
The string in lower case is BCP
char a[10],b[10];
printf("Enter any string\n");
gets(a);
strupr(a);
printf("The string in upper case is ");
puts(a);
}
5. Strcat():
This function is used to join two strings together.
strcat(string1,string2)
Example:
#include<stdio.h>
#include<string.h> Enter two strings
void main() Very
{ good
char a[10],b[10]; The resultant string is Verygood
printf("Enter two strings\n");
gets(a);
gets(b);
strcat(a,b);
printf("The resultant string is ");
puts(a);
}
6. Strncat():
This will concatenate the left-most n characters of second string at the end of first string. This is a three
parameter string.
strncat(string1,string2,number);
Example:
#include<stdio.h>
#include<string.h> Enter two strings: Bala Gurusamy
void main() Enter the number of characters to be concatenated: 4
{ The resultant string is BalaGuru
char a[10],b[10];
int n;
printf("Enter two strings\n");
gets(a);
gets(b);
printf(“Enter the number of characters to be concatenated: ”);
scanf(“%d”,&n);
strcat(a,b,n);
puts(a);
}
7. Strcpy():
This function is used to copy one string to another.
strcpy(string1, string2);
Here string1 will be act as destination and string2 will be act as source. It means string2 will be copied into string1.
Example:
#include<stdio.h>
#include<string.h>
void main()
{
char a[10],b[10];
printf("Enter any string\n");
gets(a);
strcpy(b,a);
printf("The copied string is ");
puts(b);
}
8. Strncpy():
This function copies only the left most n characters of the source string to the target string. This is a
three parameter function.
strncpy(string1,string2,number);
Example:
#include<stdio.h>
#include<string.h>
Enter two strings
void main()
{
Balaguru
char a[10],b[10]; int n; samy
printf("Enter two strings\n"); Enter the number of characters to be copied: 3
gets(a); The resultant string is Samaguru
gets(b);
printf(“Enter the number of characters to be copied : ”);
scanf(“%d”,&n);
strncpy(a,b,n);
printf("The resultant string is ");
puts(a);
}
9. Strcmp():
This function compares two strings. This comparison is done by taking the numeric difference between the
characters in the strings. So this can return 3 kinds of values. They are
o Zero : if the strings are equal
o Positive value : if the first string is greater
o Negative value : if the second string is greater
}
10. Strncmp():
This function compares the left most n characters of strings. This is a three parameter function. This can
return 3 kinds of values.
o Zero : if the strings are equal
o Positive value : if the first string is greater
o Negative value : if the second string is greater
strncmp(string1,string2,number);
Example: Enter two strings: their there
#include<stdio.h> Enter the number of characters to be compared: 3
#include<string.h> The result is 0
void main() Here the first 3 characters of ‘a’ compares with that of ‘b’ and it
{ return ‘0’ because the
char a[10],b[10]; first three characters are same.
int n,x;
printf("Enter two strings : ");
gets(a);
gets(b);
printf(“Enter the number of characters to be compared :
”);
scanf(“%d”,&n);
x=strncmp(a,b,n);
printf("The result is %d ",x);
}
11. Strstr():
This is used to locate a sub string in a string
strstr(string1,string2);
Example: Output:
#include<stdio.h> #include<string.h> Enter first string:
void main() verygood
{ Enter second string:
char a[10],b[10]; good
printf("Enter first string:\n"); Substring found
gets(a);
printf(“Enter second string:\n”);
gets(b);
if(strstr(a,b)==’\0’)
printf(“Substring is not found”);
else
printf(“Substring found”);
}
ASSIGNMENTS
C language does not support ________datatype. string int char float
In C language strings are represented as _______ integer array character array zero float array
Which function is used to read a string with white space? gets() putchar() putc() puts()
Which amongst the header file contains gets ( ) function in C language. math.h string.h stdio.h process.h