String methods

This is a discussion on String methods within the C Programming forums, part of the General Programming Boards category; Hello, I have to take an array of characters and take out all non alpahbetic characters. I use strncat, but ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    String methods

    Hello,

    I have to take an array of characters and take out all non alpahbetic characters. I use strncat, but it only assigns the first set of alpha characters in the array, to a temp array.
    ex rt%^ty becomes rt, and it skips the rt.

    Is there some way to make a loop and somehow by pass the middle %^ to get to the ty and put them in the temp array also?

    Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <ctype.h>
    .
    .
    .
    for ( i = 0, j = 0; string[i] != '\0'; i++ ) {
      if ( isalpha ( string[i] )
        newString[j++] = string[i];
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    thanks

    if my function had pointers to chars

    void func(char *s1, char *s2)
    where s1 is filled with all the characers inlcuding the ^&&&, would i be able to ouput all the alpha character to s2?

    im new to C, and not very good with pointers
    thanks

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Yes, provided s2 is an array that you passed to the function. Otherwise you would have to allocate enough memory to hold all of the characters that you intend to transfer to s2. Also try to protect yourself from yourself by declaring all variables that are not to be changed as const, so s1 would be const while s2 would not be.
    void function ( const char *src, char *dst );

    -Prelude
    My best code is written with the delete key.

  5. #5
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Thanks
    ive been trying to write some code..
    using the fucntion as before, and having an array filled with all the alpha charaacters, i used strspn ans strncat, but strspn only counts the inital segemnt of the alpha characters in the array
    ex. ffhhh%^^ghjf, since the ffhhh are in alpha in puts them in the new array, and leaves the ghjf.
    Here is my problem, how would i skip all occurence of non-alpha characters? How could i compare the char *In with the alpha char characters?, but still using the strspn and strncat functions?

    I probably should have asked the question this way ftom the beginning, sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 04:41 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

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