Thread: Using string functions

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

    Using string functions

    Hello
    i was wondering if someone can help me code a little bit, I understand how some of it works, and ill show what i have, I just dont know how to put this work in a loop. Thanks
    If in starts with "abc32fe5v" it should end up with only alpha characters so Out should = "abcfev"

    alpha - character array stores the eniter alphabet

    1. strspn("abc32fe5v",Alpha) gives 3
    2. strcpy the first 3 chars of "abc12de6x" to Out
    3. strcspn("32fe5v",Alpha) gives 2, so skip over the net 2 chars of "21fe5v"

    1. strspn("fe5v",Alpha) gives 2
    2. strcpy the first 2 chars of "fe5v" to Out
    3. strcspn("5v",Alpha) gives 1 so skip over 1 chars of "5v"

    1. strspn("v",Alpha) gives 1
    2. strcpy the first 1 char of "v" to Out
    3. strcspn("",Alpha) gives 0 so skip over 0 chars of ""
    loop ends because In is finished

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Man thats really complicating it, do you have to use string functions?
    You would be better processing it a character at a time.
    A couple of lines of code in a while loop and your there.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
    	char in[] = "abc32fe5v";
    	char out[7];
    	char *p, *q;
    
    	p = in;
    	q = out;
    
    	// all processing done here
            while(*p)
    	{
    		if(isalpha(*p)) // isalpha tests for a letter
    	          *q++ = *p; // store letter in out
    		p++;
    	}
    	*q = '\0'; // ensure there is a null on the end of new string
    
    	printf("%s", out);
    
    	return 0;
    }
    You could use subscripts for the arrays if you are unfamiliar with pointers.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    thanks, I know it would be eaiser however, it requires the use of these functions

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one way.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    //alpha - character array stores the eniter alphabet
    char *Alpha = "abcdefghijklmnopqrstuvwxyz";
    int main(void)
    {
    char *original = "abc32fe5v";
    char Out[80] = "";
    size_t alpha_count;
    char *p;
    
    p = original;
    while (*p != '\0')
    {
       alpha_count = strspn(p,Alpha);
       printf("alpha_count:%d\n",alpha_count);
       strncat(Out,p,alpha_count);
       p += alpha_count;
       printf("p:%s\n",p);
       p += strcspn(p,Alpha);
       printf("p:%s\n",p);
       getchar();
    }
    printf("Out:%s\n",Out);
    return 0;
    }

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

    Thumbs up

    thank you very much for your help, it works like a charm.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Badly designed n string functions?
    By anonytmouse in forum C Programming
    Replies: 3
    Last Post: 11-01-2003, 06:16 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM