I have array like a[]={1,121,222,44,451,5510,414}
.I want to print array output as j={1,222,44}
. how can I do this in the C language.
Asked
Active
Viewed 120 times
3 Answers
1
You can try something like this:
#include <stdio.h>
#include <limits.h>
int same_digit(int x);
int
main(void) {
int array[]={1,121, 222,44,451,5510,414};
size_t arrsize = sizeof(array)/sizeof(*array), count = 0;
int j[arrsize], i;
for (i = 0; i < arrsize; i++) {
if (same_digit(array[i])) {
j[count++] = array[i];
}
}
printf("j={");
for (i = 0; i < count; i++) {
printf("%d", j[i]);
if (i != count-1) {
printf(",");
}
}
printf("}\n");
return 0;
}
int
same_digit(int number) {
int digit;
if (number < 0) {
if (number == INT_MIN) {
return 0;
}
number *= -1;
}
digit = number % 10;
while (number > 0) {
if (number % 10 != digit) {
return 0;
}
number = number/10;
}
return 1;
}
Output:
j={1,222,44}

RoadRunner
- 25,803
- 6
- 42
- 75
-
and depends on his compiler, he might not be able to declare variables in his functions.. – I-V Dec 10 '16 at 10:17
-
@I-V: what kind of compiler does not let you declare variables in functions? – chqrlie Dec 10 '16 at 11:31
-
It's "out of my league" but just look in here http://stackoverflow.com/questions/288441/variable-declaration-placement-in-c – I-V Dec 10 '16 at 11:35
-
This works for me @radh. Are you sure you copied it correctly? – RoadRunner Dec 13 '16 at 15:59
0
given array a[],
#define SIZE_OF_ARRAY(_array) (sizeof(_array) / sizeof(_array[0]))
int i;
int j;
int digit;
int res[20] = { 0 };
bool sameDigit(int num)
{
digit = num % 10;
while (0 != num)
{
if ((num % 10) != digit)
{
return false;
}
num = num / 10;
}
return true;
}
int main()
{
j = 0;
for (i=0; i<SIZE_OF_ARRAY(a); ++i)
{
if (sameDigit(a[i]))
{
res[j] = a[i];
++j;
}
}
return 0;
}

I-V
- 725
- 4
- 10
-
1You forgot the include files, also `a.length()` is not C code, `res = {0};` is meaningless... – chqrlie Dec 10 '16 at 10:05
-
First, I have no idea what files you are talking about... Secondly thanks, I fixed the length part,, And lastly, initializing arrays is a good practice so it isn't meaningless – I-V Dec 10 '16 at 10:08
-
You are not programming in C here. The OP wants to print the matching elements, `
` seems required, and since you use `true` and `false`, ` – chqrlie Dec 10 '16 at 10:11` too. Defining `i`, `j`, `res` and `digit` as global variables is **very** bad style. -
`res = {0};` isn't valid.(This can't compile). and `int res[20];` is already initialized. – BLUEPIXY Dec 10 '16 at 10:12
-
he didn't say he want to print it. and yes, I didn't include headers because I showed him how to do what he asked and not how to write (and compile) C code – I-V Dec 10 '16 at 10:13
-
@BLUEPIXY I have no idea what compiler you use but look here: http://stackoverflow.com/questions/201101/how-to-initialize-all-members-of-an-array-to-the-same-value – I-V Dec 10 '16 at 10:15
-
-
-
-
FYI It is not necessary for static class variable. `int res[20];` and `int res[20] = { 0 }; ` have the same meaning. – BLUEPIXY Dec 10 '16 at 10:25
-
as I said before, it's just a good practice in order to prevent future errors – I-V Dec 10 '16 at 10:27
-
-
@I-V: how is is *much worse* to include unnecessary standard headers, which by the way I did not? How is `printf` unsafe? – chqrlie Dec 10 '16 at 10:40
-
-
1. just read about printf vulnerabilities! it can easily be exploited (even crash by strange input) 2. you are crucifying me for no reason, my code works just fine and answers the question. 3. including stdio when you don't get an input is a waste of the binary's size! the output header instead. – I-V Dec 10 '16 at 10:42
-
What else are we supposed to use then @I-V? This is `C`, `printf` is fine. – RoadRunner Dec 10 '16 at 10:46
-
@I-V: `printf` is not an *input* function, you may be referring to an indirect vulnerability involving the `%n` conversion specifier. Microsoft introduced `printf_s` which disables `%n` along with a myriad of similar *safer* versions of the C library functions, a contorted solution for a problem that is not so easy to exploit. Everybody still uses `printf` because `printf_s` is not available everywhere. OTOH It took them more than 10 years to update their own C tool chain, hardly a leader's behavior. – chqrlie Dec 10 '16 at 10:48
-
http://stackoverflow.com/questions/16891019/how-to-avoid-using-printf-in-a-signal-handler – I-V Dec 10 '16 at 10:50
-
1@I-V, I don't know why we're worrying about that for this simple question. – RoadRunner Dec 10 '16 at 10:56
0
Here is a solution:
#include <limits.h>
#include <stdio.h>
int a[] = { 1, 121, 222, 44, 451, 5510, 414 };
#define lengthof(a) (sizeof(a) / sizeof((a)[0]))
int same_digit(int num) {
if (num < 0) {
if (num == INT_MIN)
return 0;
num = -num;
}
int digit = num % 10;
while ((num /= 10) != 0) {
if ((num % 10) != digit)
return 0;
}
return 1;
}
int main(void) {
const char *sep = "";
printf("j={");
for (size_t i = 0; i < lengthof(a); i++) {
if (same_digit(a[i])) {
printf("%s%d", sep, a[i]);
sep = ",";
}
}
printf("}\n");
return 0;
}
Output:
j={1,222,44}

chqrlie
- 131,814
- 10
- 121
- 189