The question is as follows:
Write a program to read in a word that is up to 10 characters long. See if any 3 consecutive characters in the word are found somewhere else within the word. If they are, echo those characters to the screen. For example, if the word is "acmacadacma", then the 3 character sequences that are found more than once are "acm" and "cma". If the input word was "rsksksk" then the 3 character sequence pairs that are found would be "sks" and "ksk". For each pair of 3 character sequences, the sequence should only be displayed once by your program. Following are examples of the program execution:
Please enter a word up to 10 characters long: Middiddippi
Pairs of 3 character sequences: idd ddi
Please enter a word up to 10 characters long: feefeefg
Pairs of 3 character sequences: fee eef
As for my code I have this:
#include <stdio.h>
#include <string.h>
//main function
int main(){
//variable definitions
char string1[40], string2[40],temp[40], *ptr1, *ptr2;
//prompts the user to enter a string
printf("Enter in a string: ");
//user enters the string
gets(string1);
//initializes string2
strcpy(string2," ");
//loop to check the whole string the user inputted
for(int i = 0; i < strlen(string1);i++){
//selects the three characters to be compared
for(int j = i; j < i + 3; j++){
temp[j] = string1[j];
}
//pointer to where the occurance is
ptr1 = strstr(string1,temp);
//checks to see if it already checked the users string with the three letters selected
if(strstr(string2,temp)!= NULL)
continue;
//copies the selected letters into string2 so that it doesnt repeat it more than once
strcat(string2,temp);
//prints the combonation of letters if there is some reoccured
if(ptr1++ != '\0'){
printf("The combonation %s occurs more than once in the string", temp);
}
}
return 0;
}
Basically the problem is the program just stops responding after the user inputs a string