Thread: Check certain character is ina string or not

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    12

    Check certain character is ina string or not

    I want to check whether a certain character is in a string or not
    but my code is not working


    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
      {
      char ch[200];
      char c;
      int n,i;
      puts("Enter string");
      scanf("%s",ch);
      puts("ENTER Character");
      scanf("%c",&c);
      for(i=0;ch[i]!='\0';i++)
         {
         if(ch[i]==c);
         puts("found");
         }
    
      return 0;
      }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if(ch[i]==c);
    Watch the ; at the end of this line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    12
    Now it is running without entering a character

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Try putting a space before the %c in the second scanf(), this will tell scanf() to skip leading white space characters.


    Jim

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    12
    Well know it is working but could you elaborate your answer,I am not understanding it perfectly

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Salem and jimblumberg have addressed critical problems in your code in their posts #2 and #4 respectively, but to add:
    • You should not use "%s" as the format specifier when reading a string with scanf. The reason is that this allows the user to enter a longer string than can be stored in the corresponding array. Rather, since the array has 200 chars, you should set a field width one less than this (one element is reserved for the null character), i.e., "%199s".
    • Unless you are required to write the loop, or it is otherwise convenient/efficient to use your own loop, you should make use of existing library components, e.g., use strchr from <string.h>.

    As for why jimblumberg's suggestion works: when you enter the string, the newline left from entering the input is left in the input buffer. This is then read by scanf with %c. Adding a leading space consumes that newline instead, hence allowing you to read the desired character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    12
    still not getting it

  8. #8

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to check if a string has at least one character.
    By johnjrgs in forum C Programming
    Replies: 7
    Last Post: 02-20-2013, 11:48 PM
  2. Check to check string for a series of characters
    By robi in forum C Programming
    Replies: 1
    Last Post: 02-28-2012, 05:42 PM
  3. Help! with character validity check
    By Tdankert in forum C Programming
    Replies: 19
    Last Post: 07-30-2007, 08:41 PM
  4. Check if next character is a letter?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2002, 06:02 PM
  5. Replies: 12
    Last Post: 01-12-2002, 09:57 AM

Tags for this Thread