I'm trying to put in upper case a first letter in a name. Something does not work. Also, I want to put whole surname in upper case. Which doesn't work too. Somehow only the first letter(of the above mentioned) get an upper case.
I have this code, and it does not work as expected:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
struct Student
{
char name[50];
char surname[50];
int year_of_birth;
};
void display(struct Student array[],int size);
int main()
{
int n;
printf("enter number: ");
scanf("%d",&n);
struct Student* array = malloc(n*sizeof(array));
for(int i=0; i<n; i++)
{
printf("enter name: ");
scanf(" %[^\n]*c",array[i].name);
printf("enter surname: ");
scanf(" %[^\n]*c",array[i].surname);
printf("enter birth year: ");
scanf(" %d",&array[i].year_of_birth);
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
array[i].surname[j] = toupper(array[i].surname[j]);
array[i].name[0] = toupper(array[i].name[0]);
if(isspace(array[i].name[j]))
array[i].name[j+1] = toupper(array[i].name[j+1]);
}
}
puts("");
puts("::SHOWING::");
display(array,n);
return 0;
}
void display(struct Student array[],int size)
{
for(int i=0; i<size; i++)
{
printf("Student info %d: %s %s %d \n",i+1,array[i].name,array[i].surname,array[i].year_of_birth);
}
}
The output:
enter number: 2
enter name: olivia emma
enter surname: james
enter birth year: 2000
enter name: john noah
enter surname: Smith
enter birth year: 2002
::SHOWING:: Student info 1: Olivia emma JAmes 2000 Student info 2: John noah SMith 2002
Expected output:
Student info 1: Olivia Emma JAMES 2000 Student info 2: John Noah SMITH 2002