Created
October 21, 2010 13:03
-
-
Save erenon/638442 to your computer and use it in GitHub Desktop.
[Proggyak I] Betűszűrő
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Betuszuro | |
* | |
* Beker ket szot, es kiirja azokat a karaktereket, | |
* melyek csak az elsoben szerepelnek | |
* | |
* @todo source_unique and source_filter violates DRY. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
/** | |
* Get source length | |
* | |
* @param char source[] source | |
* @return sources length | |
*/ | |
int source_length(char source[]) { | |
int i = 0; | |
while (source[i]) { i++; } | |
return i; | |
} | |
/** | |
* Makes all char unique in source | |
* | |
* @param char source[] source | |
* @todo refactor into source_filter | |
*/ | |
void source_unique(char source[]) { | |
int length, i = 0, j, newchar = 0, uindex = 0; | |
char *unique = NULL; | |
length = source_length(source); | |
unique = (char *)malloc(length * sizeof(char) ); | |
while (source[i]) { | |
newchar = 1; | |
for(j = i-1; j >= 0; j--) { | |
if (source[j] == source[i]) { | |
newchar = 0; | |
} | |
} | |
if (newchar) { | |
unique[uindex] = source[i]; | |
uindex++; | |
} | |
i++; | |
} | |
unique[uindex] = '\0'; | |
i = 0; | |
while (unique[i]) { | |
source[i] = unique[i]; | |
i++; | |
} | |
source[i] = '\0'; | |
free(unique); | |
} | |
/** | |
* Looks for filter's characters in source and removes them. | |
* | |
* @param char source[] source | |
* @param char filter[] characters to filter out | |
*/ | |
void source_filter(char source[], char filter[]) { | |
int slength, flength, i = 0, j, passed = 0, findex = 0; | |
char *filtered = NULL; //chars passed the filter | |
slength = source_length(source); | |
flength = source_length(filter); | |
filtered = (char *)malloc(slength * sizeof(char) ); | |
while (source[i]) { | |
passed = 1; | |
for(j = 0; j < flength; j++) { | |
if (source[i] == filter[j]) { | |
passed = 0; | |
} | |
} | |
if (passed) { | |
filtered[findex] = source[i]; | |
findex++; | |
} | |
i++; | |
} | |
filtered[findex] = '\0'; | |
i = 0; | |
while (filtered[i]) { | |
source[i] = filtered[i]; | |
i++; | |
} | |
source[i] = '\0'; | |
free(filtered); | |
} | |
/** | |
* Prints source's characters | |
* separated by spaces | |
* | |
* @param char source[] source to print | |
*/ | |
void source_print(char source[]) { | |
int i = 0; | |
while (source[i]) { | |
printf("%c ", source[i]); | |
i++; | |
} | |
if (i == 0) { | |
//source is empty | |
printf("Nincsenek ilyen betuk"); | |
} | |
} | |
/** | |
* Betuszuro | |
*/ | |
int main() { | |
char source[128], filter[128]; | |
printf("Elso szo, mely azon betuit keressuk...\n"); | |
scanf("%s", source); | |
printf("...melyek nincsenek benne a masodik szoban:\n"); | |
scanf("%s", filter); | |
source_unique(source); | |
source_filter(source, filter); | |
printf("A kovetkezo betuk felelnek meg a felteteleknek\n"); | |
source_print(source); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment