Thread: problem with 2D character array

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    3

    problem with 2D character array

    Hello everyone

    I was requested to write a program which perceives
    a string from the user and a small letter .the function should dynamically allocates new array of characters which contains all words in the string starting with the chosen letter(there is not different between small or capital letters) the function should return by pointer the size of array and returns the place of array in the memory.
    for example the user typed the string "Hello World" and the chosen letter is w
    so the array contains the word 'world' and its size is 1 hence there is only one row in the array
    I tried to programming that but fortunately my code did not work.
    I will be very thankful if someone can help

    thanks,

    so that is my code

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<string.h>
    #define SIZE 20                 //maximal size of string str
    char **split(char letter, char *str, int *size_of_array);
    void main()
    {
      char str, letter, **arr1;
      int size_of_array;
      printf("Please enter a string of worlds as you wish\n");
      fgets(str, SIZE, stdin);
      printf("%c", str[1]);
      printf("Please enter a small letter as you wish\n");
      scanf("%c", &letter);
      _flushall();
      arr1 = split(letter, str, &size_of_array);
      printf("The size of arr is %d", size_of_array);
      for (int i = 0; i < size_of_array; i++) {
        for (int j = 0; arr1[i][j] != '\0'; j++)
          printf("%c", arr1[i][j]);
        printf("\n");
      }
    }
    
    char **split(char letter, char *str, int *size_of_array)
    {
      char **arr;
      int number_of_words = 0, i = 0, number_of_chars = 0, j = 0, k = 0, s = 0;
    
      while (str[i] != '\0') {
        if ((str[i] == ' '
             && (str[i + 1] == letter || str[i + 1] == tolower(letter)))
            || str[0] == letter) {
          number_of_words++;
        }
        i++;
      }
      arr = (char **) malloc(sizeof(char *) * number_of_words);
      i = 0;
      while (str[i] != '\0') {
        if (((str[i] == letter || str[i] == tolower(letter)) && str[i - 1] == ' ')
            || str[0] == letter) {
          while (str[i] != ' ') {
            arr[j] = (char **) malloc(sizeof(char *) * (number_of_chars++));
            i++;
          }
          for (k = 0; k < number_of_chars; k++) {
            arr[j][k] = str[s];
            s++;
    
          }
        }
        arr = (char **) realloc(arr[j], sizeof(char *) * number_of_chars);
        i++;
        j++;
      }
      *size_of_array = number_of_words;
      return arr;
    }
    Last edited by Salem; 06-02-2019 at 08:46 AM. Reason: Removed crayola

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    why is str a single character when your trying to read an array into it?

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    @someone111888

    Code:
    _flushall();
    This is a non Standard C, Mickey$oft function only, and ONLY flushes OUTPUT buffers, NOT stdin!!!

    Please see the FAQ on this site for more information.

    The command you should use is:
    Code:
    int ch;
    while ((ch = getchar()) != '\n' && ch != EOF);

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    115
    [...]

    Quote Originally Posted by rstanley View Post
    Code:
    int ch;
    while ((ch = getchar()) != '\n' && ch != EOF);
    Correct me if I am wrong, can I write those lines into the following ?

    Code:
        int ch;
        while(true)
        {
            ch=getchar();
            bool fic = (ch=='\n' || ch==EOF); // fic : flush input char
            if(fic)
                break;
        }

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    it begs the question as to why however fic doesn't have to be a bool type it can be an int and still work the way you have it may save you having to add another header same as while can be while(1) it may even be better to have a variable thats set to 1 until a newline or eof is reached
    if all you are trying to do is get rid of the new line for your next scanf just use scanf(" %d", &varname) notice the space between the opening " and the % that will ignore any amount of rubish left over from before untill it reaches the first number and allocates it to varname.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    791
    Don't use void main().


    Quote Originally Posted by ordak View Post
    [...]



    Correct me if I am wrong, can I write those lines into the following ?

    Code:
        int ch;
        while(true)
        {
            ch=getchar();
            bool fic = (ch=='\n' || ch==EOF); // fic : flush input char
            if(fic)
                break;
        }
    You can, but it expands 2 lines into 8, which doesn't make the code any more readable or clear as to its purpose.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,908
    Quote Originally Posted by christop View Post
    Don't use void main().
    You can use void main in specific situations (free standing), but while using the Windows OS is not one of them.

    This is a non Standard C, Mickey$oft function only, and ONLY flushes OUTPUT buffers, NOT stdin!!!
    The _flushall function flushes both stdin and stdout.

    MSDN: "All buffers associated with open input streams are cleared of their current contents... "

    It is non-standard, but it is okay using language extentions if you are aware that you are doing it, and aware that it is no longer portable - Especially in the case of throw away code, or when the code is only targetting one platform and the compiling conditions are not going to change.

    You'll quite often see people on here using Linux or POSIX specific extentions and that is also okay if you are aware that it is no longer portable.


    You can, but it expands 2 lines into 8, which doesn't make the code any more readable or clear as to its purpose.
    You'll see it written in the one line quite frequently in many programmes - There is an advantage in writing your code the same way: people reading your code know what you are trying to do without analysing your code.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  2. Character array problem
    By alter.ego in forum C Programming
    Replies: 5
    Last Post: 09-04-2012, 12:19 PM
  3. character array problem .. please help :((
    By amerjamil in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2010, 04:02 AM
  4. Character array initialization problem
    By ThatDudeMan in forum C Programming
    Replies: 7
    Last Post: 12-03-2010, 03:56 PM
  5. Problem with moving character around(using array for map)
    By o0obruceleeo0o in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2003, 05:29 AM

Tags for this Thread