Thread: Can someone solve this problem for me?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    15

    Can someone solve this problem for me?

    Im trying to break a string into arrays of pairs... But the thing is an error will occur when this code is executed.

    Code:
    void letterPair(char str[])
    {
      int i;
      int j;
      int total = 0;
      char temp[50][50];
      char abc[1];
    
      for(i = 0; str[i] != '\0'; i++)
      {
    	if(!isspace(str[i]) )
    	{
                 if(!isspace(str[i+1]) )
    	    {
    		if( str[i+1] != '\0' )
    		{
    		  abc[0] = str[i];
    		  abc[1] = str[i+1];
    		  strcat(temp[total],abc);
    	           total++;
    		}
    	    }
    	}
       }
    }
    More information:
    Lets say my str = "THIS IS A STRING"
    I want to break it up to "TH" "HI" "IS" "IS" "ST" "TR" "RI" "IN" "NG"
    And im leaving out the pairs which have spaces. Eg " I" "S " " A" "A " and " S". (Pls note the spaces)

    Or are there any other ways to join two characters together??Help is very much appreciated!! Thanks a lot!!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char abc[1];
    This only holds 1 character. In order to hold the 2 characters you require, and the NULL terminator which is needed when using any of the string functions, this array must hold at least 3 characters. Your use of strcat is also inappropriate here, the strcpy function would be a better choice. A simpler set of loop and conditional statements should be:

    Code:
    for( i = 0; str[i]; ++i )
        if( !isspace(str[i]) && !isspace(str[i+1]) && str[i+1] )
        {
            // Copy two characters starting at str[i] into your array
        }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    I see!!! Thanks a lot for helping me out!! Really appreciate it!! Had no one to turn to with my C programming...Again, thank you very much!! ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Solve This Problem Within 3 Hr Urgent Need
    By annum in forum C Programming
    Replies: 12
    Last Post: 10-04-2009, 09:56 AM
  2. Problem I can't seem to solve
    By psykik in forum C Programming
    Replies: 15
    Last Post: 05-19-2005, 12:18 PM
  3. problem solve
    By coolnarugodas in forum C Programming
    Replies: 7
    Last Post: 04-26-2005, 12:31 PM
  4. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  5. problem cant solve please help.
    By sarah in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:32 PM