1

This is the code.Check Line 5 in the code below.

#include<stdio.h>
float occurence(char array[],char searched);
int main(){
    /*char new[] = "NITTE"; */
    char new[100];
    char *point = & new[0];
    char query;
    printf("String:\t");
    scanf("%s",point);  //THIS CAUSES ERROR BUG HERE!!!.ALONG WITH "char new[100]" IN THE LINE ABOVE.
    /*printf("Search:\t");*/
    scanf("%c",&query);
    printf("Search:\t");    
    scanf("%c",&query);
    printf("String =\"%s\"\nQuery =\"%c\" ",point,query);

    //printf("%c",*(point+1));
    for(int i = 0 ; *(point+i)!='\0';i++ ){
        if(*(point+i)==query){
            static char * add;
            add=&new[i];
            printf("Position:%d\nAddress:%p \n", i+1,add);
        }
    }
    printf("Occurance in String = %f %%\n",occurence(new,query));
    return 0; 
}
float occurence(char array[],char searched){
    float appearance = 0,occurance=0;int i=0;
        for(i=0;array[i]!='\0';i++){
            if(array[i]==searched){
                appearance++;
            }
        }
    float length = i;
    appearance=appearance+0.0;length=length+0.0;//THIS IS THE ONLY WAY I COULD THINK OF CONVERTING INT TO FLOAT SINCE
    //                                          APPEARENCE WOULD RETURN 2 AND 2.O AND SAME GOES FOR THE LENGTH.
    occurance = (appearance/length)*100;
    return occurance;
}

HERE two scanf s are needed but it has the same definitions. Explain with details No short answer please.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
Neo
  • 27
  • 3
  • On the 8th line, you don't need to use an ampersand with %c specifier. And you must use an ampersand with %s. – Rohan Bari May 12 '20 at 14:05
  • @Jabberwocky DIY. You'll get to know the differences between using %c and %s with/without ampersand in scanf(). – Rohan Bari May 12 '20 at 14:06
  • Also `char *point = & new[0]` is just a od way to write `char *point = new` – Jabberwocky May 12 '20 at 14:06
  • But why? @LinuXMan – Neo May 12 '20 at 14:06
  • 2
    @LinuXMan - I suggest you do it yourself. You got it backwards – StoryTeller - Unslander Monica May 12 '20 at 14:07
  • 2
    `scanf("%c",&query);` will read the newline or other whitespace that terminated `scanf("%s",point);` but it would not need to be twice if you add a space thus: `scanf(" %c",&query);` before the `%`. That is not clear because of a line break so i will type it again: `scanf(" %c",&query);` – Weather Vane May 12 '20 at 14:07
  • so you mean to say you don't need & when using %c in scanf? @LinuXMan – Neo May 12 '20 at 14:08
  • 1
    "/THIS CAUSES ERROR BUG HERE!!!.ALONG WITH "char new[100]" IN THE LINE ABOVE." - *what* is the error? Is it a compiler error, a runtime error, what? – John Bode May 12 '20 at 14:08
  • 1
    You must not use it for `%s` but you do need it for `%c`. – Weather Vane May 12 '20 at 14:08
  • 3
    @LinuXMan it's the other way round – Jabberwocky May 12 '20 at 14:09
  • How is it pointless? @Jabberwocky – Neo May 12 '20 at 14:10
  • @Learner I removed the first comment which was not very clear. It's pretty pointless to have `scanf("%c",&query);` twice. – Jabberwocky May 12 '20 at 14:11
  • 1
    Plus one thing, what error are you getting and what's your desired output? That's not clear to give a satisfy answer. Please edit your post. – Rohan Bari May 12 '20 at 14:12
  • If I Comment the one scanf.It doesn't accept any values after entering the values for String: – Neo May 12 '20 at 14:12
  • @Learner pease read my comment which answers your question. `scanf` leaves a newline char in the buffer. – Weather Vane May 12 '20 at 14:13
  • Wow you are right. @WeatherVane Thanks – Neo May 12 '20 at 14:16
  • But Why did that happen? why type scanf(" %c",&query) instead of scanf("%c",&query) – Neo May 12 '20 at 14:17
  • 1
    The format specifiers `%d` and `%s` and `%f` automatically filter leading whitespace, but `%c` and `%[...]` and `%n` do not. But you can instruct `scanf` to do so by adding a space just before the `%`. The `scanf` conversion stops at the first character it cannot convert, which is typically (but not necessarily) a space or a newline, and that character remains in the input buffer. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane May 12 '20 at 14:19

1 Answers1

1

in your first scanf() at this line

    /*printf("Search:\t");*/   scanf("%c",&query);

cathes newline char \n, and again your second scanf() at this line

    printf("Search:\t");    
    scanf("%c",&query);

catches your input.

You should leave a space for get rid of first scanf() like this

    scanf(" %c",&query);

thanks to the white space, it skips newline char \n and gets correct input.

Muhammedogz
  • 774
  • 8
  • 21