0

I'm trying to solve a basic problem where I have to print a string a output I complete my code and submitted it for test case it prints the correct output but it includes a newline character at the beginning of the output string, due to this the test case fails.

the Input is in the form of:

Arun:40:51:60
roger:49:51:62
steve:80:71:60

The output should be the name of the student with the highest score: steve

but my program outputs: " steve"

this is my program:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int val=0,topMark=0;
    char topStudent[101];
    scanf("%d",&val);
    for(int i=0;i<val;i++)
    {
        char arr[101]; 
        int m1,m2,m3;
        scanf("%[^:]:%d:%d:%d",arr,&m1,&m2,&m3);
        if(topMark<(m1+m2+m3))
        {
            topMark=m1+m2+m3;
            strcpy(topStudent,arr);
        }
    }
    puts(topStudent[i]);   
}

I tried using both printf and puts functions, but it still prints a new line.

I'm expecting a way to get the input without the newline character at the beginning of the string or printing the string without the newline character at the beginning.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    Does this answer your question? [scanf() leaves the newline character in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer) – pmacfarlane Feb 21 '23 at 17:30
  • Note that there are just three conversion specifiers that do not skip white space, and they are `%c`, `%n` and `%[…]` (scan sets). Include a space at the start of the format string: `" %[^:]…"`. If you printed all the student information, you'd find that `Arun` did not have a newline at the start, but both `steve` and `roger` do. – Jonathan Leffler Feb 21 '23 at 17:32
  • @Vignesh Magalingam, Do not use `scanf("%[^:]...` without a _width_. In this case use `scanf(" %100[^:]` and check the return value of `scanf()`. – chux - Reinstate Monica Feb 21 '23 at 17:45

1 Answers1

1

Add a leading space in the format string as for example

scanf(" %[^:]:%d:%d:%d",arr,&m1,&m2,&m3);
      ^^^

It allows to skip white space characters including the new line character '\n' stored in the input buffer after a preceding call of scanf.

From the C 23 Standard (7.23.6.2 The fscanf function)

5 A directive composed of white-space character(s) is executed by reading input up to the first nonwhite-space character (which remains unread), or until no more characters can be read. The directive never fails.

Here is a demonstration program.

#include <stdio.h>

int main( void )
{
    enum { N = 10 };
    char s[N];

    printf( "Enter a string (no more than %d symbols): ", N - 1 );
    scanf( "%9[^\n]", s );

    printf( "s = \"%s\"\n", s );

    printf( "ENter a string (no more than %d symbols): ", N - 1 );
    scanf( " %9[^\n]", s );

    printf( "s = \"%s\"\n", s );
}

The program output might look like

Enter a string (no more than 9 symbols):      A
s = "     A"
Enter a string (no more than 9 symbols):      B
s = "B"
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335