0

I have a question for a homework assignment. This is C, not C++ (sorry! i didn't know where else to go).

I have 2 character arrays, declared as char s1[100] and s2[100] in my main function.
The part of this assignment I am having trouble with is the readString function. But I cannot figure out how to use getchar properly in order for the white spaces to be read and print out the entire string. The directions for the assignment also tell me to return the starting address to this string at the end of the function, but I have no idea what that means.

the code i used is straight from the book, but it either prints nothing, or only the second word of a string.

Here is my code

char* readString(char* s1,char* s2)
{
       *s1 = (char *) malloc(100 * sizeof(char));
       *s2 = (char * ) malloc(100 * sizeof(char));
       char c;
       int i = 0;
       printf("Enter first string: ");
       while((c = getchar() != '\n')) {
             s1[i++] = c;
       }
       s1[i] = '\0';
       printf("Enter second string: ");
       while((c = getchar() != '\n')) {
             s2[i++] = c;
       }
       s2[i] = '\0';
       puts("\nFirst string is: ");
       puts(s1);
       puts("\nSecond string is: ");
       puts(s2);
2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by coreyhughes
0

I have a question for a homework assignment. This is C, not C++ (sorry! i didn't know where else to go).

Maybe in the C forum? I pity the future... :icon_rolleyes:

0

Maybe in the C forum? I pity the future... :icon_rolleyes:

My apologies, kind sir, for rushing to create this thread and not checking other areas of the forum.

Edited by coreyhughes: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.