strings_material_(2_classes).pdf

14
Subject: Data Structure with C 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

Upload: naveen-gowdru

Post on 02-Sep-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

  • Subject: Data Structure with C

    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].

    Figure 2: Variable length string

    String literal Representation of string constant or literal is also equally important. Perhaps it is a sequence of character which enclosed in doubled quotes. Let we consider an example VTU, Data structure with C. These literal are used in a program, C compiler automatically create an array of character. In similar way double quotes identify data always as string value. On the other hand to store single character there are two options, data as character literal or string literal.

    Character Literal and String literal The character literal is stored in memory with single quotes and it occupies a single memory location. The string manipulation operations such as moving a character from one location to another require only assignment.

    The string literal used the double quotes, one additional memory location essential for delimiter and moving string require a function call. Representation is also crucial even though data object is absence. In other words, logically we specify space or \0 represents the absence of data in String literal. String can be empty or string can be with no data. Consider an example a character - V , a string- V , a null string- . The following segment of program attempted to add more information using pointer data structure as portrayed in figure 3.

    String and Pointers { /* Printing the string char greeting[ ]=Hello; char *ptr; ptr=greeting;

  • while (*ptr != \0) { printf( %c, *ptr); ptr++; } print(\n); }

    Figure 3: Variable length string

    String Input and output functions Generally there are two way of read and write string. First one is to Read/ Write with formatted I/O function such as Scnaf & printf. Second is special set of string only functions gets/ fgets and puts/ fputs for input and output operations respectively. In the file process system operating system will encourage various operation. These operations may be read and write from or to a file. Similar concepts are further emphasized through a figure 4 which is logical organization of file process. Essence of the program and data are stored in memory through a file. In order to do so input and output functions essential and this is controlled by operating system.

  • Figure 4: Logical organization of file process Formatted String I/p - Flush In most of the time user enter too much of data with the width in the field, examples such as char month [10]; and scanf (%9s, month);

    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.

    Figure 5: function gets

    The fgets function reads from std i/p file stream char *gets(char *strPtr); fgets put it in memory the string and appends an end-of-string delimiter as shown in figure 6.

    Figure 6: function fgets

    It reads from stream specified from fp char *fgets(char *strPtr, int size,FILE *fp)

    puts and fputs In the similar way, output the data to outside world. These are null terminated and sting from memory and write to file as a line. These are also called string-to-line output functions as demonstrated in the figure 7.

    Figure 7: function puts

  • In the figure 8 fgets function is narrated which is add the newline

    Figure 8: function fgets

    Array of String We have been observing But, this approach waste the space. Ragged array common with strings-texual format Ragged array we create with pointer. It is much easier to create using array of string Pointers In this way, each string is independent. Though, we can pass them as a group of function by passing only the name of array of pointers

    Array of String: print the days of Week #include int main(void) { char *pDays[7]; char **pLast; char **pWalker; pDays[0]=Sunday; pDays[1]=Monday; pDays[2]=Tuesday; pDays[3]=Wednesday; pDays[4]=Thursday; pDays[5]=Friday; pDays[6]=Saturday; printf(the days of the week\n); pLast=pDays + 6; for (pWalker=pDays; pWalker

  • Figure 9: pointer to string

    Ragged Array Ragged array is quite productive, when size of data in array is variable. If you use 2D, lot of memory will be wasting. Instead of doing so better create five one dimensional arrays are joined through a array of pointers. Here, right element in each row may be empty, It even (ragged) right border. 2D array contain only. 2 rows are full; rest will contain 1 to 4 and it is illustrated in the figure 10.

    Figure 10: ragged array

  • String Manipulation Function It is not a standard type. Move individual element from source string to destination string. C has rich set of string functions. Besides, using these functions there is an opportunity to make efficient when operations are supported by hardware. The following function computes the length of the given string.

    String Length new_len(char *s) { int length=0; while (*s != \0) { length++; s++; } return(length) } string input = VTU Edusat Output = 10 (including null character)

    Similarly, following is the C programe using built in function and it return length of string excluding null. If string is empty return zero int strlen( const char *string);

    Program to add two space to left margin at beginning of each line before writing it to a file #include #include int main(void) { FILE *outFile; char strng[81]; If (!(outFile = fopen(p2.txt, w))) { printf(\ c could not open output file\n;} while (fgets (strng,sizeof(strang),stdin) ) { fputc( , outFile); fputc( , outFile); fputs(strng, outFile); if(strng[strlen(strng-1] != \n) fputs(\n,outFile); fclose(outFile) }

    Due to fgets(), some data may left in the buffer, but not necessary to FLUSH it, Because it require for write them to the file on the next line. Also fputs does not add newline when it writes the file

    String Length vs. Its Array Size

  • 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);

    Figure 11: string copy function

    String number copy Similarly, copy the string of certain length to destination and it is illustrated in the figure 12.

  • char *strncpy(char *to_string, const char *from_strng, int size);

    Figure 12: string number copy function newStrCpy same as strcpy( ) main() { char source[ ] = Edusat; char target[20]; newStrCpy(target, source); } newStrCpy( char *t, char *s) { while(*s != \0); { *t=*s; s++; t++; } *t = \0; }

    What would happen if s= s- 5; *s= O; Output: Odusat

    Character searching in String There are two string function to search a character in a string. The first one is called strchr ; it searches for the first occurrence of a character from the beginning of the string. The second one is strrchr which searches the string for the first occurrence beginning at the rear end working toward the front. In either case, if the character is located, the

  • function returns a pointer to it. If character is not in the string, they return NULL pointer. It is further illustrated in figure13.

    Figure 13: string search function 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, ;); }

  • Figure 14: string parsing function

    Parsing with String Token

    Figure 15: string parsing function

    String SPAN-strspn & strcspn The strspn function helps to determines the number of characters in the string set. Where as strcspn function stops at the first character that matches one of the characters in the

  • 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).

    Figure 16: string span function

    strtol- string to long function This function assist to converts a string to long integer. It skips leading white space characters and stops with the first non-numeric character, which is consider to be the start of trialing string. The address of trailing string stored in the second parameters, unless it is null pointer. A third parameter determines the base of the string number.

    long strtol (char *str, char **ptr, int base) ex: num= strtol(12345 Decimal constant :, &ptr, 0) output: Decimal constant : 12345

    num= strtol(11001 Binary constant :, &ptr, 2) output: Binary constant : 25

    num= strtol(13572 Octal constant :, &ptr, 8) Output: Octal constant : 6010

    Memory Formatting-Files in C The C compilers treat the keyboard as an input file, called stdin. It treats the screen as an output file, called stdout. So in C reading keyboard input is a special case of reading from a file, while outputting on the screen is a special case of writing to a file. This is why scanf, printf and the other file functions are in stdio header files. scanf & printf do not read/write files rather from memory gets & puts gives more control over formatting sscanf the i/p string.

    sscanf It is a one- to- many function and also it splits one string to many variables which is portrayed in figure 17.

  • Figure 17: sscanf function sprintf

    It is a many-to-one function and also it joins many pieces data into one string which is shown in figure 18.

    Figure 18: sprintf function

    Summary We have discussed most of the string related concepts such as C string concept

    String manipulation and Formatting I/O functions with relevant examples.