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);

Recommended Answers

All 2 Replies

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:

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.