Strings

This is a discussion on Strings within the C Programming forums, part of the General Programming Boards category; Hi, I have an array "iWord", and i'm trying to build a function that takes the punctuation and spaces out ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    36

    Strings

    Hi,
    I have an array "iWord", and i'm trying to build a function that takes the punctuation and spaces out of it and puts it in a new array containing only letters, "iAlphaWord". Here is what I have so far:

    Code:
    *Function to make elements only letters*/
    void allAlpha (char sWord[], sAlphaWord[])
    {
    	int i, iLength;
    
    	iLength = strlen(sWord);
    
    	for (i=0; i < iLength; i++)
    	{
    		if (isalpha(i))
    		{
    			sAlphaWord[i] = sWord[i]
    		}
    		else
    		{
    			?????
    		}
    	}
    
    	return(0)
    }
    I can determine whether it's a letter or not and put that in the corresponding place in the new array, but as you can see, i'm lost on how to get rid of the elements containing spaces/punctuation.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    Check out the memmove() function. You're basically going to have to copy every element after the index point to the element right before it. memmove() can help you do this quickly and easily.

    Another thing to consider is using a separate index variable for the two strings. Let's say the first character of sWord is a punctuation mark. It wouldn't copy the letter to sAlphaWord. Then the next character of sWord is a letter so it sets sAlphaWord[i] (and remember, i is now 1 instead of 0) to whatever's in sWord[i]. But what's sitting in sAlphaWord[0]? It has some garbage value in it. The solution would be to use a separate variable to keep track of the sAlphaWord index and only increment it when you actually copy something into there.
    Last edited by itsme86; 11-22-2005 at 02:24 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    do you have to change iWord array? If not, then you don't have to do anything with the punctuation characters -- just remove the else statement from that loop, and when done you will need to null-terminate the string in sAlphaWord. Also, the function declaration isn't quite correct
    Code:
    void allAlpha (char sWord[],char sAlphaWord[])

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    36
    So if I erase the "else" statement, what will happen if the "if" statement can't be satisfied (a non-alphabetical character)?

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    It will continue without satisfying the condition, pretty much do nothing, then repeat the loop.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    36
    Say, for example, that I had the following in my array:

    Hi! I'm Rob

    This function would put the "H" and the "i" as the 0 and 1 elements in my new array. But since the next one is a "!", it would skip that, and skip the next " "(blank space), and place the 4th element "I" in position 4 in my new array. What would be in positions 2 and 3?

    I'm pretty sure i'd want the "I" to come in the position immediately after the first "i".
    Last edited by w274v; 11-22-2005 at 07:21 PM.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Right, well that makes sense. Then you could change your for loop to a while loop.

    Perhaps something like this:
    Code:
    i = 0;
    x = 0;
    while (i < iLength) {
        if (sWord[x]) {
    	sAlphaWord[i] = sWord[x]
    	i++;
        }
        x++;
    }
    Ofcourse, you could keep a for loop, but then I don't like to use for loops for anything but purely count controlled loops, this is somewhat event controlled. Just fix it like this:

    Code:
    int x = 0;
    for (i = 0; i < iLength; i++) {
        if (isalpha(sWord[i])) {
           sAlphaWord[x] = sWord[i]
           x++;
        }
    }
    Last edited by SlyMaelstrom; 11-22-2005 at 08:24 PM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21