Click here to Skip to main content
16,018,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a program to print the word in the correct order. 

The input will be an array of strings like ["P>E", "E>R", "R>U"]. Each string will always have 3 characters in length, the middle character will always be '>'. For P>E, it means that P should come ahead of E in the resulting string. Likewise E>R means E should come before R in the resultant string. The resultant string For ["P>E", "E>R", "R>U"] is : PERU

The resultant string for : ["I>N", "A>I", "P>A", "S>P"] is SPAIN

There will be no repetition of characters in the resultant string. (*Please note that your program has to work for all possible input strings and not only the above example)


What I have tried:

Please help me out as soon as possible!!!
Posted
Updated 4-May-21 15:29pm

I won't repeat Richard's link - though I would have suggested it as well - but I'll add some specifics to help you think about the task.

Start with "how would I do it manually?" and work out exactly what it requires.
I'd look at writing down a list of the letters as they are met, and adding in the "right place" as needed
I>N
I N
A>I
A I N
P>A
P A I N
S>P
S P A I N

That way, if the letter groups come in a different order, it still works:
A>I
A I
P>A
P A I
S>P
S P A I
I>N
S P A I N
Then all you need to do is work out how to "separate" the letters, and compare them to find where in the "result string" they should go.

Give it a try, this isn't as complicated as it looks at first sight!
 
Share this answer
 
Comments
Jagadish Prasad Dash 4-May-21 10:03am    
I tried out sorting the alphabets but it didn't work out. then I went with giving specific inputs but it didn't work for every input. so can you please help me with the algorithms?
OriginalGriff 4-May-21 10:22am    
"it didn't work out"
What does that mean?
You code didn't compile?
It did something you didn't expect, or didn't do something you did?
The first time you ran it your computer exploded and set fire to the goldfish?


Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Quote:
Please help me out as soon as possible!!!
In what way exactly? If you are expecting someone here to write your assignment for you then I am afraid you will be disappointed. You could start by reading How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Quote:
How do I sort alphabets as required in the following question?

'sort' is not the correct word, it is rather a reordering, aka changing the order of strings. This imply that you can't use the sort function or use a usual sort algorithm.
You need to create an algorithm which on the end will find the solution to the problem.

Train yourself by solving the problem by hand, repeatedly, and create new datasets.
Answer those questions:
- How to find what is the first letter of solution ?
- How to find the next letter ?
- How do you know that a letter is the last one ?
The answers to those questions are basically your algorithm.

Give it a try.
If you are stuck, show your work and ask specific questions.

[Update]
Not the solution, but
Since "Each string will always have 3 characters in length":
C++
// replace
char s[100][100]={"p>e","e>r","r>u"};
char s1[100];
// with
char s[100][4]={"p>e","e>r","r>u"};
char s1[100];

Since alphabet is only 26 letters,
C++
// replace
char s[100][4]={"p>e","e>r","r>u"};
char s1[100];
// with
char s[26][4]={"p>e","e>r","r>u"};
char s1[26];

As far as I can see, your loop is just wrong, there is no logic related to the problem.
Have a look at Linked list - Wikipedia[^] and pay attention to doubly linked list.

To see what us going on in your code,
There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v3
Comments
Jagadish Prasad Dash 4-May-21 22:10pm    
#include <stdio.h>
#include <string.h>

int main ()
{
char s[100][100]={"p>e","e>r","r>u"};
char s1[100];
int i,j,m=0,n;
n=strlen(s);
int row = sizeof(s) / sizeof(s[0]);
int column = sizeof(s[0])/row;
for (i = 0,j=0; s[i][j] !=0; i++,j++) {
if(i==2){
s1[m++]=s1[i]+s[i][0];
s1[m++]=s[i][2];
}
else
{
s1[m++]=s1[i]+s[i][0];
}
}
printf("The sorted string is : %s", s1);
return 0;
}



this is what I tried to get. but still doesn't work. Help me out.
Patrice T 5-May-21 0:13am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
I>N
I N


A>I
A I N


P>A
P A I N


S>P
S P A I N
 
Share this answer
 
Comments
Jagadish Prasad Dash 4-May-21 22:12pm    
#include <stdio.h>
#include <string.h>

int main ()
{
char s[100][100]={"p>e","e>r","r>u"};
char s1[100];
int i,j,m=0,n;
n=strlen(s);
int row = sizeof(s) / sizeof(s[0]);
int column = sizeof(s[0])/row;
for (i = 0,j=0; s[i][j] !=0; i++,j++) {
if(i==2){
s1[m++]=s1[i]+s[i][0];
s1[m++]=s[i][2];
}
else
{
s1[m++]=s1[i]+s[i][0];
}
}
printf("The sorted string is : %s", s1);
return 0;
}

This doesn't work.
how can I take conditional strings as input?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900