Strings
Strings
In C programming language, string is considered as a sequence or series of characters and treated as a single data item.
A string data is enclosed within the double quotes. It includes letters, symbols, digits, and control characters. Strings
are used in programs to store and process text data manipulation. Since string is a sequence or an array of characters, it
is declared as char (character) data type and an array variable. Turbo C has library routines for string manipulations
called <string.h>. Every time we want to use some of these string functions, we are required to include this string
function library to make our program runs properly.
1. getch() - a string input function. The function reads text from the keyboard until the Enter key is pressed.
Syntax: gets(string_var);
2. puts() - a string output function. This function displays the string value (which is stored from the string variable).
Syntax: puts(string_var);
Syntax: strlen(string);
4. strcpyO— this string function copies the content of string2 to string 1. The str1 and str2 can be a variable or a string
data (value).
Syntax: strcpy(str1,str2);
5. strcat() - this string function concatenates the strings. It appends (add/join) string2 to the end of string1.
Syntax: strcat(str1,str2);
6. strlwr() - this string function converts all the uppercase letters in string to lowercase.
Syntax: strlwr(str);
Syntax: strupr(str);
8. strrev() - this string function reverses all the characters in the string.
1
Syntax: strrev(str);
9. strcmp() - this string function compares two strings. If string1>string2, the function returns a positive value; if string
l<string2, the function returns negative value; if string1==string2, the function returns a zero (0) value.
Syntax: strcmp(str1,str2);
10. strcmpi( ) - this string function compares two strings and ignores whether an a uppercase or lowercase letters are
being compared. Lowercase and uppercase letters are treated equal or the same.
11 .strncpy() - this string function copies only a portion (size) of strings into string 1
12. toupper( ) - this string function converts an input lowercase letter into its uppercase equivalent.
Syntax: toupper(vletter);
13. tolower() - this string function converts an input uppercase letter into its lowercase equivalent.
Syntax: tolower(vletter);
Example 1:
This program demonstrates how to copy a string data into the variable.
Algorithm:
strcpy(gn,'Tara");
strcpy(fn,"Williams");
puts(gn);
puts(fn);
Solution:
#include <stdio.h>
#include<conio.h>
#include <string.h>
main()
{
char gn[10], fn[10] ;
clrscr() ;
strcpy(gn,"Tara") ;
strcpy(fn,"Williams");
puts(gn) ;
puts (fn) ;
getch(); •
}
2
Example 2
This program demonstrates how to copy a string data coming from keyboard input using the gets() function and prints
them.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
clrscr();
strcpy(cgn,gn);
strcpy(cfn,fn);
puts(cgn);
puts(cfn);
printf("%s %s",cgn,cfn);
getch();
}
Example 3
This program demonstrates how the value of string1 is overwritten by the value of string2.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
clrscr();
strcpy(str1,"I Love");
strcpy(str2,"Tara");
strcpy(str1,str2);
puts(str1);
puts(str2);
3
getch();
}
This program demonstrates how the value of strings are being converted into lowercase or uppercase and how it is being
reversed. Then it computes the length of string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
clrscr();
strcpy(str1,"Tara");
strcpy(str2,"Williams");
length1 = strlen(str1);
length2 = strlen(str2);
strcat(str1,str2);
strlwr(str1);
strupr(str2);
strrev(str2);
getch();
Sample 5: strcmp()
This program demonstrates how the string compare function (strcmp()) works.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
4
char str[10];
int c;
clrscr();
c =strcmp(str,"Tara");
if (c==0)
puts("Welcome to the system");
else
puts("Intruder detected!,Police, police!");
getch();
}
Sample 6: strcmpi();
This program demonstrates how strcmpi() works and how it differs from strcmp(). The strcmpi() ignores the lowercase
or uppercase difference between the two compared values. Capital letter and lowercase letters are treated equally and
the same.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str[10];
int c;
clrscr();
c =strcmpi(str,"Tara");
if (c==0)
puts("Welcome to the system");
else
puts("Intruder detected!,Police, police!");
getch();
}
Sample 7: strncpy();
This program demonstrates how strncpy() function works technically. This string function copies only a portion (a number
of characters) of the given string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
5
char target[15], source[15];
clrscr();
strcpy(target,"Taramylove!");
strncpy(source,target,7);
puts(source);
getch();
}
6
Lab Activity
1. Write a program using string functions that accepts a coded value of an item and display its equivalent tag price. The
base of the key is:
0 1 2 3 4 5 6 7 8 9
X C OM P U T E R S
2. Write a program using string function that determines if the input word is a palindrome. A palindrome is a word that
produces the same word when it is reversed.
3. Write a simple encryption program using string functions that apply the substitution method. Here is the given
Substitution Table.
Substitution Table:
A *
E $
I /
0 +
u -
4. Write a program using string functions that will accept the name of the country as input value and will display the
corresponding capital. Here is the list of the countries and their capitals.
COUNTRY CAPITAL
7
Canada Ottawa
United States Washington D.C.
U.S.S.R. Moscow
Italy Rome
Philippines Manila
5. Write a program that will accept the currency value and the name of the country and will display the equivalent in U.S.
dollar, based on the given list:
6. Write a program that takes nouns and forms their plurals on the basis of these rules:
7. Write a program that takes data, a word at a time and reverses the words of the line.
Quiz
1. Write a program using standard string functions that accepts a price of an item and display its coded value. The base
of the key is:
XCOMPUT ERS
0 1234 5 6789