English 中文(简体)
你们怎么在数字之后删除 com?
原标题:How do you delete the comma after the number?
  • 时间:2022-09-15 07:36:46
  •  标签:
  • c

So I have this exercise from my Coding class and we were tasked to input the user s input. However, we have to remove the comma of the last number of our output, and I m struggling with how to remove that comma. Can anyone please help?

这是我的法典。

#include <stdio.h>

#define MAX_SIZE 100 

int main(){
    int arr[MAX_SIZE];
    int N, i;
    int * ptr = arr;    
    
    printf("Enter length: ");
    scanf("%d", &N);

    for (i = 0; i < N; i++){
        printf("Enter element %d: ", i+1);
        scanf("%d", ptr);
        ptr++;   
    }

    ptr = arr;

    printf("[");
    for (i = 0; i < N; i++)
    {
        printf("%d,", *ptr);

        ptr++;
    }

        printf("]");

    return 0;
}

for convenience, this is the output I got

Enter length: 5
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
[1,2,3,4,5,]

这就是产出应如何

Enter length: 5
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
[1,2,3,4,5]
问题回答

简单:

const char *sep = "";
printf( "[" );
for( i = 0; i < N; i++ ) {
    printf( "%s%d", sep, arr[ i ] );
    sep = ",";
}
printf( "]" );

Put the commas BEFORE the NEXT value being printed. Specify the "prefix" to be "", until that is changed to ",".

在整个过程中使用“记录索引”比改变点更合适。 很容易忘记再利用它。

并且,增加一些代码,以核实scanf()已给未初始变量N指定了分类值! 如果用户类型four”回应第一种即时情况,那么N的价值就算不上了,哪怕是243,478,658...... 此外,该代码还应核对各用户类别120以填入“代码>100各部分。


在这种情况下(环绕的括号),我们甚至可以消除一条法典。

const char *sep = "[";
for( i = 0; i < N; i++ ) {
    printf( "%s%d", sep, arr[ i ] );
    sep = ",";
}
printf( "]" );

接着,通过改变周围的事物,请“蓝色-磷”

i = 0;
for( char pre =  [ ; i < N; pre =  ,  )
    printf( "%c%d", pre, arr[ i++ ] );
putchar(  ]  );

您可使用一个简单的ternary控制/printf,例如:

    for (i = 0; i < N; i++)
    {
        printf(i ? ",%d" : "%d", *ptr);

        ptr++;
    }

这只印出第1版后的数字。 采用<代码>if-else的类似长式如下:

    for (i = 0; i < N; i++)
    {
        if (i) {
            printf(",%d", *ptr);
        }
        else {
            printf("%d", *ptr);
        }
        
        ptr++;
    }

Also suggest the final puts ("]"); to ensure your program is POSIX compliant -- outputting a final before program exit. Likewise, there is no need to call the variadic printf() function to output a single-character. A simple putchar ( [ ); will do.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签