In this
tutorial you will learn about Initializing Strings, Reading Strings from the
terminal, Writing strings to screen, Arithmetic operations on characters,
String operations (string.h), strlen() function, strcat() function, strcmp
function, strcmpi() function, strcpy()
function, strlwr () function, strrev()
function and strupr() function
A
string is a sequence of characters. Any sequence or set of characters defined
within double quotation symbols is a constant string. In c it is required to do
some meaningful operations on strings they are:
·
·
Combining or concatenating strings
·
Copying one string to another.
·
Comparing string & checking whether they are
equal
·
Extraction of a portion of a string
Strings
are stored in memory as ASCII codes of characters that make up the string
appended with ‘\0’(ASCII value of null). Normally each
character is stored in one byte, successive characters
are stored in successive bytes.
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’,’\0’};
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*/ |
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. Each character occupies a byte. A null character (\0) that
has the ASCII value 0 terminates the string. The figure shows the storage of
string January in the memory recall that \0 specifies a single character whose
ASCII value is zero.
Character string terminated by a null
character ‘\0’.
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 determines the number of characters in the string name.
Example:
Char month[10];
char address[100];
The
function scanf with %s format specification is needed
to read the character string from the terminal.
Example:
Char address[15];
scanf(“%s”,address);
Scanf statement has a draw back 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 gets can be used repeatedly to read a sequence of successive single
characters and store it in the array.
The printf
statement along with format specifier %s to print
strings on to the screen. The format %s can be used to display an array
of characters that is terminated by the null character for example printf(“%s”,name); can be used to
display the entire contents of the array name
We can
also manipulate the characters as we manipulate numbers in c language. When
ever the system encounters the character data it is automatically converted
into a integer value by the system. We can represent a
character as a interface by using the following
method.
X=’a’;
Printf(“%d\n”,x);
Will display 97 on the screen.
Arithmetic operations can also be performed on characters for example x=’z’-1;
is a valid statement. The ASCII value of ‘z’ is 122 the statement the therefore
will assign 121 to variable x.
It is
also possible to use character constants in relational expressions for example
ch>’a’ && ch < = ’z’ will check whether the character stored in
variable ch is a lower case letter. A character digit can also be converted
into its equivalent integer value suppose un the
expression a=character-‘1’; where a is defined as an integer variable &
character contains value 8 then a= ASCII value of 8 ASCII value ‘1’=56-49=7.
We can
also get the support of the c library function to converts a string of digits
into their equivalent integer values the general format of the function in x=atoi(string) here x is an integer variable & string is
a character array containing string of digits.
C
language recognizes that string is a different class of array
by letting us input and output the array as a unit and are terminated by
null character. C library supports a large number of string handling functions
that can be used to array out many o f the string manipulations such as:
·
Length (number of characters in the string).
·
Concatentation (adding
two are more strings)
·
Comparing two strings.
·
Substring (Extract substring from a given
string)
·
Copy(copies one string over another)
To do
all the operations described here it is essential to include string.h library header file in the program.
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(“
The
function will assign number of characters 9 in the string to a
integer variable length.
|
/*writr a c program to find the
length of the string using strlen() function*/ |
strcat()
function:
when you combine two strings, you add the
characters of one string to the end of other string. This process is called
concatenation. The strcat() function joins 2 strings together. It takes the following
form
strcat(string1,string2)
string1
& string2 are character arrays. When the function strcat
is executed string2 is appended to string1. the string
at string2 remains unchanged.
Example
strcpy(string1,”sri”);
strcpy(string2,”Bhagavan”);
Printf(“%s”,strcat(string1,string2);
From
the above program segment the value of string1 becomes sribhagavan.
The string at str2 remains unchanged as bhagawan.
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”.
This
function is same as strcmp() which compares 2 strings but not case sensitive.
Example
strcmpi(“THE”,”the”); will return 0.
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.
This
function converts all characters in a string from uppercase to lowercase.
syntax
strlwr(string);
For example:
strlwr(“EXFORSYS”)
converts to exforsys.
This
function reverses the characters in a string.
Syntax
strrev(string);
For ex strrev(“program”)
reverses the characters in a string into “margrop”.
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*/ |
In this
tutorial you will learn about C Programming - File management in C, File
operation functions in C, Defining and opening a file, Closing a file, The getw and putw functions, The fprintf & fscanf functions,
Random access to files and fseek function.
C
supports a number of functions that have the ability to perform basic file
operations, which include:
Naming a file
Opening a file
Reading from a file
Writing data into a file
Closing a file
|
Function Name |
Operation |
|
fopen() |
Creates
a new file for use |
|
fclose |
Closes
a file which has been opened for use |
|
getc() |
Reads
a character from a file |
|
putc() |
Writes
a character to a file |
|
fprintf() |
Writes
a set of data values to a file |
|
fscanf() |
Reads
a set of data values from a file |
|
getw() |
Reads
a integer from a file |
|
putw() |
Writes
an integer to the file |
|
fseek() |
Sets
the position to a desired point in the file |
|
ftell() |
Gives
the current position in the file |
|
rewind() |
Sets
the position to the begining of the file |
If we
want to store data in a file into the secondary memory, we must specify certain
things about the file to the operating system. They include the fielname, data structure, purpose.
The
general format of the function used for opening a file is
FILE *fp;
fp=fopen(“filename”,”mode”);
The
first statement declares the variable fp as a pointer
to the data type FILE. As stated earlier, File is a structure that is defined
in the I/O Library. The second statement opens the file named filename and
assigns an identifier to the FILE type pointer fp.
This pointer, which contains all the information about the file, is
subsequently used as a communication link between the system and the program.
The second statement also specifies the purpose of opening the file. The mode
does
this job.
R open the file for
read only.
W open the file for writing only.
A open the file for appending data to it.
Consider
the following statements
FILE *p1,
*p2;
p1=fopen(“data”,”r”);
p2=fopen(“results”,”w”);
In
these statements the p1 and p2 are created and assigned to open the files data
and results respectively the file data is opened for reading and result is
opened for writing. In case the results file already exists, its contents are
deleted and the files are opened as a new file. If data file
does not exist error will occur.
The
input output library supports the function to close a file; it is in the
following format.
fclose(file_pointer);
A file must be closed as soon as all operations on it have been completed. This
would close the file associated with the file pointer.
Observe the following program.
FILE
*p1 *p2;
p1=fopen (“Input”,”w”);
p2=fopen (“Output”,”r”);
….
…
fclose(p1);
fclose(p2)
The
above program opens two files and closes them after all operations on them are
completed, once a file is closed its file pointer can be reversed on other
file.
The getc and putc functions
are analogous to getchar and putchar
functions and handle one character at a time. The putc
function writes the character contained in character variable c to the file
associated with the pointer fp1. ex putc(c,fp1); similarly getc
function is used to read a character from a file that has been open in read
mode. c=getc(fp2).
The
program shown below displays use of a file operations.
The data enter through the keyboard and the program writes it. Character by character, to the file input. The end of the
data is indicated by entering an EOF character, which is control-z. the file input is closed at this signal.
#include< stdio.h >
main()
{
file *f1;
printf(“Data input output”);
f1=fopen(“Input”,”w”);
/*Open the file Input*/
while((c=getchar())!=EOF) /*get a character from key
board*/
putc(c,f1); /*write a character to input*/
fclose(f1); /*close the file input*/
printf(“\nData output\n”);
f1=fopen(“INPUT”,”r”);
/*Reopen the file input*/
while((c=getc(f1))!=EOF)
printf(“%c”,c);
fclose(f1);
}
These
are integer-oriented functions. They are similar to get c and putc functions and are used to read and write integer
values. These functions would be usefull when we deal
with only integer data.
The fprintf and scanf functions are
identical to printf and scanf functions except that
they work on files. The first argument of theses functions is a file pointer
which specifies the file to be used. The general form of fprintf
is
fprintf(fp,”control string”, list);
Where fp id a file pointer
associated with a file that has been opened for writing. The
control string is file output specifications list may include variable,
constant and string.
fprintf(f1,%s%d%f”,name,age,7.5);
Here
name is an array variable of type char and age is an int variable
The
general format of fscanf is
fscanf(fp,”controlstring”,list);
This
statement would cause the reading of items in the control string.
Example:
fscanf(f2,”5s%d”,item,&quantity”);
Sometimes
it is required to access only a particular part of the and not the complete
file. This can be accomplished by using the following function:
1 > fseek
The
general format of fseek function is a s follows:
fseek(file pointer,offset, position);
This
function is used to move the file position to a desired location within the
file. Fileptr
is a pointer to the file concerned. Offset is a number or variable of type
long, and position in an integer number. Offset specifies the number of
positions (bytes) to be moved from the location specified bt the position. The position can take the 3 values.
Value
Meaning
0 Beginning of the file
1 Current position
2 End of the file
A
unique feature of c language is the preprocessor. A program can use the tools provided
by preprocessor to make his program easy to read, modify, portable and more
efficient.
Preprocessor is a program that processes
the code before it passes through the compiler.
It
operates under the control of preprocessor command lines and directives.
Preprocessor directives are placed in the source program before the main line
before the source code passes through the compiler it is examined by the
preprocessor for any preprocessor directives. If there is any appropriate
actions are taken then the source program is handed over to the compiler.
The
preprocessor offers several features called Preprocessor directives
.Preprocessor directives follow the special syntax rules and begin with the
symbol # and do not require any semicolon at the end. A set of commonly used
preprocessor directives
|
Directive |
Function |
|
#define |
Defines
a macro substitution |
|
#undef |
Undefined
a macro |
|
#include |
Specifies
a file to be included |
|
#ifdef |
Tests
for macro definition |
|
#endif |
Specifies
the end of #if |
|
#ifndef |
Tests
whether the macro is not def |
|
#if |
Tests
a compile time condition |
|
#else |
Specifies
alternatives when # if test fails |
The preprocessor directives can be divided into three
categories
1. Macro substitution /expansion
directive
2. File inclusion directive
3. Conditional Compiler directive
Macro
substitution is a process where an identifier in a program is replaced by a pre
defined string composed of one or more tokens we can use the #define statement for
the task.
It has
the following form
The
preprocessor replaces every occurrence of the identifier the source code by a string. The
definition should start with the keyword #define and should follow on
identifier and a string with at least one blank space between them. The string
may be any text and identifier must be a valid c name.
There
are different forms of macro substitution. The most common form is
1.Simplemacrosubstitution
2.Argumentmacrosubstitution
Simple
string replacement is commonly used to define constants example:
#define
pi 3.1415926
Writing
macro definition in capitals is a convention not a rule a macro definition can
include more than a simple constant value it can include expressions as well.
Following are valid examples:
#define
AREA 12.36
#include<conio.h>
#include<stdio.h>
#define kimt 10
void main()
{
int i;
clrscr();
for(i=0;i<kimt;i++)
printf("%d\n",i);
getch();
}
The
preprocessor permits us to define more complex and
more useful form of replacements it takes the following form.
#
define identifier(f1,f2,f3…..fn) string.
Notice
that there is no space between identifier and left parentheses and the
identifier f1,f2,f3 …. Fn is
analogous to formal arguments in a function definition.
There
is a basic difference between simple replacement discussed above and
replacement of macro arguments is known as a macro call
A
simple example of a macro with arguments is
#
define CUBE (x) (x*x*x)
If the
following statements appears later in the program,
volume=CUBE(side);
The
preprocessor would expand the statement to
volume =(side*side*side)
#include<conio.h>
#include<stdio.h>
#define cube(x) (x*x*x)
void main()
{
int i;
clrscr();
i=cube(5);
printf("%d\n",i);
getch();
}
A
defined macro can be undefined using the statement
# undef identifier.
This is
useful when we want to restrict the definition only to a particular part of the
program.
The
preprocessor directive "#include file name” can be used to include any
file in to your program if the function s or macro definitions are present in
an external file they can be included in your file
In the
directive the filename is the name of the file containing the required definitions
or functions alternatively the this directive can take the form
#include<
filename >
Without double quotation marks. In
this format the file will be searched in only standard directories.
The c preprocessor also supports a more general form of test condition #if
directive. This takes the following form
#Ifconstantexpression
{
statement-1;
statemet2’
….
….
}
#endif
the constant expression can be a logical expression such as test < = 3 etc
If the result of the constant
expression is true then all the statements between the #if and #endif are included for processing otherwise they are
skipped. The names TEST LEVEL etc., may be defined as
macros.
Conditional Compiler
directive
C preprocessor provides a conditional compilation directive
which is used to select alternate segment of code in a c program depending upon
the condition.
1. #ifdef #else
#endif
2. #ifndef #else
#endif
3. #if
#else #endif
The #ifdef #else
#endif directive allows us to skip part of the
source code.The genral form
of this preprocessor directive is:
#ifdef macroname
statement1;
#else
statement2;
#endif
In case we have used #define for macro_name
then statement1 will be compiled otherwise statement2.
#include<conio.h>
#include<stdio.h>
#define test
void main()
{
clrscr();
#ifdef
test
printf("kimt");
#else
printf("mbd");
#endif
getch();
}
The #ifndef #else
#endif directive works exactly opposite to the
directive #ifdef
#else #endif.The
genral form of this preprocessor directive is:
#ifndef macroname
statement1;
#else
statement2;
#endif
In case we have not used #define for macro_name then statement1 will be compiled otherwise
statement2.
#include<conio.h>
#include<stdio.h>
#define test
void main()
{
clrscr();
#ifdef
test
printf("kimt");
#else
printf("mbd");
#endif
getch();
The #if #else #endif directive
tests an expression for non zero value and performs the conditional
compilation. The genral form of this preprocessor
directive is:
#if constant expression
statement1;
#else
statement2;
#endif
In case the constant expression following #if is true
(non-zero) then statement1 is compiled otherwise statement2.
#include<conio.h>
#include<stdio.h>
#define int test=6
void main()
{
clrscr();
#if test<5
printf("kimt");
#else
printf("mbd");
#endif
getch();
}
·
math.h
·
stdio.h
|
getc (macro) getchar (macro) printf, fprintf, sprintf, snprintf (non-standard)
putc (macro) putchar,
fputchar (non-standard) (macro) |
scanf, fscanf, sscanf, vfscanf (non-standard), vscanf,
vsscanf |
·
stdlib.h
|
itoa (non-standard) ltoa (non-standard) |
·
string.h