Hey guys Im having trouble printing out a string. Could someone help me out? I w
ID: 3619109 • Letter: H
Question
Hey guys Im having trouble printing out a string. Could someone help me out? I would like to enter a string like: "julie perez" and print out "julie perez" but my program only outputs "julie". The code that prints out only "julie" is the following: practice.c: #include <stdio.h> #include "practice.h" #define TWENTY_FIVE 25 int main () { char cara[TWENTY_FIVE]; printf("Enter String> "); scanf("%s", cara); display(cara); return 0; } practice.h (header file): ]#ifndef PRAC_H #define PRAC_H void display(const char*str); #endif practicefuncs.c (function): #include <stdio.h> void display(const char *str) { while (*str != '') { if (*str == ' ') { printf(" "); } else { printf("%c", *str); } str++; } printf(" "); } I think my functions are correct because when i declared a string (Ex. cara[] = "julie perez") it displayed "julie perez". I think its the input from user im not getting. Well they way i have it, when it scans the string from the user, is it saying put a '' after the first blank?? In this case "julie" so thats why its only printing julie??Explanation / Answer
please rate - thanks scanf only reads data until it gets to a whitespace. so as soon asit sees the space between your 2 words it stops, only reading thefirst name. you need to use gets instead of scanf what was changed is in red, everything else is unchanged int main () { char cara[TWENTY_FIVE]; char *in; printf("Enter String> "); in=gets(cara); display(cara); return 0; }