Page 1 of 15

problem 490 - rotating sentences

Posted: Fri May 03, 2002 3:33 pm
by Shahid
hi ,i am getting runtime error from the judge. can anyone help me?
another question: isn't the problem statement ensures that tabs are not given as input, or it should be ignored in analyzing the input?

here is my code:

[c] /*@BEGIN_OF_SOURCE_CODE*/





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


void main()
{
char in[100][100] = {0};

int i= 0, j = 0, k, l, max = 0;

for( ; ; )
{
gets(in);
if(!strcmp(in, NULL))
break;

if(strlen(in) > max)
max = strlen(in);
i++;
}

for(j = 0; j < max; j++)
{
for(k = i-1; k >= 0; k--)
if(strlen(in[k]) > j)
printf("%c", in[k][j]);
else
printf(" ");

printf("\n");
}

}
/*@END_OF_SOURCE_CODE*/
[/c]
thanx in advance for helping

Posted: Sat May 04, 2002 12:44 am
by Stefan Pochmann
One problem might be that sentences can have 100 characters, but you only have space for 99 characters (don't forget the '\0' character that marks the end of the string).

Posted: Sun May 05, 2002 2:48 am
by Shahid
i increased the array size , but the result is same :-?

Posted: Sun May 05, 2002 4:49 am
by Stefan Pochmann
Maybe it's a bad idea to compare to the string at address NULL, since you're not allowed to access that address. Try comparing it to the empty string "".

Posted: Thu May 09, 2002 12:23 pm
by Shahid
right u r, stefan. i just solved removing the null......
thanx for ur help. but one more question. isn't a empty string = "\0" = NULL...then whats the problem null is doing?

Posted: Thu May 09, 2002 1:58 pm
by Ivor
When you pass "\0" C actually allocates memory for this one symbol, and passes the address. When you try to compare to NULL, you're trying to compare your string to the one at address NULL (or 0) and that's giving you the runtime error. It's the same if you tried to compare to string at some arbitrary address.

Ivor

490 runtime error

Posted: Mon Jun 24, 2002 11:35 pm
by mido
I get a runtime error with this...this almost always happens if I use string functions..anyways, here's my code if you wanna look :
[cpp]
#include <iostream.h>
#include <string.h>

char *str2[100];

int
i=0,
j=0,
max=0,
count=0;

void Rem_Tab(char*& x)
{
for (int i=0;i<strlen(x);i++)
{
while (x==9)
{
for (int j=i;j<strlen(x)-1;j++)
{
x[j]=x[j+1];
}
x[j]=0;
}
}
}

void main()
{
for (i=0;i<100;i++)
str2=new char[100];
i=0;
while (cin.getline(str2,110,'\n'))
{
count++;
if (strlen(str2)>max)
max=strlen(str2);
Rem_Tab(str2);
i++;
}
for (i=0;i<max;i++)
{
for (j=count-1;j>=0;j--)
{
if (i<strlen(str2[j]))
cout<<str2[j];
else
cout<<" ";
}
cout<<endl;
}
cout.flush();
}[/cpp]

Posted: Mon Jun 24, 2002 11:57 pm
by mido
Looks like I just had to extend the arrays somewhat.. oh well :)

490: Why WA

Posted: Tue Aug 06, 2002 2:29 am
by razibcse
490: I always get WA...why this happens?Please tell me....Language:C


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

void main()
{
char w[110][110],ww[110][110];
long t,x,a,b,c,d,i[110],k;
a=0;
while(gets(w[a]))
{
t=0;
for(x=0;x<strlen(w[a]);x++)
if(w[a][x]!=' ')
{
t++;
ww[a][t]=w[a][x];
}
i[a]=t;
a++;
}

b=a;
k=0;
for(a=0;a<b;a++)
if(i[a]>k)
k=i[a];

for(d=0;d<=k;d++)
{
for(c=(b-1);c>=0;c--)
printf("%c",ww[c][d]);

printf("\n");
}

}

490 why WA

Posted: Tue Aug 27, 2002 7:53 pm
by ttmoptic
I cannot see why I am getting WA. Language is c++

Code: Select all

#include <iostream>
#include<string>
#include <vector>
using namespace std;

void main(void)

{
        char ch;
        vector<string> sent;
        for(cin.get(ch);cin;cin.get(ch))
        {
            string word;
            word+=ch;
            for(cin.get(ch);ch!='\n';cin.get(ch))
                    word+=ch;
            sent.push_back(word);
        }
        int max=0;
        for(int i=0;i<sent.size();i++)
                if(sent[i].length()>max)
                        max=sent[i].length();
        for(int i=0;i<sent.size();i++)
                if(sent[i].length()<max)
                        sent[i].resize(max);
        for(int i=0;i<max;i++)
        {
                for(int j=sent.size()-1;j>=0;j--)
                                cout << sent[j][i];
                cout << endl;
        }
}

Posted: Wed Aug 28, 2002 1:24 am
by arc16
you have small mistake. try this:

Code: Select all

input:
a
bb
ccc
dddd
eeeee
ffff
ggg

your output:
gfedcb
gfedc
 fed
  e

correct output:
gfedcba
gfedcb
gfedc
 fed
  e

Posted: Fri Mar 28, 2003 1:48 pm
by Hisoka

Code: Select all

input:
aaaaaa
bbbbbb
ccccccc
output:
cba
cba
cba
cba
cba
cba
and it doesn't NULL char. and only space char in back of the input.
like this:
b=space char.
akuborangbkerenbbbbbbbbbbbbbbbbbb... :wink:

Posted: Sat Mar 29, 2003 10:15 am
by little joey
Well, I don't know why, but your program can't handle input like this:

Code: Select all

1234

12
1234567890
1...56..9
...4.....0
123

12345
It should give:

Code: Select all

1.1.111.1
2.2..22.2
3.3..3..3
4..4.4..4
5...55...
....66...
.....7...
.....8...
....99...
...0.0...
Here spaces are replaced by dots, for readability.
Your program prints some things and then goes into an endles loop...

Hope it helps.

Posted: Sat Mar 29, 2003 12:01 pm
by Hisoka
I think you don't understand the problem. input can more than 2 sentence. And your program only can store 2 input. And your algo program is wrong.

Posted: Mon Mar 31, 2003 8:42 am
by Dominik Michniewski
little joey, are you seru that you should print spaces at end of line? When I print it I got WA ...

Dominik MIchniewski