-1

I will start with the code itself to explain my question

void main()
    {
       char arr[50];
       for ( int i = 0; i < sizeof(arr); i++ )
           arr[i] = '0';

up till here, every value that arr holds is '0'.

Now let us say I filled some of arr values with characters, and got the following

arr = { '0', '0', 'Z', '0', 'A', 'b', 'Q', '0', '0', ..... '0' };

Now, I want to get rid of all the zeros after Q, because it is the last value that is not a zero.

so when I printf ( " %s ", arr); I only get in return all the values that arr holds till that Q

I tried to use memset(), but apparently I used it wrong, or that it doesn't serve my purpose

  • 1
    "up till here, every value that arr holds is 0." **No**! is `'0'` – too honest for this site Apr 15 '17 at 21:03
  • @Olaf you are right the value is 48. Lol. :-P – Gaurav Pathak Apr 15 '17 at 21:13
  • Loop through and find the index Q is on, then after Q set all the indexes to an empty string. Or are you trying to change the size of the array completely? – starlight Apr 15 '17 at 21:18
  • @starlight 'Q' was just an example, it doesn't have to be that way. it can be anything, what matters is that it is the last value that is not '0' – Ahmad Khateeb Apr 15 '17 at 21:24
  • Try starting from the last index in the array (or the length of the string), work your way down and if a value is != 0, then that is the index at which the last value is not '0'. – starlight Apr 15 '17 at 21:26
  • 1
    @GauravPathak: Not necessarily. – too honest for this site Apr 15 '17 at 21:27
  • @starlight that is actually a great idea, but how do I clear/delete the values? – Ahmad Khateeb Apr 15 '17 at 21:28
  • I would just make them an empty string, however, they would still be there as empty strings. There's always the option of creating a new array to hold the values. I'll elaborate this idea in a minute. – starlight Apr 15 '17 at 21:29
  • @AhmadKhateeb: Please understand we are not a tutoring site. Your questions are all taught in every C book, I strongly recommend to get a good beginner's book and work through the lessons. Asking particular questions does not work in programming; this is not "cooking for beginners", there is no "recipie" you can follow to make it through your course! – too honest for this site Apr 15 '17 at 21:34
  • @Olaf I tried looking it up, if you can direct me somewhere, be my guest – Ahmad Khateeb Apr 15 '17 at 21:38
  • http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – too honest for this site Apr 15 '17 at 21:40
  • 1
    At some point, read [What should `main()` return in C and C++](http://stackoverflow.com/questions/204476/). If you're not working on Windows, read it now; if you're working on Windows, you could read it later. – Jonathan Leffler Apr 15 '17 at 21:44
  • `printf("%s"` requires null-terminated array – M.M Apr 16 '17 at 00:24

1 Answers1

0

Start from the last index of the array and decrement until you find a character that is not the 0. This is the last character that is not 0 in the array. To completely get rid of them, my suggestion is to create a new array.

Here's an example,

note: arrayLen is the length of the array:

int index;
for (i = arrayLen - 1; i >= 0; i--)
{

    if (array[i] != '0')
    {
        index = i;
        break;
    }
 }

char newArray [index+2];
for (i = 0; i < index+1; i++)
{
    newArray[i] = array[i];
}
newArray[index+1] = '\0'

The string length of the new string becomes the index of the last character in the array before the 0's + 2. + 2 because one of the indexes is reserved for the null terminator. You can then use that to loop through copy only the relevant characters to your new array.

Let me know if this doesn't make sense and I will update it to explain more!

starlight
  • 130
  • 1
  • 18
  • 1
    Note that the OP has not created a string; there is no null terminator at the end of the array in the question. It isn't clear that you've created a string; I see no assignment of a null terminator in your answer, either. It is a semantic detail, but an important one. The C standard (library — introduction, definition of terms §7.1.1 starts with _"A_ string _is a contiguous sequence of characters terminated by and including the first null character. … The_ length of a string _is the number of bytes preceding the null character…"_ Any sequence of characters without a null isn't a string. – Jonathan Leffler Apr 15 '17 at 21:48
  • 1
    That also means you can't use `strlen()` reliably — the array is not null terminated, so you are accessing memory out of bounds of the array, which is undefined behaviour. – Jonathan Leffler Apr 15 '17 at 21:50
  • It did not do anything as far as I am aware, I have added it as a function to my code, named it `void clean(char array[100])` and called it inside the `main void()` but nothing happened, did I do something wrong? – Ahmad Khateeb Apr 15 '17 at 21:52
  • Never mind, it somewhat serves its purpose just needs some modification to suit my code – Ahmad Khateeb Apr 15 '17 at 22:03
  • @JonathanLeffler Thanks for catching that, I forgot the null terminator. You're right, strlen() would not work, a loop would be needed in order to get the length of the array instead since it is not terminated with the null character. – starlight Apr 15 '17 at 22:05
  • 1
    Also, you can't return a pointer to a local array — the array no longer exists once the function returns. Your comment that says (said?) "return the new array of characters that you shortened to only include relevant characters" is not a good idea. You'd have to dynamically allocate the array in the called function, and arrange to free it in the calling function (or somewhere else other than the function doing the analysis). You also need to communicate the operational length of the amended array somehow — null terminator or something. – Jonathan Leffler Apr 15 '17 at 22:08
  • @starlight `for (i = strlen(array)-1; i >= 0; i++)` the i should have -- next to it instead of ++ right? – Ahmad Khateeb Apr 15 '17 at 22:09
  • @JonathanLeffler I worded that poorly. Thanks for catching that. – starlight Apr 15 '17 at 22:12