Help me in these programs(C programing)

krishnandu.sarkar

Simply a DIGITian
Staff member
Let me tell you line by line if that makes more sense

#include<stdio.h> //Needs No Explanation

int check(int, int); //The Function Declaration

int main() //Main
{
int n, a, res;
printf("Enter a Number : ");
scanf("%d", &n); //Get a number from user and store it in n. Say user entered 12345.

printf("Enter The Number To Search For Occurrence : ");
scanf("%d", &a); //Get the number to search from n (i.e. 12345). Say user entered 3.

res = check(n, a); //Call the Check function. Check() returns the number of occurrence in integer so we store it (the return value / result) in res which is a integer declared above. Check() takes two integer as parameters... 1. The number, 2. The Number to Search from the first number. So it's actually res = check(12345, 3);

//At this point when the function gets called, the control jumps to the function definition point. i.e. where the function body is defined.

printf("The Number of Occurrence of %d in %d is %d", a, n, res); //The result is shown to the user.

return 0;
}

int check(int num, int che) //Control jumps here on calling the function and passes the values to the variables (parameters) mentioned in the () to hold the value. So now num = 12345, che = 3. NOTE : This num and che is local to this function. Outside this function body these variables have no existence.
{
int rem, occ=0, quo=num; //initializing quo as num. So quo = 12345.
while(quo>0) //if quo is > 0 i.e. if 12345 > 0
{ //The whole logic is implemented here to find the times of occurrence of 3 in 12345.
rem=quo%10;
quo=quo/10;
if(che==rem)
occ++;
} //After the whole calculation the result is stored in occ. So we return occ in the next line
return occ; //NOTE : This return value again must be hold somewhere. Right? To hold this value we wrote res = check(n, a). So this occ gets stored in res above in main().
}
 

krishnandu.sarkar

Simply a DIGITian
Staff member
You are welcome..!! Hope that helps. Do not hesitate to ask questions if you are still not clear.

In the mean time, if you get time, follow Head First C to learn the Language along with your suggested book by College (Probably Kaneetkar) to only pass the semester exam :)
 
OP
theserpent

theserpent

Firecracker to the moon
Here some more basic programs, are they right?
1)Lower case to upper case(Without using functions)
Code:
#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int i;
gets(a);
for(i=0;a[i]!='\0';i++)
{
for(a[i]>=97&&a[i]<=122)
{
a[i]=a[i]-32; [like say a is 97, so 97-32=65, where 65=A]
}
}
puts(a);
}

2)Upper to Lower

#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int i;
gets(a);
for(i=0;a!='\0';i++)
{
for(a>=65&&a<=90)
{
a=a+32;
}
}
puts(a);
}

3)Check if a word is plaindrome or not using inbuilt functions
EG: ANA,MADAM
Code:
#include<stdio.h>
#include<string.h>
main()
{
char a[100],b[100];
int c;
gets(a);
strcopy(b,a);  [Copying a to b, like i can reverse b)
strrev(b);
c=strcomp(a,b);
if(c==0)
printf("Palindrome");
else
printf("Not a Palindrome");
}

4) Palindrome without functions

How do i do this??

#include<stdio.h>
#include<string.h>
main()
{
int a,b,c;
gets(a);
for(i=0;a!='\o';i++)
{

All the programs are not executing :( , i tried compling them in a online compiler
 
Last edited:

krishnandu.sarkar

Simply a DIGITian
Staff member
Here some more basic programs, are they right?
1)Lower case to upper case(Without using functions)
Code:
#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int i;
gets(a);
for(i=0;a[i]!='\0';i++)
{
for(a[i]>=97&&a[i]<=122)
{
a[i]=a[i]-32; [like say a is 97, so 97-32=65, where 65=A]
}
}
puts(a);
}

This one is perfectly fine. But just one mistake. The nested for will be if. IDK why did you write for. Aren't you using your Logic? Why for? You are checking the value is whether between 97 and 122. So it should be if

Code:
#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int i;
gets(a);
for(i=0;a[i]!='\0';i++)
{
    if(a[i]>=97&&a[i]<=122)
    {
        a[i]=a[i]-32; //[like say a is 97, so 97-32=65, where 65=A]
    }
}
puts(a);
}

^^Just corrected your code.

2)Upper to Lower

#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int i;
gets(a);
for(i=0;a!='\0';i++)
{
for(a>=65&&a<=90)
{
a=a+32;
}
}
puts(a);
}



Same fault again. The nested for will be if.

Code:
#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int i;
gets(a);
for(i=0;a[i]!='\0';i++)
{
    if(a[i]>=65&&a[i]<=90)
    {
    a[i]=a[i]+32;
    }
}
puts(a);
}

I don't think you do the debugging part ever. Next time no help will be provided if you don't debug your program with imaginary values.

3)Check if a word is plaindrome or not using inbuilt functions
EG: ANA,MADAM
Code:
#include<stdio.h>
#include<string.h>
main()
{
char a[100],b[100];
int c;
gets(a);
strcopy(b,a);  [Copying a to b, like i can reverse b)
strrev(b);
c=strcomp(a,b);
if(c==0)
printf("Palindrome");
else
printf("Not a Palindrome");
}

From where did you copied this? Do you ever read what error compiler throws? The function names you used are strcopy and strcomp but they are actually strcpy() and strcmp(). Just change that and everything will work fine.

4) Palindrome without functions

How do i do this??

#include<stdio.h>
#include<string.h>
main()
{
int a,b,c;
gets(a);
for(i=0;a!='\o';i++)
{

All the programs are not executing :( , i tried compling them in a online compiler


I'd just say you the logic.
  • Take a String
  • Reverse it using for loop and store it in another string
  • Now check both the char array one by one

The for loop will run from back of the string... i.e. for (i=length_of_string - 1; i >= 0; i--)

(length_of_string - 1) because final character is NULL (\0)

There's another way...
Run a for loop to length_of_string/2 i.e. half and match 1st char with last char then with 2nd char with last -1 char in this way untill the char keeps matching.

If you find mismatch discard the loop and display not palindrome.

Eg.

MADAM
12345

For Loop will run 5/2 = 2
So 1 - 5 (M-M)
2 - 4 (A - A)
Display Palindrome

Hello
12345

For loop will run 5/2 = 2
Match 1 - 5 (H - O)
didn't matched...so no necessary to keep matching. Once unmatched discard the loop and display Non Palindrome.

Code:
#include <stdio.h>
#include <string.h>
int main()
{
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string: ");
scanf("%s", string1);
length = strlen(string1);

for(i=0;i < length ;i++)
{
    if(string1[i] != string1[length-i-1])
        {
        flag = 1;
        break;
        }
    }

if (flag)
{
    printf("%s is not a palindrome ", string1);
}
else
{
    printf("%s is a palindrome ", string1);
}
return 0;
}

^^The program for 2nd Logic. If you want 1st Logic, try it yourself.
 
OP
theserpent

theserpent

Firecracker to the moon
Oh crap, forgot its strcpy :/
thanks , Yeah my brain wasn't working so by mistake i wrote for here

From where did you copied this? Do you ever read what error compiler throws? The function names you used are strcopy and strcomp but they are actually strcpy() and strcmp(). Just change that and everything will work fine.

Ahh well, wont happen again, thanks for pointing it out
 
Top Bottom