As requested by @Jongware, a solution based on the scanf family, a very convenient means to parse strings if the input's structure is relatively constant.
Given the input string
4273 Багров Д. С. 5454 знззз
I'll assume for this example that the constant pattern we're after is an int followed by 3 strings, followd by an int and a string. There are other ways, I'll get back to these.
A very basic demo:
#include <stdio.h>
int main(void) {
char * inputdata = "4273 Багров Д. С. 5454 знззз";
// variables to receive the scanned data
int firstint, secondint;
char firststring[32];
char secondstring[32];
char thirdstring[32];
char fourthstring[32];
// important, you should check whether the number of converted elements
// matches what you expect:
int scannedelements;
// let's scan the input
scannedelements = sscanf (inputdata,"%d %s %s %s %d %s",&firstint, &firststring, secondstring,
thirdstring,&secondint,fourthstring);
// and show what we found. Notice the similarity between scanf and printf
// but also note the subtle differences!!!
printf("We scanned %d %s %s %s %d %s\n",firstint, firststring, secondstring,
thirdstring,secondint,fourthstring);
printf("That's a total of %d elements %d\n",scannedelements);
return 0;
}
Output:
We scanned 4273 Багров Д. С. 5454 знззз
That's a total of 6 elements
Notice that I scanned the field you named exam into an integer, you can easily extract digits out of it by a loop of digit = data % 10; data = data / 10;
Now, the fact that the first group of strings is chopped into 3 different outputs could be annoying. Depending on the output data, we could instruct sscanf to read until it encounters a digit:
#include <stdio.h>
int main(void) {
char * inputdata = "4273 Багров Д. С. 5454 знззз";
// variables to receive the scanned data
int firstint, secondint;
char firststring[32];
char secondstring[32];
char thirdstring[32];
char fourthstring[32];
// important, you should check whether the number of converted elements
// matches what you expect:
int scannedelements;
// Alternatively, let's scan the group of 3 strings into 1 variable
scannedelements = sscanf (inputdata,"%d %[^0-9] %d %s",&firstint, firststring, &secondint,fourthstring);
// and show what we found.
printf("We scanned %d %s %d %s\n",firstint, firststring,secondint,fourthstring);
printf("That's a total of %d elements %d\n",scannedelements);
return 0;
}
which outputs:
We scanned 4273 Багров Д. С. 5454 знззз
That's a total of 4 elements -1079150400
Notice the trailing space in Багров Д. С.
, which may or may not be a problem, but it's easily removed.
For your convenience, this code is available on ideone: http://ideone.com/4gFlxf#sthash.KQfhcYxr.dpuf
This example barely scratches the surface of what's possible with scanf, I encourage you to explore its manpage to discover more possibilities.
--
On how to calculate the average score:
#include <stdio.h>
int main(void) {
int inputdata = 24680;
int average = 0;
int number_digits = 0;
int digit = 0;
int digits = 0;
while (inputdata > 0) {
digit = inputdata % 10; // modulo by 10 is the last digit
average += digit;
digits++;
inputdata = inputdata / 10; // integer division by 10 = remove last digit
}
if (digits > 0) { // to avoid dividing by zero is some edge case
printf ("The average over %d scores is %.1f\n", digits, (double) average / digits);
} else {
printf ("As the input was 0, the average is 0");
}
return 0;
}