Write a function that accepts a pointer to a string and a character and returns the number of times the character is found in the string.

# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <stdlib.h>
void countletter(char *str);
int main()
{
	char str;
	printf("Please enter a string: ");
	gets(str);
	countletter(str);
	getch();
	return 0;
}

void countletter(char *str)
{
	int count;
	char a;
	gets(a);
	for (;(*str)!='\0';str++)
	{
		if (*str==a)
		{
		count++;
		}
	}
	 printf("%d",count);
}

The error im getting is
error C2664: 'gets' : cannot convert parameter 1 from 'char' to 'char *'

I tried everything to get this. But if anyone has any other ways or writing it i would love to see it. Thanks

Hello slygoth,
You have declared str as char str in main(). Declare it as a pointer.

Write a function that accepts a pointer to a string and a character and returns the number of times the character is found in the string.

Your function prototype should be

int countletter(char *str, char c);

I dont understand fully what you guys means. Can u edit the code for me and ill just check it and see whats the changes you guys made.

Change char str to char *str and also use cin to get character in countletter function.

int main()
{
	char *str; //<--str changed to *str(pointer)
	printf("Please enter a string: ");
	gets(str);
	countletter(str);
	getch();
	return 0;
}

Change char str to char *str.

int main()
{
	char *str; //<--str changed to *str(pointer)
	printf("Please enter a string: ");
	gets(str);
	countletter(str);
	getch();
	return 0;
}

Yeh i added that part. But it still getting an error
error C2664: 'gets' : cannot convert parameter 1 from 'char' to 'char *'

Try using cin instead of gets to get character from the user in countletter function.
As gets is used to get strings and not a single character.

void countletter(char *str)
{
	int count;
	char a;
	cin>>a;
	for (;(*str)!='\0';str++)
	{
		if (*str==a)
		{
		count++;
		}
	}
	 printf("%d",count);
}
commented: Since when is cin a C input commant? -3

@slygoth your entire code is correct except in line 8.You declared 'str' as character variable in line 8. You need to declare it as an character ARRAY.

char str[100];

A pointer on gets()..Here's what my help file states about its use.

Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

Try using cin instead of gets to get character from the user in countletter function.
As gets is used to get strings and not a single character.

void countletter(char *str)
{
	int count;
	char a;
	cin>>a;
	for (;(*str)!='\0';str++)
	{
		if (*str==a)
		{
		count++;
		}
	}
	 printf("%d",count);
}

You release this is the C section.

# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <stdlib.h>
void countletter(char *str);
int main()
{
	char str[8];
	printf("Please enter a string: ");
	gets(str);
	countletter(str);
	getch();
	return 0;
}

void countletter(char *str)
{
	int count;
	char a;
	gets(a); <---- The error is here
	for (;(*str)!='\0';str++)
	{
		if (*str==a)
		{
		count++;
		}
	}
	 printf("%d",count);
}

This one gives me 1 error and points where i stated above
error C2664: 'gets' : cannot convert parameter 1 from 'char' to 'char *'
and

# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <stdlib.h>
void countletter(char *str);
int main()
{
	char str[8];
	printf("Please enter a string: ");
	gets(str);
	countletter(str);
	getch();
	return 0;
}

void countletter(char *str)
{
	int count;
	char a;
	scanf("%s",a);
	for (;(*str)!='\0';str++)
	{
		if (*str==a)
		{
		count++;
		}
	}
	 printf("%d",count);
}

This one runs but it didnt show anything it just said break unexpected error.

Sorry i write it two times see post above

@Arbus as gerard4143 said dont use c++ code here. gerard4143 nice information on gets() function. I think u copied those lines from manual of GCC..juss kidding ;)

The problem is your understanding of a c-string. A c-string is an array of characters terminated by an '\0'. So this

char a;

Is not a c-string, its a character.

THis thing is driving me nuts. Can someone just modify the code so it runs. So it count the amount of time the letter is seem in the string please. Just modify the code and ill copy it

What?? you DONT want to learn but want to COPY??? "gerard4143" has given answer directly, can't you analyse it??

This forum isn't a homework completion site, its a site to aid coders in the understanding of the computers and programming.

No its not a homework question is just something im trying to figure out but ive been trying everything you guys posted but still im getting an error. Its because im see so much different changes to make its kinda confusing on which one to use and who the follow

# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <stdlib.h>
void countletter(char *str);
int main()
{
	char str[8];
	printf("Please enter a string: ");
	gets(str);
	countletter(str);
	getch();
	return 0;
}

void countletter(char *str)
{
	int count;
	char a;
	scanf("%s",a);
	for (;(*str)!='\0';str++)
	{
		if (*str=a)
		{
		count++;
		}
		else;
	}
	 printf("%d",count);
	 getch();
}

this one runs. But the error after i put in the inputs i need to are.


An unhandled exception of type 'System.AccessViolationException' occurred in Pointers and Array.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Here's an example that uses enough functionality to answer your questions.

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

#define ARR_SIZE 100

int main()
{
	size_t i = 0;
	size_t size = 0;
	char str[ARR_SIZE];

	fputs("Enter a string->", stdout);
	fgets(str, ARR_SIZE, stdin);

	size = strlen(str);

	for (i = 0; i < size; ++i)
		fputc(str[i], stdout);
	return 0;
}

Ok i dont understand what you did there but i guess ima go back to the drawing board about pointers and arrays because obviously i dont understand it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.