0

I try to make a function that gets a string delivered like "Hello". The function's return value should be an array of strings that would look like

H
He
Hel
Hell
Hello

I really don't know how to do that. My current code is super messy because I made a lot of adjustions and everyone made it worse.

char stutterString(char string[])
{
  rowLength = strlen(string);
  char help_string[][rowLength]; // I wanted to make the onedimensiol string i get into a two dimensial one so I can then compare my strings in a forloop 

    strcpy(help_strig, string); // I'm not sure if that's how you copy a 1d string into a 2d string, I guess not.

My loops look like

for(; count1 <= COLUMN; count1++)
  {
    for(count2 = 0; count2 <= NumberRow; count2++)
    {
      new_string[count1][ROW] = help_string[0][count2];

      ROW++
    }
    NumberRow++;
  }

// NumberRow indicates the limit on the letters that should be copied. like in the beginning it's 1 for H, then 2 for the He and so on..
//count1 is for the column number we r currently and count2 for the row

Any ideas how I could achieve that easier / where to improve my code?

2
  • char help_string[][rowLength]; is illegal, as is strcpy(help_strig, string);. Also it's not possible to return an array of strings in C. I'd suggest reading some tutorials or books about arrays, before continuing Commented Jan 22, 2015 at 0:34
  • Thanks for the suggestion, yes, I'll still have to read A LOT. Commented Jan 22, 2015 at 0:36

2 Answers 2

1

You need to declare a two dimensional array, e.g.

char array[50][200];

Then to copy a 1D string into the array you would do this

strcpy( array[0], "Hello" );

To copy a portion of a string from a 2D array to another 2D array, use the strncpy function, e.g.

length = 3;
strncpy( new_string[index], help_string[10], length );
new_string[index][length] = '\0';

That would copy the first 3 characters of help_string[10] into the new_string array. Note that strncpy may not terminate the string with a NUL character (depending on the length of the source string) so that needs to be done after the copy.

Sign up to request clarification or add additional context in comments.

1 Comment

Be wary of strncpy(); it does not always null-terminate the string it copies to. If the array is already zeroed, it won't matter. If it was used for something else before hand, it does matter.
0

This should do it for you. Using dynamic allocation can reduce the program's memory footprint at runtime as well as file size of the executable.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char **
stutterString(char *str)
{
  int i, sz, len=strlen(str);
  char **rtn; /* a 2-dimensional array */
  char *s;

  /* allocate the number of rows plus an extra one to indicate the end of list.
     calloc() will set all "rows" to NULL.
  */
  rtn = (char **)calloc(len+1, sizeof(char **));
  for(i=0; i<len; ++i) {
    /* allocate the string -
      '2' is 0 offset plus i plus 1 for \0
    */
    s = (char *)malloc(sizeof(char *) * (i+2));
    memcpy(s, str, i+1);
    s[i+1] = '\0';
    rtn[i] = s;
  }

  return(rtn);
}

int
main()
{
  int i;
  char **stutter;   /* think of it as a 2-dimensional array */

  stutter = stutterString("hello");

  /* Print and free the string. It's important to free when
     you are done with the memory! */
  for(i=0; stutter[i]!=NULL; ++i) {
    puts(stutter[i]);
    free(stutter[i]);
  }
  free(stutter); /* free the array itself */

  return(0);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.