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.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was trying to explain to my friend something about C coding and he asked me why his code (with "scanf") didn't work.
#include
int main() {
char x=scanf("%c",&x);
printf("%c\n",x);
return 0;
}
and this one yes
#include <stdio.h>
int main()
{
int k;
char x=getchar
printf("%c\n",x);
return 0;
}
When scanf completes, x contains the character that was read. However, that value is immediately overwritten when x is assigned the return value of scanf, which is the number of items successfully matched or EOF in the event of an error.
If you call scanf without assigning the return value to x you should get the expected result.
For example, this should work.
char x;
scanf("%c",&x);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've found this function on the internet and I find it to be very useful
but I am new to programming, and could someone please explain briefly what does it exactly do
#include <stdio.h>
int diffcount(char* s)
{
unsigned char seen[127];
int cnt=0,i;
for(i=0;i<127;i++)
seen[i]=0;
for(i=0;s[i];i++)
{
if(!seen[(int)s[i]])
{
cnt++;
seen[(int)s[i]]=1;
}
}return cnt;
}
int main(void) {
char string[20];
scanf("%s",string);
printf("Razlicitih znakova: %d\n", diffcount(string));
return 0;
}
First of all we init an empty array of zeros int seen[127];
"seen" array is used to find out whether char with code i has been met in the array s : if seen[i]==1 than (char)i was in the string s.
After that we make a loop through char* s and check if char s[i] has already been met by looking at the value of seen[s[i]]; and if it is false we put seen[s[i]]=true (because we met it) and increase our counter.
The result of the function is the value of variable cnt
This may also help:
each char has it's code between zero and 127. For example, (int)'a' = 97.
bool in the C is just the same as int, that's why we sometimes use 0 and 1 instead of true and false
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 the following code:
int (*predicate)(char) = 0;
Can anyone tell me what this code means? What is the meaning of the word predicate in C?
The sentence is a declaration and definition of a pointer to a function taking one argument (char) and returning int. The pointer is initialized to the null pointer value.
The word "predicate" is the programmer's choice for the variable name.
Reference: cdecl
One might use predicate like this:
/* UNTESTED */
int IsLower(char c) { return c >= 'a' && c <= 'z'; }
int main () {
int (*predicate)(char);
predicate = IsLower;
if ( (*predicate)('f') == 1 ) printf("'f' is lower case!\n");
}
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let us say that the English alphabets A to Z has value start from 1. A = 1, B = 2, C = 3 and so on. Write a program which calls a function which accepts the name of a person which is constant character array and returns an integer value with sum of the Alphapets. What is the benefit of passing the name of the person as const char array?
Suppose somebody else provides me a function that takes non-const char * and does the job. What the function is actually implemented is like this:
int get_int_sum(char *name)
{
int sum;
//codes to calculate sum of alphas
name[0] += 1;
//continue
return sum;
}
When I call the function using
char my_name[] = "Yu Hao";
int reuslt = get_int_sum(my_name);
Even if I got the result I want, my_name is changed to "Zu Hao" without my notice. However, if a function has a prototype of
int get_int_sum(const char*name)
I am sure that the string I passed will not be modified.
One advantage is that elements in a array are protected from changing its value.
For example, here is simple code.
int Your_function(const char * a)
{
a[3] = 'A'; // this statement causes compile error.
// do something
return 0;
}