Hi there i am trying to complete a string handling program. That searches a string and finds each 3 character sequences in that string.
ex: Please enter a word up to 10 characters long: Middiddippi
Pairs of 3 character sequences: idd ddi
Now the issue i am having is for every sequence i want to store it into a 2D array and then upon recursion i want a new sequence to be stored in the next row of the 2D array. The problem is the 2D array is only holding the first string sequence and even though the rows are incrementing it is not copying the sequence into the new row. Either I am really tired or i am missing something. Anyhow here is my code.
Code:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define MAX 30
void checkSeq(char *,char [][4],int, int);
int main()
{
char word[MAX];
char dup[MAX][4];
do{
printf("Enter a string that is atleast 10 characters long\n");
gets(word);
}while(strlen(word)<10);
checkSeq(word,dup,0,strlen(word));
return 0;
}
void checkSeq(char word[MAX],char dup[MAX][4], int x, int len)
{
char seq[4];
strncpy(seq,&word[x],3);//temp = first 3 characters of word
seq[3]='\0';//makes sure the array gets capped
strcpy(dup[x],seq);
int i = 0;
int j, k;
int counter = 0;
for(j = 0;j<(len-2);j++)
{
if(strncmp(&seq[i],&word[j],1)==0)
if(strncmp(&seq[i+1],&word[j+1],1)==0)
if(strncmp(&seq[i+2],&word[j+2],1)==0)
{
counter++;
}
}
for(k = 0; k<x;k++)
{
if(strncmp(dup[k],seq,3)==0)
{
counter = 0;
}
}
if(counter!=0)
{
printf("A 3 char sequence %s was found %d times\n", seq,counter);
}
if(x == (len - 3))
{
exit(0);
}
checkSeq(word,dup,x+1,len);
}