How to remove whitespace at the end of each line using c language? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to remove whitespace at the end of each line using c language?

Title: Numbered Triangle Problem: You need to print this pattern upto N. For N = 4, 1 1 2 1 2 3 1 2 3 4 Input: A single positive integer N. Output: Numbered Triangle upto N. Do not leave trailing whitespaces at the end of each line. Constraints: 1 ≤ N ≤ 9 Sample Input: 3 Sample Output: 1 1 2 1 2 3

21st Oct 2019, 5:05 AM
aejaz ahmed
aejaz ahmed - avatar
7 Answers
0
For evading the last space in a row, you could use this pattern: for(int j=0; j<4; ++j) printf("%d%s", j, j<3? " ": "");
21st Oct 2019, 3:39 PM
HonFu
HonFu - avatar
+ 1
/*i got the answer thanks for sharing ideas love you*/ #include<stdio.h> int main() { int n,i,j; char sp=' '; scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) { printf("%d",j); if(i!=j) { printf("%c",sp); } } printf("\n"); } return 0; }
21st Oct 2019, 1:35 PM
aejaz ahmed
aejaz ahmed - avatar
0
How did you get that newline? printf function in C should not put newlines automatically.
21st Oct 2019, 6:58 AM
Seb TheS
Seb TheS - avatar
0
But how can i solve? Help me
21st Oct 2019, 7:10 AM
aejaz ahmed
aejaz ahmed - avatar
0
I have no idea. Can you link the code where you met this problem?
21st Oct 2019, 7:11 AM
Seb TheS
Seb TheS - avatar
0
Title: Numbered Triangle Problem: You need to print this pattern upto N. For N = 4, 1 1 2 1 2 3 1 2 3 4 Input: A single positive integer N. Output: Numbered Triangle upto N. Do not leave trailing whitespaces at the end of each line. Constraints: 1 ≤ N ≤ 9 Sample Input: 3 Sample Output: 1 1 2 1 2 3 Link: https://code.dcoder.tech/question/5b55730fab46cd2f99d3bf80
21st Oct 2019, 7:13 AM
aejaz ahmed
aejaz ahmed - avatar
0
Expected: 1 1 2 1 2 3 1 2 3 4 Output: 1 1 2 1 2 3 1 2 3 4 Miika i try to code Space before" %d" But it's 1st space after that number.
21st Oct 2019, 7:53 AM
aejaz ahmed
aejaz ahmed - avatar