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

Recommended Answers

All 8 Replies

Never use gets, use fgets instead. From my help file

BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

Also, can't read your code. Please format it so we can. If we can't understand it we can't help.

A few format pointers, don't comment standard C functions. We know what they are and how they work or we can readily find information on them.

Here's how I would start this code

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

#define CHAR_SET 3

int main()
{
	int i = 0, j = 0;
	char string1[40], temp[CHAR_SET + 1];
	temp[CHAR_SET] = '\0';

	printf("Enter in a string: ");

	fgets(string1, 40, stdin);

	for(i = 0; i < strlen(string1) - CHAR_SET; i++)
	{
		for(j = 0; j <  CHAR_SET; j++)
		{
			temp[j] = string1[i + j];
		}
		

		fprintf(stdout, "char set->%s\n", temp);

		/*now compare the set with the remainder of the string*/

	}
	return 0;
}

My apologies this should be better

#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
	fgets(string1,40,stdin);
	//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;
}

Also is fgets just a security issue because the program still doesnt respond

My apologies this should be better

Somewhat. Please actually READ the link. There are still problems with your formatting. Yours, too, gerard4143...

Also is fgets just a security issue because the program still doesnt respond

What does this mean?

The problem you have is you didn't think through the problem yet. I'll bet you just started coding...

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

If string1 is ABCDABCDE and i=8 what's going to happen in the j loop?

//pointer to where the occurance is
    ptr1 = strstr(string1,temp);

Which occurrence is this statement going to find?

Im sorry, your right ive recently started coding in c, but i have solved the problem now thanks to yours and gerard4143 help. I appreciate it thanks.

Somewhat. Please actually READ the link. There are still problems with your formatting. Yours, too, gerard4143...

Really? Is it this line?

for(i = 0; i < strlen(string1) - CHAR_SET; i++)

should be

for(i = 0; i < (strlen(string1) - CHAR_SET); i++)

No. It's this:

Indent blocks of code 3 to 4 spaces. 2 is not quite enough, 5 is getting too much. Try not to use tabs because:

1. different programs may display tabs differently
2. most forums handle tabs badly

Usually tabs are 8 spaces, which means a 4th-block indent starts 32 spaces from the left border, which in itself makes code hard to read. Too much of a good thing...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.