Write a C program to input your full forename and full surname. e.g James McCarren and display in one string your complete initial and surname i.e J McCarren . Your initial should always be in upper case.
We could use pointers or char arrays let us do both
Answer: pointers
#include
#include
/* malloc's prototype is in stdlib.h */
#include
/* toupper's prototype is in ctype.h */
#include
void main()
{
char prompt;
Date: 26th August 2012
Version 1.0
Function : Example to show string manipulation
Modifications: none*/
char *text,*forename,*surname;
/* We must allocate space for the strings say 80 chars so we use 81 because the terminator takes 1
space*/
text = (char *)malloc(81);
forename = (char *)malloc(81);
surname = (char *)malloc(81);
if ((text == NULL)|| (forename == NULL) || (surname== NULL))
{
printf("Memory full Error type -1 \n\r");
exit(1);
}
printf("Please enter in your fore and surname name\n\r");
/* Note no address operator required because text is an address
Scanf will read up to a white space and assign that to forename
the rest will be assigned to surname*/
scanf("%s%s",forename,surname);
/* We can extract the initial of the forename and store in in temp*/
*text = toupper(*forename);
*(text+1) = ' ';
*(text+2) = '\0';
/*We can now add together the two strings */
strcat(text,surname);
printf("Hello %s\n\r",text);
printf("Press and key to exit \n\r");
scanf("\n%c",&prompt);
}