Yíu
Yíu

Reputation: 383

two dimensional array of strings

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?

Upvotes: 0

Views: 875

Answers (2)

Scott
Scott

Reputation: 63

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);
}

Upvotes: 0

user3386109
user3386109

Reputation: 34829

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.

Upvotes: 1

Related Questions