write the function int countchtr(char string[],int
ch);which returns the number of timesthe character ch
appears in the string. for example the call countchtr("she
lives in Newyork",'e') would return 3.

Answers were Sorted based on User's Feedback



write the function int countchtr(char string[],int ch);which returns the number of timesthe charac..

Answer / goloap

int countchtr(char *str, char ch)
{
int count=0;
char *itr = str;
while (*itr != '\0')
{
if(*itr == ch)
{
count++
}
itr++;
}
return count;
}

Is This Answer Correct ?    4 Yes 0 No

write the function int countchtr(char string[],int ch);which returns the number of timesthe charac..

Answer / vignesh1988i

a small change..............

#include<stdio.h>
#include<conio.h>
int string(char *,char);
void main()
{
char str[100],ch;
int c;
printf("enter the string :");
gets(str);
printf("enter the character to be searched :");
scanf("5c",&ch);
c=string(&str[0],ch);
printf("the character %c occurs for %d times ",ch,c);
getch();
}
int string(char *a,char ch)
{
int count=0;
for(int j=0;*a!='\0';j++)
{
if(*a==ch)
{
count++;
*a++;
}
}
return count;
}

Is This Answer Correct ?    3 Yes 0 No

write the function int countchtr(char string[],int ch);which returns the number of timesthe charac..

Answer / vignesh1988i

#include<stdio.h>
#include<conio.h>
int string(char *,char);
void main()
{
char str[100],ch;
int c;
printf("enter the string :");
gets(str);
printf("enter the character to be searched :");
scanf("5c",&ch);
c=string(&str[0],ch);
printf("the character %c occurs for %d times ",ch,c);
getch();
}
int string(char *a,char ch)
{
int count=0;
for(int j=0;*a!='\0';j++)
{
if(*a==ch)
{
count++;
*(a++);
}
}
return count;
}

Is This Answer Correct ?    4 Yes 2 No

write the function int countchtr(char string[],int ch);which returns the number of timesthe charac..

Answer / vadivel t

#include<stdio.h>
#include<conio.h>

int main()
{
char ptr[100]= "She lives in NEWYORK";
char ch;
printf("ENTER THE CHARACTER:\n");
scanf("%c", &ch);
printf("CHAR %c EXIST %d TIME(S)\n",ch, countchtr(ptr, ch));
getch();
}

int countchtr(char *ptr, char ch)
{
int count = 0;
char ch1;
if(ch >= 97 && ch <= 122)
{
ch1 = ch - 32;
}
else if(ch >= 65 && ch <= 96)
{
ch1 = ch + 32;
}
while(*ptr != '\0')
{ if((*ptr == ch) || (*ptr == ch1))
{
count++;
}
ptr++;
}
return count;
}

Is This Answer Correct ?    1 Yes 0 No

write the function int countchtr(char string[],int ch);which returns the number of timesthe charac..

Answer / jagjit

#include<stdio.h>
#include<conio.h>
int string(char *,char);
void main()
{
char str[100],ch;
int c;
printf("enter the string :");
gets(str);
printf("enter the character to be searched :");
scanf("5c",&ch);
c=string(&str[0],ch);
printf("the character %c occurs for %d times ",ch,c);
getch();
}
int string(char *a,char ch)
{
int count=0;
for(int j=0;*a!='\0';j++)
{
if(*a==ch)
{
count++;
*(a++);
}
}
return count;
}

Is This Answer Correct ?    0 Yes 0 No

write the function int countchtr(char string[],int ch);which returns the number of timesthe charac..

Answer / vadivelt

Hi all,

In my post, Answer #5 pls change the statement in if
condition from "ch <= 96" to "ch <= 90"

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

How can you increase the size of a dynamically allocated array?

0 Answers   TISL,


Difference between Shallow copy and Deep copy?

0 Answers  


fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is called as: fun(10); what will it print? }

17 Answers   NDS,


why we wont use '&' sing in aceesing the string using scanf

0 Answers   HCL,


What is hash table in c?

0 Answers  


main() {int a=200*200/100; printf("%d",a); }

14 Answers   TCS,


void main() { for(; 0 ;) ... { printf("hello"); ... } getch(); }

1 Answers  


Difference between for loop and while loop?

1 Answers  


What does 2n 4c mean?

0 Answers  


What is the use of keyword VOLATILE in C?

1 Answers  


What are multibyte characters?

0 Answers  


How do I use void main?

0 Answers  


Categories