how to print string with spaces 
Author Message
 how to print string with spaces

The following program prints the first word
How can it print entire sentence ?

Sameer

#include<stdio.h>
#include<stdlib.h>

main()
{
char string[100];

printf("Enter the string \n");
scanf("%s", &string);
printf("%s", string);

Quote:
}



Mon, 25 Jul 2005 20:49:00 GMT  
 how to print string with spaces

Quote:

> The following program prints the first word
> How can it print entire sentence ?

> Sameer

> #include<stdio.h>
> #include<stdlib.h>

> main()
> {
> char string[100];

> printf("Enter the string \n");
> scanf("%s", &string);
> printf("%s", string);

> }

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

size_t OK_input(char *array, size_t arraysize)
{
    size_t length;

    if (!fgets(array, arraysize, stdin) || feof(stdin)) {
        if (ferror(stdin)) {
            fputs("\n\n\nferror 1\n\n", stderr);
            exit(EXIT_FAILURE);
        }
        if (strlen(array)) {
            fputs("\n\n\nfeof 1\n", stderr);
            exit(EXIT_FAILURE);
        } else {
            fputs("\n\n\nDon't do that!\n\n", stderr);
            clearerr(stdin);
            length = 0;
        }
    } else {
        length = strlen(array) - 1;
        if (length) {
            if (array[length] != '\n') {
                do {
                    if(!fgets(array, arraysize, stdin) ||
                        feof(stdin)){
                        if (ferror(stdin)) {
                            fputs("\n\n\nferror 2\n", stderr);
                            exit(EXIT_FAILURE);
                        } else {
                            fputs("\n\n\nfeof 2\n", stderr);
                            exit(EXIT_FAILURE);
                        }
                    } else {
                        length = strlen(array) - 1;
                    }
                } while (array[length] != '\n');
                fprintf(stderr, "\n"
                    "Don't type more than %lu character%s\n"
                    "before hitting the Enter key.\n\n",
                    arraysize - 2lu, arraysize == 3 ? "" : "s");
                length = 0;
            }
        }
    }
    return length;

Quote:
}

int main(void)
{
    char string[100];

    do {
        puts("Enter the string");
        fflush(stdout);
    } while (!OK_input(string, sizeof string));
    printf("\n%s\n", string);
    return 0;

Quote:
}

--
pete


Mon, 25 Jul 2005 21:16:47 GMT  
 how to print string with spaces
Hello Sameer,

Your problem is not with printf() but with scanf() which will stop storing
characters in the array when it encounters whitespace.



Mon, 25 Jul 2005 21:21:16 GMT  
 how to print string with spaces

Quote:

> The following program prints the first word
> How can it print entire sentence ?

> Sameer

> #include<stdio.h>
> #include<stdlib.h>

stdlib.h not need in this code snippet

Quote:

> main()

int main(void)

Quote:
> {
> char string[100];

> printf("Enter the string \n");
> scanf("%s", &string);

change to:
scanf("%99[^\n]%*[^\n]", string);

Quote:
> printf("%s", string);

printf("%s\n",string);
return 0;

Quote:

> }

#include<stdio.h>

int main(void)
{
    char string[100];
    printf("Enter the string \n");
    scanf("%99[^\n]%*[^\n]", string);
    getchar(); /*remove '\n' from stdin */
    printf("%s\n",string);
    printf("Enter another string: ");
    fflush(stdout);
    scanf("%99[^\n]%*[^\n]", string);
    getchar();
    printf("%s\n",string);
    return 0;

Quote:
}

-------

Al Bowers
Tampa, FL. USA

http://www.geocities.com/abowers822
comp.lang.c



Mon, 25 Jul 2005 21:32:14 GMT  
 how to print string with spaces

Quote:

> The following program prints the first word
> How can it print entire sentence ?

Your problem is not with printing the sentence. Your problem is what
retrieving the sentence.

Quote:
> Sameer

> #include<stdio.h>
> #include<stdlib.h>

There is no need for <stdlib.h>. Both function calls are prototyped in
<stdio.h>.

Quote:
> main()

A function without an explicitly defined return type will default to int.
So this function is equivalent to:

        int main()

You failed to place a return 0; at the end of main. Note, it should not be
changed to:

        void main()

search the newsgroup for an explanation. You should actually use:

        int main(void)

Quote:
> {
> char string[100];

> printf("Enter the string \n");
> scanf("%s", &string);

Here is your problem. The %s directive will get input from the stdin
stream until whitespace is encountered. This means it stops when it
reaches a return, space, tab, etc.

I would just avoid using scanf. In addition to the problems you are
encountering it has a whole other set of things that will confuse you.
Just use fgets to read in a line of text. If you don't want the '\n' on
the end of the string then use:

        strtok(string, "\n");

to get rid of it.

Quote:
> printf("%s", string);

> }

---

"iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int); while
(*i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);}return 0;}


Mon, 25 Jul 2005 22:08:43 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Using CDaoDatabase with Excel file converts string with only spaces to empty string

2. Problem in wordwrapping when printing words without spaces.

3. allocating string space for long decimal

4. Help to trim spaces of a string

5. Input of a string containing spaces?

6. removing unwanted white space from a string

7. String Data and spaces

8. string space

9. A proposed replacement for gets (was: I/P of a string containing spaces)

10. A proposed replacement for gets (was: Input of a string containing spaces)

11. Backslash-space in string literals?

12. Stripping spaces from a string

 

 
Powered by phpBB® Forum Software