Thread: assistance with string tokenizing

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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