Print Position of Char

How to print the position of the char in a given word
I have a input like:
Column_name:
Indicator
YNNYYN
NNNYYN
YYYYYN
NNNNNN
REQUIRED O/P:
Where the Y is present in a given string, i want position of the string with CONCATENATE of P Letter.
o/p:
P1
P4
P5
P4
P5
P1
P2
P3
P4
P5

Showing Answers 1 - 5 of 5 Answers

Sandeep

  • Mar 27th, 2018
 

>Use for loop in reformat,get positions for Y using string_substring (compares the character Y and returns that particular position) and append in a vector type variable.
>then use NORMALIZE

  Was this answer useful?  Yes

Nitin

  • Apr 6th, 2018
 

out :: length(in) =
begin
out :: length_of(string_split(in.data,Y))-1;
end;
temp:: normalize(temp,in, index) =
begin
temp.pdata:: string_concat("P",(string(""))(decimal(""))string_index(in.data, "Y" , temp.off ));
temp.off:: string_index(in.data, "Y" , temp.off );
end;
type temporary_type=record
decimal("") off;
string("") pdata;
end; /*Temporary variable*/
temp :: initialize(in) =
begin
temp.off::0;
temp.pdata:: "";
end;
out :: finalize(temp, in) =
begin
out.position :: temp.pdata;
end;

  Was this answer useful?  Yes

neha.nagare

  • Apr 17th, 2018
 

we can also achieve this by using only normalize component in template mode by calculating number of "Y" in each string and asigned it as a length in length function and at the same time capture the position of the character "Y" in a vector and assigned the index of the vector in the normalize function .
Please find below code :
let string("")str=""; // global variable to read each character in string
let decimal("")[] position=allocate_with_defaults(); // To collect the position of the character "Y" in a string
out::length(in)=
begin
let decimal("") cnt=1; // local variable to increament loop count
let decimal("")cnt_Y=0; // local variable to count number of "Y" in a string ,so that can e assigned as a length for each record
position=allocate_with_defaults();
while(cnt != (string_length(in.name))+1) // adding 1 because cnt starts from 1
begin
str=string_substring(in.name,cnt,1)
if(str==Y)
begin
cnt_Y=cnt_Y+1;
position=vector_append(position,cnt); // capturing position of Y in string in this vector
end
cnt=cnt+1;
end
out::cnt_Y;
end
out::normalize(in,index)=
begin
out.name :: string_concate("P",position[index]);
end

  Was this answer useful?  Yes

PJ

  • Apr 17th, 2018
 

How come you got
P5 for 3rd row
P4 for 4th row and so on?

  Was this answer useful?  Yes

mohan

  • Aug 31st, 2018
 

it will work with simple string function :
string_index() , check this function

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions