Creating an array with a piece of a string problem

This is a discussion on Creating an array with a piece of a string problem within the C Programming forums, part of the General Programming Boards category; I am really struggling with this problem! I have included my code although I know there are errors in it. ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    34

    Creating an array with a piece of a string problem

    I am really struggling with this problem! I have included my code although I know there are errors in it. Any help and guidance is greatly appreciated.

    Problem as follows:

    Write a function named GetSubString that copies a portion of a string. Syntax is as follows: char * GetSubString(const char source[], int start, int count, char result[]);

    Where source is the character string containing the substring, start is the position of the first character in source to be copied, count is the number of char to copy and result is an array into which substring is copied. GetSubString returns a pointer to the copied substring but does no printing itself. For example call printf(GetSubString("character", 4, 3, resultArray)); would copy substring "act" from "character" into resultArray, return resultArray, then print "act".

    Following are additional requirments:

    1. GetSubString must append a '\0' to every substring it creates
    2. If start is beyond end of source string, create a substring containing only a '\0'.
    3. If count extends beyond end of source, copy characters only up to the end of source string, even though more have been requested
    4. No calling of any library functions
    5. You may declare only one variable other than forma parameters, if you do it must be a pointer.
    6. You must use the following Main, unaltered:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define RESULT_MAX 19
    
    int main(void)
    {
       static char source[] = "one two three";
       char result[RESULT_MAX];
    
       puts(GetSubString("This is really fun", 2, 800, result));
       puts(GetSubString("This is really fun", 261, 9, result));
       puts(GetSubString("This is really fun", 0, 12, result));
       puts(GetSubString("source", 5, 87, result));
       puts(GetSubString("source", 18, 7, result));
       puts(GetSubString("source", 6, 5, result));
       puts(GetSubString("source", 0, 3, result));
    
       return(EXIT_SUCCESS);
    }
    Here is the beginnings of my code, it doesn't run properly and I know I need to start by figuring out if the start point is before the end of the string ( I have not worked that part out yet)...

    Code:
    char *GetSubString(const char source[], int start, int count, char result[])
    {
    
      
       {
          for(result[start] = source[start]; start <= (start + count - 1) && start != '\0'; start++)
             result[start] = source[start];
          result[start + 1] = "\0";
       }
    
       return( *result[0] );
          
    }
    Thanks for any and all help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to start copying to result[0], not result[start]. Start is your index and should not be compared to '\0'. Perhaps you meant to compare some member of the source array to '\0' instead. start <= (start+count-1) will always be true, unless count is 0. You probably want to assign the character '\0' at the end of the day, not the string "\0".

  3. #3
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Think how is the end of a string marked? Then count how many characters there is in string before the endmark. A simple loop for loop will do that for you

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by steals10304 View Post
    I know I need to start by figuring out if the start point is before the end of the string ( I have not worked that part out yet)...
    Use strlen(). Also this is better written the (more normative) second way:

    Code:
    char *GetSubString(const char source[], int start, int count, char result[])
    char *GetSubString(const char *source, int start, int count, char *result)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    He had the "no library functions" addendum.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Maz View Post
    He had the "no library functions" addendum.
    Oh yeah. Well, it doesn't say "no other functions":
    Code:
    int stringLength (char *ptr) {
    	int i=0;
    	while (ptr[i] !='\0') i++;
    	return i;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    34

    Worked on it some more and still need help...

    Still working on same problem, made a couple adjustments so I could take this one bite at a time, I am using whatever variables I need and changed GetSubString into a void function that does printing itself. I've placed a printf function toward the bottom that I hoped would print what is in my result array, but it prints nothing. I am also getting a compiler warning: warning C4047: '=' : 'char' differs in levels of indirection from 'char [2]'

    My function call from main is: (GetSubString("character", 4, 3, result));

    Thanks for any help past and present...

    Code:
    void GetSubString(const char *source, int start, int count, char *result)
    {
       int i = 0;
       int a = 0;
       
       while ( *source )
       {
          ++source, i++;
       }
    
       if( start > i)
          result[0] = "\0";
       else
       {
          while (result[a] < result[count] && (*result))
          {
             result[a] = source[start];
             printf("%c", result[a]);                                // debug check
             a++, start++;
          }
         
          
          //for(result[a] = source[start]; a <= count && ( *source ); start++, a++)
            // printf("%c", result[a]);
    
       }

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    >>result[0] = "\0";
    result[0] is one element inside a string - a char. But double quotes signifies a string. That is, two or more chars. How can the compiler fit that into the space of one char?
    Single chars use single quotes ('').
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> while (result[a] < result[count] && (*result))

    You need to compare the indexes, not the characters at those indexes.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    34

    Trying to wrap this one up...

    OK, I have a working program which meets the assignment criteria....almost.

    Here is the original requirements:

    Problem as follows:

    Write a function named GetSubString that copies a portion of a string. Syntax is as follows: char * GetSubString(const char source[], int start, int count, char result[]);

    Where source is the character string containing the substring, start is the position of the first character in source to be copied, count is the number of char to copy and result is an array into which substring is copied. GetSubString returns a pointer to the copied substring but does no printing itself. For example call printf(GetSubString("character", 4, 3, resultArray)); would copy substring "act" from "character" into resultArray, return resultArray, then print "act".

    Following are additional requirments:

    1. GetSubString must append a '\0' to every substring it creates
    2. If start is beyond end of source string, create a substring containing only a '\0'.
    3. If count extends beyond end of source, copy characters only up to the end of source string, even though more have been requested
    4. No calling of any library functions
    5. You may declare only one variable other than formal parameters, if you do it must be a pointer.
    6. You must use the following Main, UNALTERED:

    I have a couple problems with my code that I can see:

    1. By incrementing variable start on in my while loop, the next time the function is called it does not operate properly, namely my if statement evaluates start which was adjusted in the last function call.

    2. I am only allowed to use one variable in the whole program, and that must be a pointer. So I need to work out int a and int i out of my code.


    Can anyone show me how to remedy these two issues and get this program running properly? Thanks for all your help. This forum is great. Code is as follows:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define RESULT_MAX 19
    
    char *GetSubString(const char *source, int start, int count, char *result)
    {
       int i = 0;
       int a = 0;
       char *p = source;
     
       
       while ( *p )
       {
          ++p, i++;
       }
    
       if( start > i)
       {
       result[0] = '\0';
       printf("You lose");              // debug check
       }
       else
       {
          while ( a < count && ( *result ) && a < RESULT_MAX )
          {
             result[a] = source[start];
             a++, start++;
          }
          result[a] = '\0';
       } 
    
       return(result);
    }
    
    
    int main(void)
    {
       static char source[] = "one two three";
       char result[RESULT_MAX];
    
       //printf(GetSubString("character", 4, 4, result));
    
       puts(GetSubString("This is really fun", 2, 800, result));
       puts(GetSubString("This is really fun", 261, 9, result));
       puts(GetSubString("This is really fun", 0, 12, result));
       puts(GetSubString("source", 5, 87, result));
       puts(GetSubString("source", 18, 7, result));
       puts(GetSubString("source", 6, 5, result));
       puts(GetSubString("source", 0, 3, result));
    
       return(EXIT_SUCCESS);
    }

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    It can all be done by simple pointer arithmetic if you take out some time to pen down the algorithm of extracting a substring.

  12. #12
    Registered User
    Join Date
    Aug 2010
    Posts
    1

    steals10304...

    Is there any way you can post the final result?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another String Problem
    By w2look in forum C Programming
    Replies: 6
    Last Post: 02-25-2009, 11:00 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  4. problem with string and array
    By Methje in forum C++ Programming
    Replies: 9
    Last Post: 01-26-2006, 12:15 PM
  5. string array input problem
    By DoItAllMom115 in forum C++ Programming
    Replies: 6
    Last Post: 03-25-2003, 09:38 AM

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