490 - Rotating Sentences
Moderator: Board moderators
-
- Learning poster
- Posts: 68
- Joined: Fri Oct 26, 2001 2:00 am
- Location: Dhaka, Bangladesh
- Contact:
problem 490 - rotating sentences
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
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
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
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
Ivor
490 runtime error
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]
[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]
490: Why WA
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");
}
}
#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
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;
}
}
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
Code: Select all
input:
aaaaaa
bbbbbb
ccccccc
output:
cba
cba
cba
cba
cba
cba
like this:
b=space char.
akuborangbkerenbbbbbbbbbbbbbbbbbb...
![:wink:](./images/smilies/icon_wink.gif)
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm
Well, I don't know why, but your program can't handle input like this:
It should give:
Here spaces are replaced by dots, for readability.
Your program prints some things and then goes into an endles loop...
Hope it helps.
Code: Select all
1234
12
1234567890
1...56..9
...4.....0
123
12345
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...
Your program prints some things and then goes into an endles loop...
Hope it helps.
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
little joey, are you seru that you should print spaces at end of line? When I print it I got WA ...
Dominik MIchniewski
Dominik MIchniewski
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Born from ashes - restarting counter of problems (800+ solved problems)