# 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 - 10 of 10 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

#### 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;

#### neha.nagare Profile Answers by 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

#### PJ

• Apr 17th, 2018

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

#### mohan

• Aug 31st, 2018

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

#### Sagar

• Jan 10th, 2020

we will use 2 components
1 reformat :- in transform we will use string filter to remove to N from each line and place the string filter in String length which will give the number of Y in a line store in another column(Count_of_y) for each row with data
2. Normalize :- now count of y will become the length and in normalize we will use string index for each time with index and pass the result to output

#### Shikha

• Jun 23rd, 2020

Use Reformat
out :: string_concat("P",(decimal(""))(string_index(in.data,"Y")))

#### Moreshwar Randhe

• Jan 13th, 2021

1.use Reformat
Out.data :: vector_search_all(re_split_no_empty(in.data,"s?"),"Y")
You will get vector of y positions,
2.use normalize
In length function give length of input vector
And then in normalize function use string_concat with y positions using index and character P.

#### RR

• Jan 22nd, 2021

How do you define the output format of Reformat here?

#### shivank b

• Apr 4th, 2021

The simplest solution would be using reformat with below function.
out.data :: string_concat("P",(decimal(1))string_index(in.column_name,"Y"))
Regards,
Shivank B