-2

why the code doesn't work?? anything wrong with that loop?? if then what should be answer? and why it can't be. Please make me clear. :)

#include<stdio.h>
#include<string.h>

int main()
{
    char s[1000];
    int i,j=1,x,y; char k,l;

    gets(s);
    l = strlen(s);
    scanf("%c",&k);
    for(s[i]=0; s[i]<l; i++)
    {
        if(s[i]=='k')
            j++;
    }
    printf("\n%c is %d time(s) in string",k,j);


    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

4 Answers4

0

please check this way. you are matching letter 'k' instead of variable.

if(s[i]==k)
nithinTa
  • 1,632
  • 2
  • 16
  • 32
0

First use l = strlen(s)+1; instead of l = strlen(s);. Then change s[i]=0 in for loop to i = 0; and use i<l instead of s[i]<l.

Also, change if(s[i]=='k') to if(s[i]==k).

Full example:

#include<stdio.h>
#include<string.h>

int main()
{
    char s[1000];
    int i,j=0,l; 
    char k;

    gets(s);
    l = strlen(s)+1;
    scanf("%c",&k);

    for(i=0; i<l; i++)
    {
        if(s[i]== k)
            j++;
    }
    printf("\n%c is %d time(s) in string",k,j);
    return 0;
}
msc
  • 33,420
  • 29
  • 119
  • 214
  • @rsp thanx a lot. it works!!! would you please explain me "l = strlen(s)+1;" this statement? – Saturninus Appius Oct 12 '17 at 09:11
  • @Saturninus Appius The strlen() function calculates the length of the string s, excluding the terminating null byte. So, we need 1 byte more for null terminator. – msc Oct 12 '17 at 09:35
  • Never, never, never use `gets` or provide answers containing `gets`. It is so insecure it has been completely removed from the C11 standard. It should not be used, and should never be used as an example of proper coding technique on StackOverflow. Perhaps `fgets` *with validation of the return*? – David C. Rankin Oct 13 '17 at 18:03
  • @David C. Rankin I know, but OP used gets in program. I did correction only. – msc Oct 13 '17 at 18:14
  • Well, you did a partial correction, otherwise you would have removed `gets` and explained why its use is highly improper. I know how the copy/paste occurred. I am simply trying to help you understand that when answering questions here, you step into the roll of teacher. And, in that regard, you should correct and explain why the use of `gets` is completely and utterly improper in 2017. If you had, I could upvote your question in good conscious rather than needing to comment on the answer containing a function that has been completely removed from the current C standard.. – David C. Rankin Oct 13 '17 at 19:18
0

The datatype of the variable l is char.It should be declared of the type int. You have initialised j with value 1 when it should have been initialised with 0 . The for loop is incorrect. Instead of s[i] use i and check the condition i < l . And finally in the if condition replace 'k' by k I hope this will help you get desired result

0

I can see you made many mistakes, I will list them up:

  • if(c[i] == 'k') should be if(c[i] == k)
  • You should use fgets instead of gets, gets is not stable or safe
  • Please do use MACRO for 1000 in char s[1000]
  • for(s[i]=0; s[i] < l; i++) is wrong because you should be iterating with i so it should be for( i = 0; i < length; ++i)

The code example:

#include <stdio.h>     /* printf */
#include <stdlib.h>    /* fgets  */
#include <string.h>    /* strlen */

#define MAX_LINE_LENGTH  1000

int main()
{
    char k;
    char line[MAX_LINE_LENGTH];
    size_t count, length, i;

    /* Reading a line/string */
    printf("Please enter a line : ");
    if (!fgets (line, MAX_LINE_LENGTH, stdin)) 
    { 
           /* handle error */ 
           printf("Failed to read input\n");
           return 1;
    }

    /* calculating the length */
    length = strlen(line) + 1;

    /* Reading the letter to count its occurences */
    printf("Please enter a letter : ");
    scanf("%c",&k);

    /*
     * Counting the occurences of k in a string/line
     */
    count = 0;
    for(i = 0; i < length; ++i)
    {
        if(line[i] == k)
        {
            ++count;
        }
    }

    printf("%c is %d time(s) in string\n", k, count);

    return 0;
}
  • What happens if the user cancels input (e.g. `ctrl+d`) for `line` or `k`? Far better to `if (!fgets (line, MAX_LINE_LENGTH, stdin)) { /* handle error */ }` (and likewise for ALL user input. – David C. Rankin Oct 12 '17 at 16:49
  • @DavidC.Rankin Ofcurse we should check the input, but I always assume that such acts are the user problem, other engineers think it's better to be defensive and I agree, but for such a small task this is good enough. – George zaher Oct 13 '17 at 17:03
  • Never overestimate the level of understanding of those you are helping here. When you are answering on SO, you step into the roll of teacher. The goal should always be a robust example that insures the questioner has as complete an understanding as is necessary to avoid *Undefined Behavior*. – David C. Rankin Oct 13 '17 at 18:00
  • Thank Mr. David C. Rankin – George zaher Oct 13 '17 at 18:07