Thread: 2D Array problems

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    36

    2D Array problems

    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);
    }
    Last edited by Annihilate; 02-09-2011 at 02:39 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why are you checking before you've finished splitting?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    36
    I am not sure what you mean by that question.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you type in "hello world", you first extract "hel" and then go looking for all duplicates.

    How about starting with a function which extracts
    hel
    ell
    llo

    and so on, and then have another function which begins with
    for(j = 0;j<(len-2);j++)
    to check for duplicated sub strings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    36
    Hmm i never really thought about the whole duplicate substrings until after i wrote that functions. I added in the 2D array after. I just can't see where my function is going wrong in terms of finding a duplicate seq. If i was to create another function and extract all the sub strings from the original word and store it into a 2D array and then pass it to the above function would that not be somewhat similar as to what i am doing now. Just i am keeping track of the current substrings and thats all until i find a duplicate substring. Once i find one of those then i do not print the sequence was found and how many times i skip it.

    If anyone would like i can comment my code.

    Nevermind my code was working the way i wanted to i just had to change one of my if() statement conditions and now it works just as expected. Yes i was tired. Thanks anyhow for the help Salem.

    CLOSED
    Last edited by Annihilate; 02-09-2011 at 01:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Comparing a 2d Array (newbie)
    By Cockney in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2001, 12:15 PM