Strings Material (2 Classes) PDF
Strings Material (2 Classes) PDF
Topic:
Strings
In this chapter we are emphasizing the reading and storing string and also manipulation
concepts through the following millstones.
The discussion starts with string concepts , strings in c programming language, input and
output functions, array of strings, string manipulation ,memory formatting and in
conclusion summarizing the overall concepts.
String Concepts
The series of character treated as a unit and string created in pascal is different
from C. In the figure 1 (a) and (b) are attempted to illustrate storing of string in the fixed
length format and further second approach is the under the variable length format, under
this there are two sub approaches. These are mainly length controlled and delimited.
Figure 1 (a) and (b): Fixed and variable length string representation
String Copy-Length Controlled
In variable length format, problems such as unequal string-array sizes can be controlled
with the string number copy function, which is strncpy. This function contains a
parameter that specifies the maximum number of characters that can be copied at a time
String concepts in C
C programming language supports the variable in length string structure and ends with
\0. A character require a memory location, where as character string require two
memory location. In fact string is not data type but it is a data structure.
Normally string stored in an array of character ends with delimiter. Character require
only one memory location but string character require two memory location. One for
data and one for delimiter. At this juncture, in our mind following doubts may ascend.
Such as, why do we need null character ? , string is not data, it is a data structure, Its
implementation is logical not physical, physical structure is the array in which data is
stored. Remedies for the doubts by addressing one by one in subsequent sections
Variable length string
In real life situations, string is mostly variable length structure. Thus we need to identify
the logical end of data within physical structure. If data length is not variable, then it is
not necessary of string data structure to store. They are easily stored in an array, end is
last element. It provides enough space for maximum length string and plus one. It is
always possible that structure will not be filled, so array will be null in the middle. In
such case part of array from beginning to the null as the string and rest will be ignored
and same concept is described through a figure 2. In other words any part of array of
character can be treated as a string as long as the string ends with null char, for example
consider character array length may be 16. It can be declared as char str[16].
examples
Here scanf will read up to nine characters then insert the null. If user accidentally inserts
more than 9 character, extra character will be left in the input output stream, it may cause
a problem. Of course it can be possible to clean the stream using FLUSH pre-processor.
FLUSH also the clean a new line, when user correctly enters data.
Formatted O/p: printf
C has three options left justify flag, width & precision flag and width used together.
Where as wiithout flag it is right justified, otherwise left justified. This can be further
elaborated through some statements of C along with outputs.
printf(|%30s|\n, This is the string);
output:
|
This is the string|
Printf(|%-30s|\n, This is the string);
output:
|This is the string
|
Printf(|%-15.14s|, 12345678901234567890);
output:
|12345678901234 |
Maximum characters are one less than the width to ensure a space between the string
and next column.
C String :gets & fgets
The set of strings functions which are read/ write string without formatting any data
These functions converts text file lines to string and string to text file. A line consist of
a string of character terminated by a newline character. These are called line-to-string
input functions
The string input functions gets converts the \n to \0 (neweline to end of string
character ). It is further highlighted through the figure 5, which is read the data from
keyboard stored in memeory.
The length of a string is the count of the number of characters preceding the first NULL
character. It is important to realize that this definition differs from the size of the array.
Ex: Consider the following declarations:
1. char name[8]='
Edusat'
'
;
The array size of 8 bytes appears in brackets; but the length of the inputted string is six
characters.
2. char name[6]={'
s'
,'
a'
,'
m'
,'
\0'
,'
\0'
,'
\0'
};
The size of the array is 6 bytes; but the declared string length is three charactersare
initilized; and the first NULL character terminates the string.
3. char name[6]={'
J'
,'
h'
,'
o'
,'
\0'
,'
n'
,'
n'
};
The size of the array is 6 bytes; the string length is three characters; and the NULL
character terminates the string.
String is the name of data structure. Character array is home for string data structure.
String Copy
Two string copy functions , strcpy & strncpy. The function attempted to describe the
copy of string from source to destination and it is shown in the figure 11.
char *strcpy(char *to_strng, const char *from_strng);
function returns a pointer to it. If character is not in the string, they return NULL pointer.
It is further illustrated in figure13.
String Parsing
Let we consider an example of parsing. Parse sum= sum +10; The semicolon serves
as the last token in the string. Each string token separated by white space. Delimiter set
includes semicolon and blank between tokens
char *strtok( char *string, const char *delimiters);
When delimiters finds, it terminates and find returns a null pointer. The following
programme further convinces the concepts using such functions also shown in figure 14
and figure 15.
Parsing with String Token
int main (void)
{ char *strng = sum = sum+10;; char *pToken;
int tokenCount=0;
pToken = strtok(strng,;);
While (pToken)
{
tokenCount++;
printf( pToken %2d contains %s\n);
tokenCount, pToken);
pToken = strtok ( NULL, ;);
}
set. If none of characters in the string match the set, it returns the length of the string and
these functions are illustrated in figure 16 (a) and (b).
sprintf
It is a many-to-one function and also it joins many pieces data into one string
which is shown in figure 18.