Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#How do I have the same result in C...

Python
t=int(input())
for j in range(t):
        s=input()
        def co(a,b):
                c=0
                for i in b:
                        if(i==a):
                                c+=1
                return c

        for i in s:
                if(co(i,s)==len(s)/2):
                        print("YES")
                        break
        else:
                print("NO")


What I have tried:

Objective-C
#include<stdio.h>
#include<string.h>
int co(char,char);
void main(){
	char s[50];
	int i,c;
	scanf("%s",s);
	while(s[i]!='\0'){
		if(co(s[i],s)==len(s)/2)
			printf("YES");
		else
			printf("NO");
		i++;
	}
}
int co(char a,char b[50]){
	int i,c=0;
	while(b[i]!='\0'){
		if(b[i]==a)
			c++;
	}
	return c;
}
Posted
Updated 2-Mar-18 22:50pm

1 solution

C++
int i,c;
scanf("%s",s);
while(s[i]!='\0'){

You have not initialised the index variable (i) to zero, to start indexing the array. Your code would probably be easier to debug if you used proper meaningful variable names rather than single letters.
 
Share this answer
 
v2
Comments
stackphish 3-Mar-18 7:40am    
I do accept your valuable suggestion...But the error seem to be some another as follows...

char_occur.c:4:1: warning: return type of 'main' is not 'int'
[-Wmain-return-type]
void main(){
^
char_occur.c:4:1: note: change return type to 'int'
void main(){
^~~~
int
char_occur.c:9:14: warning: incompatible pointer to integer conversion passing
'char [50]' to parameter of type 'char' [-Wint-conversion]
if(co(s[i],s)==len(s)/2)
^
char_occur.c:3:17: note: passing argument to parameter here
int co(char,char);
^
char_occur.c:9:18: warning: implicit declaration of function 'len' is invalid in
C99 [-Wimplicit-function-declaration]
if(co(s[i],s)==len(s)/2)
^
char_occur.c:16:5: error: conflicting types for 'co'
int co(char a,char b[50]){
^
char_occur.c:3:5: note: previous declaration is here
int co(char,char);
^
3 warnings and 1 error generated.
Richard MacCutchan 3-Mar-18 8:20am    
Yes, a number of other errors. You need to correct those as specified in the error messages.

- the standard declaration for main in C and C++, is that it returns an integer value, so int main()
- Your declaration and definition of co do not match, they should both be int co(char a,char b[50]) or better still: int co(char a,char *b)
- there is no function len in your code; you should use the standard C library function strlen

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