Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- int getline(char s[], int lim)
- {
- int i;
- char c;
- for (i = 0; i<lim && (c = getchar()) != EOF && c != '\n'; ++i)
- s[i] = c;
- s[i] = '\0';
- while (c != EOF && c != '\n')
- c = getchar();
- return i;
- }
- int containsChar(char *strng, char c){
- int i;
- int strng_length = strlen(strng);
- for (i = 0; i < strng_length; i++)
- {
- if (strng[i] == c){
- return 1;
- }
- }
- return 0;
- }
- #define MAX 100
- char* removee(char *s, int i, int n){
- int s_length = strlen(s);
- int j;
- char result[MAX + 1];
- strcpy_s(result, MAX, s);
- if (s_length >= i + n){
- for (j = i; j < s_length - n; j++)
- {
- result[j] = result[j + n];
- }
- result[j] = '\0';
- return result;
- }
- else{
- return "";
- }
- }
- char* getLongestChildrenString(char *s1, char *s2, int current_longest){
- char result[MAX + 1];
- char temp_result[MAX + 1];
- char test[MAX + 1];
- char *in, s1_copy[MAX + 1], s2_copy[MAX + 1];
- int i;
- if (strlen(s1) == 0 || strlen(s2) == 0){
- return "";
- }
- if (current_longest >= strlen(s1) || current_longest >= strlen(s2)){
- return "";
- }
- for (i = 0; i < strlen(s1); i++)
- {
- if (containsChar(s2, s1[i]) == 0){
- strcpy_s(s1, MAX, removee(s1, i, 1));
- i--;
- }
- }
- for (i = 0; i < strlen(s2); i++)
- {
- if (containsChar(s1, s2[i]) == 0)
- {
- strcpy_s(s2, MAX, removee(s2, i, 1));
- i--;
- }
- }
- strcpy_s(result, MAX, "");
- in = strstr(s1, s2);
- if (in){
- return s2;
- }
- in = strstr(s2, s1);
- if (in){
- return s1;
- }
- for (i = 0; i < strlen(s1); i++)
- {
- strcpy_s(test, MAX, removee(s1, i, 1));
- strcpy_s(s2_copy, MAX, s2);
- strcpy_s(temp_result, MAX, getLongestChildrenString(test, s2_copy, current_longest >= strlen(result) ? current_longest : strlen(result)));
- if (strlen(temp_result) > strlen(result))
- {
- strcpy_s(result, MAX, temp_result);
- }
- }
- for (i = 0; i < strlen(s2); i++)
- {
- strcpy_s(test, MAX, removee(s2, i, 1));
- strcpy_s(s1_copy, MAX, s1);
- strcpy_s(temp_result, MAX, getLongestChildrenString(s1_copy, test, current_longest >= strlen(result) ? current_longest : strlen(result)));
- if (strlen(temp_result) > strlen(result)){
- strcpy_s(result, MAX, temp_result);
- }
- }
- return result;
- }
- int main(int argc, char *argv[]){
- char s1[MAX + 1];
- char s2[MAX + 1];
- char longest_child[MAX + 1];
- printf("Adja meg az elso sztringet: ");
- getline(s1, MAX);
- printf("Adja meg az masodik sztringet: ");
- getline(s2, MAX);
- strcpy_s(longest_child, MAX, getLongestChildrenString(s1, s2, 0));
- if (strcmp(longest_child, "") != 0){
- printf("A leghosszabb gyereksztring: %s\n", longest_child);
- }
- else{
- printf("Nincs a ket szonak kozos gyereksztringje!\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement