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

Suppose I have a string entered by the user asdfgh\hjand I want to find out the index of the character \in String. How can I do this in C?

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

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

+4
source share
4 answers

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

! , , , \ "escape". "" , newline \n. \ , :

strchr("asdfgh\\hj",'\\')
+6

:

strchr("asdfgh\\hj",'\\')
+1

C , C11 6.4.4.4:

" ? , escape- \" \?, , ' \ \' \\.

strchr("asdfgh\\hj",'\\')  

.

+1

In C, backslashes are used for hard typed characters, such as \n. Therefore, you need to write \\for yourself \:

char *backslash = strch("some text containing \\ ...", '\\');

Please note that the line you provided \must also be written \\, otherwise it will be considered \hwhich does not matter.

+1
source

Source: https://habr.com/ru/post/1525677/


All Articles