Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C how do I find the '\' character in a string?

Tags:

c

string

strchr

Suppose I have a string entered by the user asdfgh\hj, and I wish to find out the index of \ character in a String. How I can do it in C?

I tried strchr() function as strchr("asdfgh\hj",'\') but compiler throws an error.

Then I used == operator but same problem with it — again the compiler throws an error.

like image 241
ranaarjun Avatar asked Jan 11 '23 06:01

ranaarjun


2 Answers

I tried strchr() function as strchr("asdfgh\hj",'\') but compiler throws an error

That's the right function! The reason you get an error is because \ is a special "escape" character. It is used to define "special" non-printable characters, such as newline \n. That is why the backslash itself \ needs escaping, like this:

strchr("asdfgh\\hj",'\\')
like image 106
Sergey Kalinichenko Avatar answered Jan 13 '23 18:01

Sergey Kalinichenko


Try this:

strchr("asdfgh\\hj",'\\')
like image 43
Agis Avatar answered Jan 13 '23 20:01

Agis