Breaking strings without using strtok()

This is a discussion on Breaking strings without using strtok() within the C Programming forums, part of the General Programming Boards category; Hey there~ Im looking for a way to break a string into substrings without the use of strtok()...I wrote my ...

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

    Breaking strings without using strtok()

    Hey there~ Im looking for a way to break a string into substrings without the use of strtok()...I wrote my own function but i think there are some bugs in it but i just cant seem to find out the problem...Hope you all will help me out here...Thanks in advance for the help!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main()
    {
      char string[50];
      char words[60][60];
      int wordNum = 0;
      int charNum;
      int length, i, j;
    
      fgets(string,sizeof(words),stdin);
    
      length = strlen(string);
    
      for(i = 0; i < length; i++)
      {
        charNum = 0;
    
        if(!isspace(string[i]))
        {
          words[wordNum][charNum] = string[i];
          charNum++;
        } 
    
        else
        {
          wordNum++;
        }
      }
    
      printf("\n\n");
    
      for(i = 0; i <= wordNum; i++)
      {
        for(j = 0; words[i][j] != '\0'; j++)
        {
    	if(isalnum(words[i][j]))
            printf("%c",words[i][j]);
        }
    
        printf("\n\n");
      }
    
      return 1;
    }
    Last edited by hykyit; 08-22-2005 at 09:38 AM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main()
    {
      char string[50];
      char words[60][60];
      int wordNum = 0;
      int charNum = 0;
      int length, i, j;
    
      fgets(string,sizeof(string),stdin);
    
      length = strlen(string);
    
      for(i = 0; i < length; i++)
      {
    
        if(!isspace(string[i]))
        {
          words[wordNum][charNum] = string[i];
          charNum++;
        }
    
        else
        {
          words[wordNum][charNum] = '\0';
          wordNum++;
          charNum = 0;
        }
      }
    
      printf("\n\n");
    
      for(i = 0; i < wordNum; i++)
      {
        for(j = 0; words[i][j] != '\0'; j++)
        {
    	if(isalnum(words[i][j]))
            printf("%c",words[i][j]);
        }
    
        printf("\n\n");
      }
    
      return 0;
    }
    Last edited by Dave_Sinkula; 08-22-2005 at 10:04 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    -.- And it took me so long to find out the problem...Thanks a lot for the help! It works just fine now!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reversing strings using strtok()
    By Sure in forum C Programming
    Replies: 5
    Last Post: 06-27-2005, 06:33 PM
  5. Replies: 1
    Last Post: 09-05-2004, 07:42 PM

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