Você está na página 1de 7

MINISTERUL EDUCATIEI REPUBLICII MOLDOVA ,

UNIVERSITATEA TEHNIC A MOLDOVEI


Facultatea Calculatoare, Informatic i Microelectronic Filiera Anglofon

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 ,

Laboratory Work No. 6

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.

Condition of the problem:


For a gine set: Variant 15. Remove from sentance the identical words.

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

Sample code : char sname [ 7 ] ;

To initialize our "sname" string from above to store the name Victor ,

Sample code : char sname [ 3 1 ] = { " V i c t o r " } ;

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:

Sample code : p r i n t f ( " F i r s t Name : %s " , sname ) ;

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)

"No word is repeating . . ."

yes

c == 0 no "New String: %s", string)

getch

Stop

Function "dc" Entrance

num = 0

l = 0; l < i; l++

i = 0

word = a[l] exit

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, )

ls = strlen(string) i++ lw = strlen(word)

a[i] = p

v = strstr(string, word)

yes v! = N U LL nc = (unsignedint)v (unsignedint)string strncpy(sc,string,nc); sc[nc 1] = \0

else

break

strncat(sc, (v + lw),(ls - lw)) strcpy(string, sc) goto top num = num + 1

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.

Text of the program in C language:


Text of the function "main"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include<s t d i o . h> #include<s t d l i b . h> #include<c o n i o . h> #include<s t r i n g . h> #include<c t y p e . h> void dc ( char b u f f , char s t r i n g , int num) ; int main ( ) { int c ; char buff [100] ,

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 ; }

Text of the function "dc"


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 void dc ( char b u f f , char s t r i n g , int num) { char word , a [ 5 0 ] , p , s c [ 1 0 0 ] ; int l , i , j , k , l s , lw , nc ; void v ; i =0; num=0; p=s t r t o k ( b u f f , " ,. " ) ; while ( p!= NULL) { a [ i ]=p ; p=s t r t o k (NULL, " " ) ; i ++;} f or ( l =0; l <i ; l ++){ word=a [ l ] ; f or ( j=l +1; j <i ; j ++){ k=strcmp ( word , a [ j ] ) ; i f ( k==0){ top : l s = s t r l e n ( s t r i n g ) ; lw = s t r l e n ( word ) ; v = s t r s t r ( s t r i n g , word ) ; i f ( v != NULL) { nc = ( unsigned int ) v (unsigned int ) s t r i n g ; s c [ 0 ] = \0 ; s t r n c p y ( sc , s t r i n g , nc ) ; s c [ nc 1 ] = \0 ; s t r n c a t ( sc , ( v + lw ) , ( l s lw ) ) ; strcpy ( string , sc ) ; num=num+1;} e l s e break ; goto top ; } } } }

Input data, results and comments

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

Você também pode gostar