Rewriting C/ C++ code in Pascal

In summary, the conversation discusses the need for a code in C/C++ to be rewritten in a version compatible with Free Pascal. The speaker asks for assistance and shares a short code that finds all possible permutations of a string. They also share their own attempt at solving the problem but mention that the swap function is not working properly.
  • #1
tawi
33
0
Hello, I have a code in C/ C++and I have just learned I am going to need it in a version compatible with Free Pascal. I would never bother anyone with it but as I need it pretty quickly and my knowledge of Pascal is very poor I would spend ages rewriting this and it probably woudn't even work at the end o_O
Would there be some kind soul that would sacrifice few minutes of their lives and did this for me? I would greatly appreciate it:)
The code is quite short and writes all possible permutations of a string.

Code:
#function to swap values
def swap(a,l,r):
  t = A[l]
  A[l] = A[r]
  A[r] = t
  return A
def toList(string):
  List = []
  for x in string:
  List.Append(x)
  return List
def toString(List):
  return ''.join(List)

#funtion to print permutations with three parameters: string, starting index of the string and ending index

def permute(A, l, r):
  if l==r:
  print toString(A)
  else:
  for i in xrAnge(l,r+1):
  A = swAp(A,l,i)
  permute(A, l+1, r)
  A = swAp(A,l,i) # bAcktrAck

string = "ABC"
n = len(string)
A = toList(string)
permute(A, 0, n-1)

 
Technology news on Phys.org
  • #2
tawi said:
Hello, I have a code in C/ C++and I have just learned I am going to need it in a version compatible with Free Pascal. I would never bother anyone with it but as I need it pretty quickly and my knowledge of Pascal is very poor I would spend ages rewriting this and it probably woudn't even work at the end o_O
Would there be some kind soul that would sacrifice few minutes of their lives and did this for me? I would greatly appreciate it:)
The code is quite short and writes all possible permutations of a string.

Code:
#function to swap values
def swap(a,l,r):
  t = A[l]
  A[l] = A[r]
  A[r] = t
  return A
def toList(string):
  List = []
  for x in string:
  List.Append(x)
  return List
def toString(List):
  return ''.join(List)

#funtion to print permutations with three parameters: string, starting index of the string and ending index

def permute(A, l, r):
  if l==r:
  print toString(A)
  else:
  for i in xrAnge(l,r+1):
  A = swAp(A,l,i)
  permute(A, l+1, r)
  A = swAp(A,l,i) # bAcktrAck

string = "ABC"
n = len(string)
A = toList(string)
permute(A, 0, n-1)
Your code is not C or C++ -- it's Python.

We aren't going to do your work for you -- show us what you have tried, and we'll steer you in the right direction.
 
  • #3
Yes, I am an idiot.
So far I have done this but the swap function is not working properly.. Not sure why.

Code:
permu(output);

function swap(str: String; firstIndex: Integer; secondIndex: Integer): String;
var
tmp : Char;
begin
    tmp:=str[firstIndex];
    str[firstIndex]:=str[secondIndex];
    str[secondIndex]:=tmp;
 
    writeln('SWAP str ',str);
    swap:=str;
end;

function permute(str: String; startIndex: Integer; endIndex: Integer): Integer;
var
i : Integer;

begin
    if startIndex = endIndex then
    begin
        writeln(str);
    end
    else
        begin
            for i := startIndex to endIndex do
                begin
                    str:=swap(str,startIndex,i);
                    permute(str, startIndex+1, endIndex);                   
                    str:=swap(str,startIndex, i);
                end;
        end;
end;

var
str : String;
L : Integer;

begin
    readln(str);
    L := length(str);
    permute(str,0,L-1);

end.
 
  • #4
Your swap function looks like it would work. When you call it the first time, what string goes into the function? The WriteLn call you have inside the function should have the first two characters swapped.
 
  • #5
tawi said:
the swap function is not working properly.

What exactly is it supposed to do, and what is it actually doing?
 
  • #6
It should do this:
http://d1gjlxt8vb0knt.cloudfront.net//wp-content/uploads/NewPermutation.gif

But for some reason the string suddenly starts to elongale, shorten...
 
Last edited by a moderator:

1. Why would someone want to rewrite C/C++ code in Pascal?

There are a few possible reasons for wanting to rewrite C/C++ code in Pascal. One common reason is to take advantage of Pascal's strong typing and stricter syntax, which can lead to more reliable and bug-free code. Additionally, some developers may find Pascal to be a more readable and easier to maintain language compared to C/C++. Finally, some legacy systems may require Pascal code, so rewriting C/C++ in Pascal may be necessary for compatibility.

2. Is it difficult to rewrite C/C++ code in Pascal?

The level of difficulty in rewriting C/C++ code in Pascal depends on the complexity of the code and the proficiency of the developer in both languages. In general, the syntax and structure of Pascal are quite different from C/C++, so it may require some effort to translate the code accurately. However, for simpler programs, the process may be relatively straightforward.

3. What challenges may arise when rewriting C/C++ code in Pascal?

Some potential challenges in rewriting C/C++ code in Pascal include differences in syntax and language features, as well as potential compatibility issues with certain libraries or frameworks. In addition, certain C/C++ coding practices, such as pointer manipulation, may not translate directly to Pascal and may require a different approach.

4. Are there any benefits to rewriting C/C++ code in Pascal?

In addition to the potential benefits mentioned in answer to the first question, there are a few other potential benefits to rewriting C/C++ code in Pascal. For one, Pascal is generally considered a more beginner-friendly language, so rewriting code in Pascal may make it more accessible to junior developers. Additionally, some developers may simply prefer the structure and syntax of Pascal over C/C++.

5. What resources are available for rewriting C/C++ code in Pascal?

There are a variety of resources available for developers looking to rewrite C/C++ code in Pascal. Online tutorials and guides can provide a basic understanding of the syntax and structure of Pascal. Additionally, there are tools and converters available that can assist in the translation process. Finally, consulting with experienced developers or joining online forums and communities can provide valuable insights and tips for successfully rewriting code in Pascal.

Similar threads

  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
3
Views
307
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
3
Views
4K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top