Question

C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char...

C program help

#include
#include
#include
void PrintName(char firstname[16], char lastname[16]);
int main ()
{ char firstname[16];
char lastname[16];
printf("please enter your first name:");
scanf("%s",firstname);
printf("please enter your last name:");
scanf("%s",lastname);
PrintName(firstname,lastname);
return 0;
}
void PrintName(char firstname[16], char lastname[16]){
char fullname[34];
*fullname=*firstname+*lastname;
(char*) malloc (sizeof (*fullname));
if (strlen(firstname) > 16||strlen(lastname)>16||strlen(fullname)>16)
fflush(stdin);
else
printf(" the full name is %s %s \n",firstname,lastname);
printf(" the full name is-> %s",fullname);/*why is one not run*/
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In C Program the operators can not be used for any string manipulation. You have to use certain library functions to achieve this or you have to use logics to achieve this.

So the error is with this statement

*fullname=*firstname+*lastname;

This can be replaced by

strcpy(fullname,firstname); //copy the first name to fullname
strcat(fullname," "); //concatnate a space with fullname
strcat(fullname,lastname);//concatnate the last name with full name

Secondly you are using pointers so before that you have to declare it,no doubt strings and pointer are closely interrelated but without declaration you cannot use the pointers like this.

Here is the rectified program

#include<stdio.h>
#include<string.h>
void PrintName(char firstname[16], char lastname[16]);
int main ()
{ char firstname[16];
char lastname[16];
printf("please enter your first name:");
scanf("%s",firstname);
printf("please enter your last name:");
scanf("%s",lastname);
PrintName(firstname,lastname);
return 0;
}
void PrintName(char firstname[16], char lastname[16]){
char fullname[34];
strcpy(fullname,firstname);
strcat(fullname," ");
strcat(fullname,lastname);
//*fullname=*firstname+*lastname;
//printf(" the full nameeeee is %s",fullname);
(char*) malloc (sizeof (*fullname)); //this code also not required
if (strlen(firstname) > 16||strlen(lastname)>16||strlen(fullname)>16)
fflush(stdin);
else
printf(" the full name is %s %s \n",firstname,lastname);
printf(" the full name is %s",fullname);
return 0;
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Convert this C program to Js (Java script) from Visual Studio Code #include<stdio.h> int main(){ char...

    Convert this C program to Js (Java script) from Visual Studio Code #include<stdio.h> int main(){ char firstName[100]; char lastName[100]; printf("Enter Your Full Name: \n"); scanf("%s %s", firstName, lastName); printf("First Name: %s\n", firstName); printf("Last Name: %s\n", lastName); return 0; }

  • I'm having trouble getting my program to output What is your first name? damion what is...

    I'm having trouble getting my program to output What is your first name? damion what is your last name? anderson damion, Your first name is 6 characters Your last name, anderson, is 8 characters Your name in reverse is: noimad nosredna This is my code so far: #include <stdlib.h> #include <stdio.h> void sizeOfName(char** name); void printSizeOfName(int *first, int *last, char** name); void reverseString(int *first, int *last, char** name); int main() {   char** name;   name = (char**)malloc(2*sizeof(char*));   name[0] = (char*)malloc(100*sizeof(char));   name[1]...

  • Q.25. Given the following program, you are asked to discuss memory leaks caused in its execution....

    Q.25. Given the following program, you are asked to discuss memory leaks caused in its execution. Determine which memory locations get uncontrolled. (2 points) 4 6 7 8 9 10 11 12 B 13 14 15 16 17 18 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 5 void main() { char *aString = "Memory leaks?"; char **strList; int i, n = 5; strlist = (char**)malloc(n*sizeof(char*)); for (i=0; i<n; i++) { printf("\nstring %d ", i+1); strList[i] = (char*)malloc(50*sizeof (char));...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z')...

    Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z') return c-32; return c; } //converting sentance to upper void convertSentence(char *sentence){ int i=0; while(sentence[i]!='\0'){ //convert to upper for each character sentence[i] = toUpper(sentence[i]); i++; } } //converting all sentences into uppercase void convertAll(char **sentenceList, int numOfSentences){ int i=0; while(i<numOfSentences){ //calling convertsentence function to conver uppercase convertSentence(sentenceList[i]); i++; } } sentences.c #include<stdio.h> #include<stdlib.h> #include "convert.c" int main(){ //declaring character array char **mySentences; int noOfSentences;...

  • Help with my code: The code is suppose to read a text file and when u...

    Help with my code: The code is suppose to read a text file and when u enter the winning lotto number it suppose to show the winner without the user typing in the other text file please help cause when i enter the number the code just end without displaying the other text file #include <stdio.h> #include <stdlib.h> typedef struct KnightsBallLottoPlayer{ char firstName[20]; char lastName[20]; int numbers[6]; }KBLottoPlayer; int main(){ //Declare Variables FILE *fp; int i,j,n,k; fp = fopen("KnightsBall.in","r"); //...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • How would you correct this function in C to prevent buffer overflow? void nameBuilder() {   ...

    How would you correct this function in C to prevent buffer overflow? void nameBuilder() {    char fname[10];    char lname[10];    char fullname[20];    printf("Enter your first name: ");    scanf("%s", fname);    printf("Enter your last name: ");    scanf("%s", lname);    strcat(fullname, fname);    strcat(fullname, " ");    strcat(fullname, lname);    printf("Welcome. %s\n", fullname);    return; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT