-3

how can i write following programm:

• a function which searches a string in a second string and returns the endindex of the first occurence

• a function which includes all occurences of the first string in the second string, which will be replaced by a third string

• a main function which scans the three strings and puts the string out within the replacement

it is not allowed to use more headers than stdio.h , stdlib.h and string.h

i could only start with a code like this, as you can see i couldnt solve any of the three problems. i hope you can help me.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int endIdx(char *string1, char *string2, char *occu)
{

    for (int i = 0; i < strlen(string1); i++)
    {
        for (int j = 0; j < strlen(string1); j++)
        {
            if (string1[i] == string2[j])
            {
                printf("occurence string1[%d] is in string2[%d]\n", i, j);
                occu[j] = string1[i];
            }
        }
    }
    return 0;
}


int main()
{
    char string1[20];
    char string2[20];
    char occu[20];

    printf("Type in the first string: ");
    scanf("%s", string1);

    printf("Type in the second string: ");
    scanf("%s", string2);

    endIdx(string1, string2, occu);

    printf("%s\n", occu);

}
Sedem
  • 47
  • 1
  • 9
  • Please ask a more specific question. Are you having issues with your current code? What is it's behaviour and what specific problem are you unable to solve with respect to that code? – kaylum Feb 08 '17 at 19:49
  • 1. i dont know how to print out the endindex of the first occurence. i could solve it with breaking out of the for loop but then i have to put all occurences into a third string. so i couldnt go on because with the break, i could not "collect" all occurences – Sedem Feb 08 '17 at 19:51
  • If you can `#include ` and use `strlen()` then you can use `strstr()` which will easily solve the first problem with some pointer arithmetic added. In the second point, what should happen if the matching substrings overlap? If the first match takes priority, what if there is *still* an overlap after replacement? – Weather Vane Feb 08 '17 at 19:52
  • ok, can you just describe, how i can solve the problem with strstr() ? i think i can solve the rest when i find out how to solve the first point corectly – Sedem Feb 08 '17 at 19:54
  • `strstr` returns a pointer to the first occurence of the substring. Then some arithmetic to work out the end index of that. Over to you ;) .... but if you are supposed to *replace* a substring with another, you cannot easily do that without creating a new string, if the replacement has different length.. – Weather Vane Feb 08 '17 at 19:56
  • substring is not a problem, all strings are allowed to have the same size. my problem is now the strstr function. i try to solve it now. thank you :) – Sedem Feb 08 '17 at 20:03
  • if my strings are like this : string1 = "hellotxk" and string2 = "stbcwert" then what ist the endindex of the first occurence? maybe i dont understand this part. can somebody tell me this? – Sedem Feb 08 '17 at 20:21
  • `"stbcwert"` does not occur in `"hellotxk"`, so the result should indicate a "not found"-situation. A solution would be to return `-1` if string2 is not contained in string1. – Stephan Lechner Feb 08 '17 at 20:28
  • thanks, can you give me an example of two strings pleace? – Sedem Feb 08 '17 at 20:30
  • You are quite new to programming at all, right? Not a problem, of course. Let me help you with `endidx`; maybe you can get then ahead with the replace-thing. Await an aswer, please... – Stephan Lechner Feb 08 '17 at 20:34

1 Answers1

0

The following program uses strstr to implement your endIdx-problem. strstr returns a pointer to the first occurrence of searchTeram in str, or NULL, if it does not occur.

In the first case, we get a start index by taking occurrence - str, i.e. the memory address of the first occurrence minus the memory address where str starts. Adding then the length of searchTerm gives the end index.

In the second case, we simply return -1:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

long endIdx(const char *str, const char *searchTerm) {
    long result = -1;
    const char* occurrence = strstr(str,searchTerm);
    if (occurrence != NULL) {
        result = (occurrence - str) + strlen(searchTerm);
    }
    return result;
}

int main(void)
{
    // ----------------012345678901234
    const char* str = "Hello world! world is beautiful!";
    const char* searchTerm = "world";

    long endidx = endIdx(str, searchTerm);
    printf("endidx (should be 11): %ld\n", endidx);

    endidx = endIdx(str, "not to be contained");
    printf("endidx (should be -1): %ld\n", endidx);

    return 0;
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • thank you very much, i have one question. you said it should be 11. but at this point there is "!". at [10] there is the last "d" of "hello world". is that not the endindex of str? – Sedem Feb 08 '17 at 20:48
  • I was not sure if the end index shall point to the first character after the occurrence or at the last. In the latter case write `result = (occurrence - str) + strlen(searchTerm) - 1`. – Stephan Lechner Feb 08 '17 at 20:50
  • i am not sure too. can you speak german? i translated it from german : "schreiben sie ein programm, dass eine Funktion enthält, die einen String in einem zweiten String sucht und den Endindex des ersten Vorkommens zurückgibt." sorry if you are not german but your name sounds like a german name :D – Sedem Feb 08 '17 at 20:53
  • Yes, I speak german :-) I'd vote for the last character, i.e. `result = (occurrence - str) + strlen(searchTerm) - 1`. – Stephan Lechner Feb 08 '17 at 20:58
  • ok i dont understand the part : result = (occurrence - str) + strlen(searchTerm) - 1 ;;; i dont understand how i can subtract two char* datatypes. ....... for example those two strings : char *string1 = "12345678"; char *string2 = "123"; int result = (string1 - string2); result equals -9 and i dont understand why. – Sedem Feb 09 '17 at 11:08
  • oh ok i found it out forget my last question :) – Sedem Feb 09 '17 at 12:17