Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program which reads first name and family name of a person, and forms a string containing the full name of the person in the following format:

The first letter of the first name (in uppercase) followed by a (.), a space and the family name, first letter of which should be in uppercase.

For example, given the input “Santanu” and “John”, it should form a string “S. John”. Print the string containing the full name in the above format, and also print the number of vowels, and consonants in it.

What I have tried:

C
#include<stdio.h>
int main()
{
    char firstname[30],familyname[30];
    printf("Enter the first name: ");
    scanf("%s",firstname);
    printf("Enter the family name: ");
    scanf("%s",familyname);

    for(int i=0; i<30; i++)
    {
        char *str;
        gets("%s%s",firstname[0]. familyname[i]);
    }
}
Posted
Updated 8-Feb-22 9:24am
v3

Try with starting some Learn C tutorial or some video tutorial.

some tips: use functions and structs with understandable names.

C++
struct Name {
  char firstName[30];
 char lastName[30];
}
void askForData(struct Name *name); // uses a pointer for input
void print(struct Name *name);
Be careful a pointer is only the address not object.
 
Share this answer
 
v2
Your final part is very wrong:

* gets is the wrong method to call: it should be printf

* Parameters are separated by commas, not dots. Replace the "." with a comma:
gets("%s%s",firstname[0]. familyname[i]);
                        ^
                        |

* The format string is wrong: you don't separate the output strings so the will be printed concatenated together: SJohn, not S. John as required by you homework assignment.

* "%s" prints a string: you pass it a character firstname[0] so that won't work: Format Specifiers in C[^]

* Again, "%s" prints a string: you pass it a character familyname[i] so that won't work either.

* Your loop is unnecessary, and unwanted: even corrected it will print the first character of the first name before each character of the family name!

* str is not used at all!

Stop and think about what you are trying to do: that code looks like it was thrown together without any real planning going on before hand, and that never helps.
This may help you: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
KarstenK 8-Feb-22 11:24am    
I think he should separate input from output. And write a print function which only prints the first character. But that is one of his "last" problems ;-)
OriginalGriff 8-Feb-22 11:57am    
He's a total beginner, from that code - I doubt if writing functions have even appeared on his course yet ... let him learn how to write code that works, before we start talking about making it quality! :laugh:
You have to use Format Specifiers in a correct way and dot need to loop to get all characters from family name. Use the following line after last scanf statement to get your result:
printf("Full Name:  %c. %s", firstname[0], &familyname);
Where %c is used for single character and %s to print family name.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900