#include<stdio.h>
int main()
{
        int n,i,j;
        char p[10][10];
        printf("please enter 10 words");
        for(i=0;i<10;i++)
        {
                for(j=0;j<10;j++)
                {
                        gets(p[i][j]) ;
                }
                printf("\n");
        }
        for(i=0;i<10;i++)
        {
                for(j=0;j<10;j++)
                {
                        printf("%c",p[i][j]);
                }
                printf("\n");
        }
        return 0;}

I actualy want to read 10 string from keyboard and print them but not gettin the output
wht to do guys:S
thanks in advance:|

Dani AI

Generated

Quick summary and what went wrong (based on the posts by , , and others): the code mixes unsafe input routines, misdeclared/used buffers, and incorrect string handling. Common bugs seen here: using gets() or scanf("%s", ...) without limits (buffer overflow), assigning arrays (e.g. temp[0] = p[i] — not allowed), walking past the last element (comparing p[i] with p[i+1] when i reaches the last index), and using goto to implement character-by-character comparison instead of higher-level string functions.

Safe, practical approach

  • Decide the maximum word length and allocate room for the terminating NUL. For example, if max 9 chars per word use buffers of length 10.
  • Read each line with fgets() (give the buffer size), then strip the trailing newline if present. That avoids overflow and preserves whitespace handling.
  • For lexicographic ordering use strcmp() to compare strings. Swap whole strings with a temporary buffer (you cannot assign arrays directly).

Example pattern (concept, not verbatim from earlier posts):

#define N 3
#define LEN 10
char p[N][LEN];
/* read each string with fgets and strip newline */
...
/* sort using strcmp; swap with a temp buffer (same LEN) */
...

Specific fixes for the posted code

  • Replace manual char-by-char comparison and goto with strcmp. It correctly handles end-of-string and differing lengths.
  • Fix swaps: declare char tmp[LEN]; and use strcpy/strncpy (careful with strncpy behavior) or memmove to copy buffers.
  • Fix loop bounds: bubble sort inner loop must stop before i+1 would be out of range (e.g., inner limit n-1-pass).
  • If you must use scanf, add a width: scanf("%9s", p[i]); for a 10-byte buffer.

Extra tips

  • Compile with warnings (-Wall -Wextra) and use AddressSanitizer (-fsanitize=address) when available to catch overflows.
  • gets() is removed from modern C; avoid it entirely.

Recommended Answers

All 7 Replies

Try something like this

#include<stdio.h>

int main()
{
	int i = 0;
	char p[10][10];
	printf("please enter 10 words\n");

	for(i = 0; i < 10; i++)
	{		
		fgets(&p[i][0], 10, stdin);
		printf("\n");
	}

	for(i = 0; i < 10; i++)
	{
		printf("%s", &p[i][0]);
		printf("\n");
	}

	return 0;
}

A few pointers..Please use code tags and never use gets() its a dangerous function.

#include<stdio.h>
int main()
{
	int i;
        char p[10][10];
        printf("please enter 10 words");
        for(i=0;i<10;i++)
        {
		gets(p[i]) ;
		printf("\n");
	}
	for(i=0;i<10;i++)
	{
		puts(p[i]);
		printf("\n");
        }
        return 0;
}

try this one

So the advice that was given to NOT use gets(), and to USE code tags, was completely lost on you?

Try again. ;)

#include<stdio.h>
int main()
{
        int i,j,pass;
        char p[3][10],temp[1][10];
        temp[0]='\0';
        printf("please enter 3 words\n");
        for(i=0;i<3;i++)
        {
                scanf("%s",&p[i]);

        }

        for(pass=0;pass<3;pass++)
        {
                for(i=0;i<3;i++)
                {
                        j=0;

                        repeat:if(p[i][j]<p[i+1][j])
                               continue;
                               else if(p[i][j]>p[i+1][j])
                               {
                                        temp[0]=p[i];
                                        p[i]=p[i+1];
                                        p[i+1]=temp[0];
                               }
                               else
                               {
                                        j++;
                                        goto repeat;
                               }
                }
        }
        for(i=0;i<3;i++)
        {
                printf("%s\n",p[i]);
        }
        return 0;
}

i have modified the code but i'm still not gettin.gerard wht does fgets do
i have it to read three string and arrange them in dictionary order.please rectify this

first you should remove any goto from your code. Always use conditional construct (if, while, for), as they are easy to manage and follow for you and avoids CPU overheads as well.

#include<stdio.h>
int main()
{
        int i,j,pass;
        char p[3][10],temp[1][10];
        temp[0]='\0';
        printf("please enter 3 words\n");
        for(i=0;i<3;i++)
        {
                scanf("%s",&p[i]);

        }

        for(pass=0;pass<3;pass++)
        {
                for(i=0;i<3;i++)
                {
                        j=0;

                        repeat:if(p[i][j]<p[i+1][j])
                               continue;
                               else if(p[i][j]>p[i+1][j])
                               {
                                        temp[0]=p[i];
                                        p[i]=p[i+1];
                                        p[i+1]=temp[0];
                               }
                               else
                               {
                                        j++;
                                        goto repeat;
                               }
                }
        }
        for(i=0;i<3;i++)
        {
                printf("%s\n",p[i]);
        }
        return 0;
}

I'm pretty sure that your line 10

scanf("%s",&p[i]);

Is just as dangerous gets().

Try running this program

#include<stdio.h>

int main()
{
	char b[] = "test this";
	char a[10];
	

	fputs("enter a long word like Pseudopseudohypoparathyroidism->", stdout);
	scanf("%s", a);

	fprintf(stdout, "a->%s\n", a);
	fprintf(stdout, "b->%s\n", b);	
	return 0;
}

The output on my machine was

a->Pseudopseudohypoparathyroidism
b->parathyroidism

which indicates a buffer overflow.

commented: Good example of buffer overflow +5

The output on my machine was

a->Pseudopseudohypoparathyroidism
b->parathyroidism

which indicates a buffer overflow.

Thanks gerard i knew scanf was 'dangerous', but your example is a eye opener.

In my case the output was

a->Pseudopseudohypoparathyroidism
b->dohypoparathyroidism

BTW whats the meaning of this word?;)

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.