Palindrome of string without using Reverse function?

Hello All,

I am trying String Manipulation programs, and I am really stuck in this where we need to write a program whether a string is a palindrome or not without using reverse.
Run a loop from starting to length/2 and check the first character to the last character of the string and second to second last one and so on …. If any character mismatches, the string wouldn’t be a palindrome —> this is the logic, however in UiPath I’m not able to write the piece of code how Subsrting or IndexOf & Length activities might be used to iterate.

Can anybody please help with this?

1 Like

Hey @BlackCurrant

You can use a ForEach and pass the String.ToCharArray in the source.

Once that is done, we need to store the ForEach index output from which we will get the current letter index.

Mame sure the typeargument in foreach will be char

So in the IF condition inside ForEach just check based on index…

str_Var(str_Var.Length-foreachOutputIndex+1).Equals(item)

Hope that helps

Thanks
#nK

1 Like

@Nithinkrishna ,

Do we have this foreachOutputIndex?

I can see other functions but not this one

1 Like

Hi,

FYI, Another solution:

The following expression returns true if yourSrting is palindrome.

yourString.Take(CInt(Math.Ceiling(yourString.Length/2))).Select(Function(c,i) c=yourString(yourString.Length-1-i)).All(Function(b) b)

Regards,

1 Like

Hey @BlackCurrant

Yes, we have this. You need to just check the properties panel of for-each.

Thanks
#nK

1 Like

@Nithinkrishna ,

You mean this?

1 Like

Yep right @BlackCurrant

Hello @Yoichi ,

What is b and i here?

yourString.Take(CInt(Math.Ceiling(yourString.Length/2))).Select(Function(c,i)

c=yourString(yourString.Length-1-i)).All(Function(b) b)

@Nithinkrishna ,

But it is throwing error

Compiler error(s) encountered processing expression “strVal(strVal.Length - ForEach(out_intA)+1).Equals(item)”.
No accessible non-generic ‘ForEach’ found.

1 Like

Screenshot of the code please ?

@Nithinkrishna,

Here it is -


1 Like

Hi,

What is b and i here?

In this case, b mean boolean type variable which is output by previous method : Select.
And i means index number of iteration of Select method.
These are anonymous function of LINQ. Please see the following document in details.

Regards,

Hey @BlackCurrant

It’s due to invalid statement, here it is …

str_Var(str_Var.Length-out_intA+1).Equals(item)

Thanks
#nK

Hello @Nithinkrishna ,

This method is not working, it’s comparing all the characters in the loop one by one.

Can you please suggest me where did I made the mistake and how to correct it?

1 Like

Hey @BlackCurrant

Sorry, That’s my mistake actually. Missed the 1/2 logic part !

Now change the for-each source value as below instead of a simple char array conversion

strVal.Take(CInt(Math.Ceiling(strVal.Length/2))).ToArray

Thanks
#nK

1 Like

@Nithinkrishna ,

Still the same

Check the output

strVal=“madam”

but in output it’s taking all the characters.

1 Like

How many logs were printed please ?

@Nithinkrishna,

3 times
image

1 Like

Which looks perfect then :slight_smile:

1/2 of the word is checked & decided !

1 Like

@Nithinkrishna

That’s is good…but the main problem is whole string should give me the output of palindrome.
Not a particular character.
Although due to the For Each loop it’s comparing all the characters in the string.

But the resultant output should be

“madam” —> is palindrome
“geek” —> not a palindrome

just like how we do it in other programming language.

1 Like