4

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.

mclc
  • 65
  • 10
ranaarjun
  • 245
  • 3
  • 5
  • 12

4 Answers4

6

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",'\\')
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Bro this string is entered by the user at the run time. Suppose he/she entered "asdfgh\hj" . then How can i change it to "asdfgh\\hj". @dasblinkenlight – ranaarjun Feb 06 '14 at 19:43
  • 1
    @ranaarjun If a string is entered by the user, you do not need to change anything. The slash is needed to be escaped only for the compiler. So if you call `strchr(userString, '\\')` you'll get a pointer to the correct position. – Sergey Kalinichenko Feb 06 '14 at 19:44
  • 1
    @ranaarjun You said the compiler throws an error. If the string is input by the user, then the error cannot possibly be related to escaping. Therefore, not only is your original post deceiving, but you need to show us the actual error you are getting. –  Feb 06 '14 at 19:45
  • When the string entered by the user is containing a backslash it's automatically stored as `'\\'` – rullof Feb 06 '14 at 19:45
  • Bro I said "I tried strchr() function as strchr("asdfgh\hj",'\') but compiler throws an error." Compiler throws an error at compile time. @remyabel – ranaarjun Feb 06 '14 at 19:48
  • 1
    @rullof No, the backslash is not stored as two slashes, it is represented as two slashes in string and char literals. Compiler turns two slashes into one, and keeps it that way in the executable binary code. – Sergey Kalinichenko Feb 06 '14 at 19:48
  • @dasblinkenlight That's exactly what i means. In other way it will be stored as `092` in the ASCII representation. – rullof Feb 06 '14 at 19:51
  • @dasblinkenlight: `'\\'` is one backslash. “'\\'” is a string containing two backslashes which is a C character constant that is the integer value of one backslash. rullof is correct to say that a backslash entered by the user is `'\\'`; if `c` is a `char` containing the character, then `c == '\\'` is true. – Eric Postpischil Feb 06 '14 at 20:03
  • int main() { char str[100]; char *p; fgets(str,100,stdin); p = strchr(str,'\\'); str[p-str+1] = 'a'; fputs(str,stdout); return 0; } Bro I entered ar\ . Now strchr() works fine and it gives me the index 3. now i wish to change the original string and at the position '3' , i write 'a' but instead of replacement of '\' by 'a'. the string looks like ar\a . How I replaced '\' by 'a'?? @dasblinkenlight – ranaarjun Feb 06 '14 at 20:16
  • @ranaarjun Your pointer math is off by one - it's `str[p-str] = 'a'`, which is a rather roundabout way of writing an equivalent `*p = 'a'`. – Sergey Kalinichenko Feb 06 '14 at 20:24
  • @ranaarjun For understanding suppose you're looking for ``'`` instead of a backslash. Then you use `strchr(mystring, '\'')` The backslash is used just by the compiler (not the runtime) so that you can say what character you mean – Brandin Feb 06 '14 at 20:32
1

Try this:

strchr("asdfgh\\hj",'\\')
Agis
  • 32,639
  • 3
  • 73
  • 81
1

C standard says, C11 6.4.4.4:

The double-quote " and question-mark ? are representable either by themselves or by the escape sequences \" and \?, respectively, but the single-quote ' and the backslash \ shall be represented, respectively, by the escape sequences \' and \\.

So use

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

instead.

haccks
  • 104,019
  • 25
  • 176
  • 264
1

In C the backslash is used for hard typed characters like \n. So you need to write \\ for the \ itself:

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

Note that in the string you provided the \ also need to be writen \\ otherwise it will be considered as \h which has no meaning.

rullof
  • 7,124
  • 6
  • 27
  • 36