0

I tried to convert a character array to string array. Means single string to multiple sentences.

char str[1000]="This is first. Second sentence is possible. I'm third. The fourth one is here!";

The above string should converted as array of sentences.

char **sentences={
 "This is first.",
 "Second sentence is possible.",
 "I'm third.",
 "The fourth one is here!"
 };

I tried to below code.

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

int
char_comp (char a, char b)
{
  return a == b;
}

int
is_special (char str)
{
  return char_comp (str, '?') || char_comp (str, '.') || char_comp (str, '!');
}

void
str_to_arr (char a[], char **b)
{
  int i = 0, j = 0, length = strlen (a), sentance_length =6, k = 0; //here added sentance_length 8 as static
  for (i = 0; i < sentance_length; i++)
    {
      b[i] = (char *) malloc (length * sizeof (char));
      int n = 0;
      for (j = k; j < length; j++)
    {
      b[i][n] = a[j];

      n++;
      k++;
      if (is_special (a[j]))    //if !,?,. found then move to next sentence.
        {
          b[i][n] = '\0';
          break;
        }

    }
    }
}

int
main ()
{
  char finalstr[1000]; 
    fgets(finalstr, 1000, stdin);
  char **arr = (char **) malloc ((6) * sizeof (char));
  str_to_arr (finalstr, arr);

  for (int i = 0; i < strlen (finalstr); i++)
    {
      printf ("%i--->%s\n",i, arr[i]);
    }
  return 0;
}

When I run the code.

input:

Die schnelle Fourier-Transformation ist ein Algorithmus zur effizienten Berechnung der diskreten Fourier-Transformation.Mit ihr kann ein zeitdiskretes Signal in se ine Frequenzanteile zerlegt und dadurch analysiert werden.  Analog gibt es fuer die diskrete inverse Fourier-Transformation die inverse schnelle Fourier-Transformati on (IFFT). Es kommen bei der IFFT die gleichen Algorithmen, aber mit konjugierten Koeffizienten zur Anwendung. Die FFT gehoert zu den "Divide-and-conquer"-Verfahre n, sodass zuvor berechnete Zwischenergebnisse wiederverwendet werden koennen. Dadu rch koennen arithmetische Rechenoperationen eingespart werden.

output:

0--->��V
1--->Mit ihr kann ein zeitdiskretes Signal in se ine Frequenzanteile zerlegt und dadurch analysiert werden.
2--->  Analog gibt es fuer die diskrete inverse Fourier-Transformation die inverse schnelle Fourier-Transformati on (IFFT).
3---> Es kommen bei der IFFT die gleichen Algorithmen, aber mit konjugierten Koeffizienten zur Anwendung.
4---> Die FFT gehoert zu den "Divide-and-conquer"-Verfahre n, sodass zuvor berechnete Zwischenergebnisse wiederverwendet werden koennen.
5---> Dadu rch koennen arithmetische Rechenoperationen eingespart werden.

input:

This is first. Second sentence is possible. I'm third. The fourth one is here!

output:

0--->This is first.
1---> Second sentence is possible.
2---> I'm third.
3---> The fourth one is here!

When sentences going more than 5. The first sentence becomes ��V like this. What I'm doing wrong here.

Note: I'm not using string.h here. Except strlen.

8
  • "I'm not using string.h here": you should. Commented Dec 15, 2021 at 9:38
  • int char_comp (char a, char b) { return a == b; } why do this? Commented Dec 15, 2021 at 9:39
  • char **sentences Nope. Arrays are not pointers. Pointers are not arrays. Commented Dec 15, 2021 at 9:43
  • 1
    I am not sure if this is only problem, but it definitely is a problem: Size calculation in malloc ((6) * sizeof (char)) is using invalid type. Commented Dec 15, 2021 at 9:43
  • 1
    @SarathKumar if you are not allowed to use functions from string.h you need to make that clear in the question. "Note: I'm not using string.h" just means exactly that, no more, no less. Commented Dec 15, 2021 at 9:50

1 Answer 1

1
char **arr = (char **) malloc ((6) * sizeof (char));

If you are trying to allocate space for 6 sentences then that is wrong. You need to allocate space for the type of elements of arr which are char*, not char:

char **arr = malloc (sizeof *arr * 6);

This allocates only the space for 6 pointers to strings. You now need to allocate the space for each sentence. Something like:

arr[i] = malloc(sizeof *arr * (size_of_sentece + 1))

Also you want to reallocate for each new sentence to not have this upper limit of 6 sentences.

There might be other errors in the code, I haven't checked.

And finally use the standard library and use strtok with ".!?" as delimiters. I understand you can't to this for grades on this task, but nevertheless it's a good thing learn to use the standard library.

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

2 Comments

Where I can get more info about malloc,realloc for array of string. I tried in the internet couldn't find proper one.

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.