Thread: assistance with string tokenizing

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    3

    assistance with string tokenizing

    Hi there, like many other first posters on the forum, I'm a programming n00b,
    For practice, I'm trying to write a function that copies chars from a source string into a buffer until either: there's a space, the string ends or the buffer is full. I have yet to code for the last condition (read: unsure how to approach it)
    This is my code so far:


    Code:
    #include<stdio.h>
    
    int tkenCpy (char*dest, const char *src, int destSize) {
    
    int main () {
          char buff[5];
          int n = tkenCpy(buff, "This is a string", 5);
          printf("%d'%s'\n", n,  buff);
          return 0;
    }
    
    int tkenCpy (char*dest, const char *src, int destSize) {
          int i = 0;
          int chcpy = destSize -1;
    
          for(i=0; i < chcpy;  i+=1) {
               if(*src != '\0' && *src != 32) {
                  dest[i ]= *(src+i);
                  }
                }
         return *dest;
    }
    As shown in the main function, I'd like my output to be the number of chars printed, then print the copied string. But my output is as follows:

    Code:
    84 'This?~
    The '?~' are two random characters. I'm not sure what the 84 refers to, and it stays the same for any array size... buff[3] etc. Also, the final apostrophe demanded by the printf function is not there.

    Any help would be greatly appreciated
    Last edited by Maria Fernando; 08-08-2012 at 02:45 AM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Your loop is wrong.

    Try to refer to arrays using their index not using *(src + i) pointer notation to make things easier. You are going way too far in the loop because your condition is wrong.
    Also, you are not null terminating dest with '\0'. You will have to do that manually after you fill it up, so make sure you always have 1 character reserved for that.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    3
    Thank-you for your help cladiu!

    Quote Originally Posted by claudiu View Post
    You are going way too far in the loop because your condition is wrong.
    I have altered my code by refering to the index of my arrays, and also null terminated my return string. I have also tried to correct the loop condition.

    I understand that 84 refers to the decimal number for the char 'T'.
    The string output is now correct, but i'm unsure how to print out the number of chars in the string using just the 1 function?

    Here is my altered code:
    Code:
    #include <stdio.h>
    
    int tokenCopy(char *dest, const char *src, int destSize);
    
    int main () {
        char buff[5];
        int n = tokenCopy(buff, "This is a string", 5);
        printf("%d '%s'\n", n, buff);
        return 0;
    }
    
    int tokenCopy(char *dest, const char *src, int destSize) {
        int i = 0;
            
        for (i=0; i < destSize-1; i+=1) {
            if(src[i] =='\0' || src[i] == 32) {
                break;
            }
            else {
                dest[i]=src[i];
            }            
        }
        dest[i]='\0';
        dest[destSize]='\0';
        return *dest;
    }
    Last edited by Maria Fernando; 08-08-2012 at 04:24 AM. Reason: Progress in coding

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Maybe instead of returning the char to which *dest is pointing, you might return the number of characters you've processed?

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    3
    Ah, I see my error now. Thanks again.
    I couldn't get my head around the fact that the function was able to return an integer, as well as the copied string. Which was the whole idea of having a pointer I suppose.

    My final code follows for those interested:

    Code:
    #include <stdio.h>
    
    int tkenCpy(char *dest, const char *src, int destSize);
    
    int main () {
        char buff[7];
        int n = tkenCpy(buff, "This is a string", 7);
        printf("%d '%s'\n", n, buff);
        return 0;
    }
    
    int tkenCpy(char *dest, const char *src, int destSize) {
        int i = 0;
            
        for (i=0; i < destSize-1; i+=1) {
            if(src[i] == 32 || src[i] == '\0') {
                break;
            }
            else {
                dest[i]=src[i];
            }            
        }
        dest[i]='\0';
        return i;
    }
    Last edited by Maria Fernando; 08-08-2012 at 05:09 AM. Reason: Including code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tokenizing a string
    By sigur47 in forum C Programming
    Replies: 3
    Last Post: 05-07-2012, 07:16 PM
  2. Tokenizing a string
    By BdON003 in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2009, 10:45 PM
  3. Tokenizing a string
    By chinesepirate in forum C++ Programming
    Replies: 3
    Last Post: 10-17-2008, 11:32 AM
  4. Tokenizing a C++ string
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2004, 06:31 AM
  5. String Tokenizing
    By irncty99 in forum C++ Programming
    Replies: 21
    Last Post: 05-08-2003, 07:47 AM

Tags for this Thread