counting words containing character [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
#include <stdio.h>
int hledejznak(x)
{
int c;
int pocitadlo=0;
while((c=getchar())!=EOF)
{
if(x==c){
pocitadlo++;
while((c=getchar())!=32)
{
printf("%d\n",c);
};
};
};
return pocitadlo;
}
int main(int argc,char *argv[])
{
int znak=*argv[1];
printf("answer is %d",hledejznak(znak));
return 0;
}
Hi people, I need to count words containing character specified as argument at terminal
example: echo 'hello babe' | ./main e
Answer is 2
....because there are two words containing letter "e"
My code doesn't work, can you help me?
Thanks

Don't nest your loops; keep the outer one that processes each character read
Have a boolean variable initalized to false & set to true whenever you see the desired character.
Whenever a word ends, increment your counter if the flag is true. Either way, set the flag to false (to get ready for the next word). (Note that the last word may NOT end with a space.)
Only when you're processed all of the input should you print the value of the counter.

Related

I can't understand an error in a function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
This function make a strange error after using it several times and I really can't understand the reason behind it.
char *get_range(char *str,int min,int max){
char *_res=(char *)malloc(sizeof(str));
int cur=0;
while (min<max){
_res[cur]=str[min];
min++;
cur++;
}
return _res;
}
The problem is that after using this function several times, the output comes with additional chars and I don't understand why.
Notice: The additional chars are allway used returned by the function beffor
char *_res=(char *)malloc(sizeof(str));
is wrong. sizeof(str) is measuring the size of a char pointer. This is either 4 or 8 (typically) depending on your system (32 or 64 bit).
You need
char *_res=(char *)malloc(strlen(str) + 1);
strlen returns the number of characters in the string, and you need to add 1 for the terminating 0;
Second you have to add a terminating zero at the end, do:
_res[cur] = '\0';
before returning

Why does this loop execute only 4 times? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am beginner in c programming.I just want to know why this loop is not working properly.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char x[8];
char t;
for (i = 0; i < 8; i++) {
scanf("%c", &t);
x[i] = t;
}
return 0;
}
Because when any input given from keyboard we need to press enter to confirm completion of input. This enter stay in buffer and if next input is char or string, stores enter in string or char var and do not wait to input that char or string. In this case, first input given at execution it stores char in X[0] and enter in x[1] and so on. So executes loop 8 time but it seems to 4 time because it ask input only four times. To check that put one printf in loop
it executes 8 times.
Whenever you press enter to submit, you are entering a whitespace character that is consuming one of your loop iterations.

How to use char in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm struggling to get Char to work. It keeps returning an error.
#include <stdio.h>
#include <cs50.h>
int main (void)
{
int tower_height;
char #;
// Inputs
do {
printf("Let's build! Give me a number between 0 and 23 'inclusive'.\n");
tower_height = GetInt();
}
while
(tower_height < 0 || tower_height > 23);
// Outputs
for (tower_height = 0; tower_height <= 23; tower_height++)
printf ("%c = tower_height - 2\n");
}
C identifier names may contain letters, underscore, and digits, as long
as the first character isn't a digit, and as long as the identifier
isn't a keyword. They may not contain #.
# is not a valid variable name.
As pointed out, # is not a valid variable name.
You can see how # is properly used in the first line of your code: #include <stdio.h>
Instead, call your char variable something that uses letters and numbers eg: char c;

Not understanding a C program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have following C program and I am not understanding some point of this program
#include <stdio.h>
int main()
{
char ara[100];
while(NULL != gets(ara))
{
printf("%s\n", ara);
}
return 0;
}
If I input some string like Hello World, this code return me the output same as input. But, what is NULL and gets?? Are they from C library? Why their colour not changed when I compile them?
Please read description of function gets()!
This function reads string from stdin. It returns NULL if found end of line or end of file before any characters.

Can't get my code to work properly [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Task says:
Given a string, compute recursively a new string where all the 'x' chars have been removed.
My code:
#include<stdio.h>
#include<string.h>
char c[50];
int xx(char a[],int b,int d){
if(a[b]=='\0')
return a;
else if(a[b]=='x'){
c[d]=a[b+1];
return xx(a,b+2,d+1);}
else {
c[d]=a[b];
return xx(a,b+1,d+1);
}
}
int main()
{
char a[50];
scanf("%s",a);
xx(a,0,0);
printf("%s",c);
return 0;
}
As long as I don't type x next to the other x it works. Like if i type xaxb, the result will be ab.
But if I type xxaxxb, the result will be xaxb...
Your code skips over a potentially important character - a '\0' or an 'x' in these three lines:
else if(a[b]=='x'){
c[d]=a[b+1];
return xx(a,b+2,d+1);
}
This code goes ahead and copies the a[b+1] without checking that character at all.
You shouldn't copy anything there - just advance b by 1, and keep d as is:
else if(a[b]=='x'){
return xx(a,b+1,d);
}
This way the next level of invocation would check a[b+1] for you, stopping or removing it as needed.

Resources