how to find length of string between two special charaters ?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello,

I have fx3s board, I want to find the length of a string between two special characters ( '$' and '#')  in my program,

will u please help me how to find this ?

regards,

Ajith.

0 Likes
1 Solution
abhinavg_21
Moderator
Moderator
Moderator
50 likes received 25 likes received 10 likes received

Hi Ajith,

I assume that the string is stored in a char array and  '$'  comes first while traversing array from left to right.

Here is the simple C code:

void main()

{

char string[50];

int i, length = 0;

printf("Enter a string \n");

gets(string);

for(i=0; string[i] != '$'; i++);

/*  keep going through each character of the string till '#' encounters*/

for (++i; string[i] != '#'; i++)

{

       length++;

}

printf("The length of a string between the special characters is %d \n", length);

}

Thanks & Regards

Abhinav

View solution in original post

0 Likes
2 Replies
abhinavg_21
Moderator
Moderator
Moderator
50 likes received 25 likes received 10 likes received

Hi Ajith,

I assume that the string is stored in a char array and  '$'  comes first while traversing array from left to right.

Here is the simple C code:

void main()

{

char string[50];

int i, length = 0;

printf("Enter a string \n");

gets(string);

for(i=0; string[i] != '$'; i++);

/*  keep going through each character of the string till '#' encounters*/

for (++i; string[i] != '#'; i++)

{

       length++;

}

printf("The length of a string between the special characters is %d \n", length);

}

Thanks & Regards

Abhinav

0 Likes
Anonymous
Not applicable

thank you Abhinav.

0 Likes