Count number of same letters in a string - c

I have been reading "The C Programming Language" and I got to this problem, that my output is 0 for any given string I send.
My function looks like this:
int number_of_repeating(char *word,char k){
int b=0,len=0,i;
gets(word);
len=strlen(word);
for(i=0;i<len;i++){
if(word[i]==k)
b++;
}
return b;
}
Problem:
I send him word for example: Jhonny, and character n, so it should count number of n's in the word (in this case the output should be 2).
What am I doing wrong?

#include <stdio.h>
int number_of_repeating(char *word,char k){
int b=0,len=0,i;
gets(word); //<------- You need to remove this one because it may overwrite
len=strlen(word);
for(i=0;i<len;i++){
if(word[i]==k)
b++;
}
return b;
}
int main(void) {
// your code goes here
printf("%d",number_of_repeating("johnny",'n'));
return 0;
}

if you're passing the string in there is no reason to call gets(), that could be it or your types could be wrong.

Related

Variable isn't storing return value

I am trying to make a program that converts a hex string into decimal. However I am having an issue assigning a returned integer value from the findLength function. Going by the printf statements I can tell that findLength(theString) will yield the correct value however length is showing a value of 0 despite the fact that I have length = findlength(theString).
This isn't a homework problem, I'm just absolutely stumped as to why this simple assignment isn't working. I've already declared length so I know that's not the issue. I'm also getting no compiler messages. Any help would be greatly appreciated.
Edit: I know convert doesn't do anything useful and the for loop needs to be fixed however that shouldn't be effecting the findLength return right?
Second Edit:
I've always submitted a string of '324' to be tested.
#include <stdio.h>
int convert(char s[], int theLength);
int findLength(char s[]);
int main(){
char theString[100];
int result;
int i;
int length;
printf("%s","Hello, please enter a string below. Press enter when finished.");
scanf("%s",theString); //Apparently scanf is bad but we'll learn better input methods later.
//For my tests I submitted a string of '324'.
length = (findLength(theString)); //length = findLength('324')
printf("%d",findLength(theString)); //yields 3
printf("%d",length); //yields value of 0 always.
result = convert(theString, length);
printf("%d\n result is",result);
return 0;
} //End of main
int convert(char s[], int theLength){ //This function will eventually converts a string of hex into ints. As of now it does nothing useful.
int i;
int sum;
for(i = theLength; i=0; i--){
sum = sum + s[i];
printf("%d\n",sum);
}
return sum;
} //End of convert
int findLength(char s[]){
int i;
for(i = 0; s[i]!='\0'; ++i){
}
return(i);
} //End of findLength
The variable length is storing the correct value. I think what has you confused is how you've laid out your printf statements. If you were to try something like the below it would be much easier to see that your code works.
#include <stdio.h>
int findLength(char s[]);
int main(){
char theString[100];
int result;
int i;
int length;
printf("Hello, please enter a string below. Press enter when finished.\n");
scanf("%s",theString);
length = (findLength(theString));
printf("findLength(theString) = %d\n",findLength(theString));
printf("length = %d\n",length);
return 0;
}
int findLength(char s[]){
int i;
for(i = 0; s[i]!='\0'; ++i){
}
return(i);
}
Just to clarify in your post you have
printf("%d",findLength(theString));
printf("%d",length);
printf("%d\n result is",result);
Note the \n before the %d in the last printf statement. This is 0 because your convert function needs to be fixed and this is the value of result NOT length.

time limit exceeded for next palindrome

given a number, i was aked to find the next number which is a palindrome.Thisis the code i have writen. my code works fine but the website I am working on says "time limit exceeded"...how do I correct this?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLEN 50
void reverse (char s[]){
int c,i,j;
for (i=0,j=strlen(s)-1;i<j;i++,j--){
c= s[i];
s[i]=s[j];
s[j]=c;
}
}
void itoa (int n, char s[]) {
int i;
i = 0;
do {
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
s[i] = '\0';
reverse(s);
}
int main (void) {
int t,j;
scanf("%d",&t);
for(j=0;j<t;j++){
int k,c=1,i;
char s[MAXLEN];
scanf("%d",&k);
int a= k+1;
while (c!=0){
itoa(a,s);
int e=strlen(s)-1;
for(i=0;i<(e+1)/2;i++){
if (s[i]==s[e-i]){
c=0;
}
else{
c=1;
goto state;
}
}
state:a++;
}
printf("%s\n",s);
}
return 0;
}
You need to use a smarter way than brute force!
Here is an algorithm with time complexity log(N):
Read the number as a string.
Take the first half of the number.
Make a palindrome by adding the first half reversed. (Consider the two cases: even/odd length of the original number)
If this palindrome is greater than the original number you are done.
If it is not:
Take the first half of the number and add 1.
Make a palindrome by adding the first half reversed. (Again consider the two cases: even/odd length of the original number).
Done!

C recursive function calling concept

/*Program to print all the numbers between a lower bound and a upper bound values*/
#include<stdio.h>
#include<stdlib.h>
void recur(int a, int b);
int main(void)
{
int x,y;
printf("Enter the lower and upper bound values: \n");
scanf("%d %d",&x,&y);
void recur(x,y);
return 0;
}
void recur(int a,int b)
{
if(a<b)
{
printf("%d /n",a);
a++;
void recur(a,b);
}
}
The output I get is:
Enter the lower and upper bound values:
10
50
process returned 0.
Is there anything wrong with the syntax or return type..?
I've just started learning c.Need Help
Both
void recur(x,y);
void recur(a,b);
declares the functions (prototype). To call them, change them to
recur(x,y);
recur(a,b);
void recur(int a,int b)
{
if(a<b)
{
printf("%d /n",a);
a++;
//void recur(a,b); You need to call the function not to declare
// there is difference between dec and calling.
recur(a,b); // is the correct way to do this .
}
}
same requires in main() method though
You have the following errors in your program:
When you call a function the syntax is
funcName(param1, param2);
or, if function returns a value:
ret = funcName2(param3, param4, param5);
In your code, putting void at call time is a syntax error:
void recur(a,b);
This is how function is declared, not called. Attention there is a difference between function declaration and function definition.
If you want to print special chars in C, like newline, you need to print '\n' line this:
printf("some message followed by newline \n");
Note that '\n' is a single char, even if you see a '\' and a 'n'. '\' has the purpose to escape the next char 'n' making it a new char. So '\n' is the newline character.
Other special characters in C are: '\t' for tab, '\' to actually print a '\'. Strings in C are enclosed in double quotes like "message" while chars are enclosed in single quotes like 'a', 'b', '\n', 'n', '\'.
Here is your working code.
#include<stdio.h>
#include<stdlib.h>
void recur(int a, int b);
int main(void)
{
int x,y;
printf("Enter the lower and upper bound values: \n");
scanf("%d %d",&x,&y);
recur(x,y); // change made here
return 0;
}
void recur(int a,int b)
{
if(a<b)
{
printf("%d \n",a);
a++;
recur(a,b); // change made here
}
}

C : Output printing a line later

I wrote this small program to count the number of trailing zeroes. I got my algorithm correct. But I cannot get the output right. The first line we enter is for the number of inputs (T). Later the user enters the number (whose number of trailing zeroes in factorial is to be calculated.) And then print the answer (count). But after I input the value for N, I get the answer on the third line (I used just one '\n'). I need to get my output right.
#include <stdio.h>
int main()
{
int T;
int i,j,temp,count=0;
long int N;
scanf("%d",&T);
for(i=0;i<T;i++)
{
scanf("\n%ld",&N);
for(j=5;j<=N;j+=5)
{
temp=j;
while(j > 1)
{
if(j%5 == 0)
count++;
j=j/5;
}
j=temp;
}
printf("\n%d",count);
count =0;
}
return 0;
}
Change:
printf("\n%d",count);
to:
printf("%d\n",count);
and:
scanf("\n%ld",&N);
to:
scanf("%ld",&N);

Defining a letter as a character

Basically what i'm trying to do is to define letters as a number so if i give a letter to my program it treats it like a normal number.
I tried to do it using this code but it doesn't seem to work: If you have any suggestions i'd be thankful.
#include<stdio.h>
int main()
{
#define F 10
int liczba;
scanf("%d",liczba);
printf("%d",liczba);
}
I also tried to define it using the loop but then i was getting an error
#include<stdio.h>
int main()
{
int i;
for(i = 10; i<32; i++)
{
#define '55+i' i
}
}
If you want the input to be a letter,then you need a char variable instead of an int:
#include<stdio.h>
int main()
{
char liczba; //char variable
scanf("%c",&liczba); //%c for a character
printf("%d",liczba); //prints the ASCII value of the character
}
I read the comments and you said that you need A=10,B=11 etc. In that case,use
if(liczba>='A' && liczba <='Z')
printf("%d",(liczba-'A')+10);
The if is required to check if the input is between A and Z.

Resources