#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:|

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.