Escolar Documentos
Profissional Documentos
Cultura Documentos
RAPORT
Lucrare de laborator nr. 1 la Programarea Calculatoarelor
A efectuat: Vasilica Victor St. gr. FAF-111 A vericat: Mihail Kulev dr., conf. univ.
Chisinu 2011 ,
Topic: Working with strings. Processing symbols and lines in C language. Objective: Programming algorithms for processing symbols and lines, using standard
functions of processing symbols and lines in C language.
Work processing:
Short theory and methods used: Denition: String in C is a set of characters stored in array(static, dynamic) of char type, terminated by null byte. The preprocessor directives which work with strings is <ctype.h>(its function is to process character),<string.h>(string processing). In C language there is no data type string. Strings are stored in an array of char type along with the null terminating character "\0" at the end. In other words to create a string in C the computer creates an array of chars and set each element in the array to a char value that makes up the string. When sizing the string array it is needed to add plus one to the actual size of the string to make space for the null terminating character, "\0".
To initialize our "sname" string from above to store the name Victor ,
It can be observed from the above statement that initializing a string is same as with any array. However the string needs to be surrounded with quotes. To write strings to the terminal, we use a le stream known as stdout. The most common function to use for writing to stdout in C is the printf function. To print the above string it needs the following code:
Note: The name of the string will be constant pointer, if the string has static memory allocation.
Functions used:
strcpy(str1,str2) - string str2 is copied in str1, after str1 is destroyed. strncpy(str1,str2,n) - n characters from string str2 is copied in str1, after str1 is destroyed. strlen(str) - length of the string str without null byte. strcat(str1,str2) - joins str1 and str2 in str1 (appending). Puts str2 from null byte further. strcmt(str1,str2) - comparing 2 strings(compares ode of characters). strncmt(str1,str2) - The same as strcmt(str1,str2) but make dierence between lower and upper cases strupr(str), strlwr(str) -from lower to upper letters and from upper letter to lower letters. strtok(str) - Splits string in to tokens.
Algorithm Flowchart
Function "main" Start
"Enter sentence:"
ush(stdin)
Data Analyzing
In-put data: bu,string - simple variable of char type, the name of the array containing the sentence. Out-put data: string - simple variable of char type, the sentence without repeating words. Intermediate data: c - simple variable of integer type, variable needed to verify if the needed condition is satised. gets(bu)
strcpy(string,bu)
dc(bu,string,&c)
yes
getch
Stop
num = 0
l = 0; l < i; l++
i = 0
p = strtok(buf f, , . )
j = l + 1; j < i; j + +
p! = N U LL yes a[i] = p
no
k = strcmp(word, a[j])
k == 0 yes top
no
p = strtok(N U LL, )
a[i] = p
v = strstr(string, word)
else
break
Data Analyzing
In-put data: bu,string - simple variable of char type, the name of the array containing the sentence. Out-put data: ...
Intermediate data: l, i, j, k,ls, lw, nc - simple variable of integer type, variables needed to enter in loops and to nd the length of the sentence and corresponding word. v - simple variable of void type, variable used to nd the distance from beginning(which can be the last deleted word) to the next word needed word, a, p - simple variables of char type, variables used to process the string.
string [100];
p r i n t f ( " Enter s e n t e n c e : " ) ; fflush ( stdin ) ; gets ( buff ) ; strcpy ( string , buff ) ; dc ( b u f f , s t r i n g ,& c ) ; i f ( c==0){ p r i n t f ( "No word i s r e p e a t i n g . . . " ) ; } e l s e { p r i n t f ( "New S t r i n g : %s " , s t r i n g ) ; } getch () ; return 0 ; }
Result 1
Comments: In the gure above the sentence is "Eu ss merg ss acasa" where the word "ss" is repeating twice. As a result the word is deleted from the sentence. The new string can be seen above the rst one.
Result 2
Comments: In the second test 2 words are repeating "ss and sd". But still the result is as expected without them.
Result 3
Comments: In the last test there is no word repeating and the program is just telling the user about this.
Conclusion
1. Working with char data type is dierent from integer and oat. Operations used to work with char data type are sub-programs while operations used to work wits integer and oat are simple mathematical operations. 2. String processing is an very important process because most of the information in the internet and OS interfaces are in written format.
Biography
1. http://www.exforsys.com 2. http://www.cplusplus.com