Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for example if string is apple and the character p should be searched in string. Then the output should be 2 since two p is there
But i am getting 0 as the output

What I have tried:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    char str[101];
    scanf("%s",str);
    char ch;
    scanf("%c",&ch);
    int count=0;
    for(int i=0;i < strlen(str);i++){
        if(str[i]==ch){
            count+=1;
        }
    }
    printf("%d",count);

}
Posted
Updated 16-Mar-22 10:46am

Did you forgot a & ?
C++
scanf("%s",&str);


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Greg Utas 16-Mar-22 16:47pm    
5. Sadly, I don't think the debugging part will be needed after the & is added.
Patrice T 16-Mar-22 19:04pm    
Thank you.
In this case, the debugger is a second approach which would have helped the OP to find the problem by himself.
I have always found the debugger a great help to clean code.
Greg Utas 16-Mar-22 19:10pm    
But judging from the other answers, the & may not be needed, probably because str is an array and decays to a pointer. I've only written C++ and have never used scanf, so I'm unfamiliar with its vagaries.
Patrice T 16-Mar-22 19:19pm    
That is where the debugger comes into play, it always shows where code goes wrong. :)
Greg Utas 16-Mar-22 19:28pm    
LOL
Be careful with scanf(). In particular scanf("%s", str) reads from the standard input up to the first white-space character, which is not removed from the input string. The next time you call scanf(), you ask for the next character in the input, which is likely to be the newline char you entered when you typed in "apple". So your program is now counting the number of newline ('\n') characters in the input string. You can fix this by changing the scanf() call to scanf("%s\n", str);.
 
Share this answer
 
Simple. This line:
scanf("%s",str);
Reads up to the first whitespace, not the end of the line.
So the second scanf returns the newline character which isn't in the target line.
Instead, use fgets[^]

But ... it should have been fairly obvious that something was wrong with the input when you typed "apple" and pressed ENTER and immediately got a result at all ... Look at all the symptom of what happens, and it's easier to work out what is wrong!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900