Please debug this C program!!!

Status
Not open for further replies.

ajaybc

Youngling
I made a C program for my college labs for counting the number of alphabets digits etc. in an entered string.But it is not working.It is not showing any error but is not giving correct results.Please debug it for me.I have to use call by reference so please dont change that.


#include<stdio.h>
#include<ctype.h>
main()
{
int a=0,b=0,c=0,d=0;
char s[100];
clrscr();
printf("Input a string:");
gets(s);
count(&a,&b,&c,&d,s);
printf("alphabets=%d \n digits=%d\nspaces=%d\nothers=%d",a,b,c,d);
getch();
}
count(int *a,int *b,int *c,int *d,char s[])
{
int i;
for(i=0;s!='\o';i++)
{
if(isalpha(s))
*a=*a+1;
else if(isdigit(s))
*b=*b+1;
else if(isspace(s))
*c=*c+1;
else
*d=*d+1;

}
}


Please help bcoz i need the corrected one before tomorrow morning
 

adi007

Youngling
i think i found it..:D

type
Code:
char s[100];
before main()

and try it..
don't forget to remove the other "char s[100]" after main()

you have to make s a global variable..
if it's not then the function cannot access the values of s...

oppss...sorry you have used the call by reference na....
hmm....lemme me think...
 

amit_at_stg

Broken In
I have done little modification now the programme is running perfectly.


plz check s!='\0' has to be zero and u had put a o sign

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
count(int *a,int *b,int *c,int *d,char s[])
{
int i;
for(i=0;s!='\0';i++)
{
if(isalpha(s))
*a=*a+1;
else if(isdigit(s))
*b=*b+1;
else if(isspace(s))
*c=*c+1;
else
*d=*d+1;

}
}

main()
{
int a=0,b=0,c=0,d=0;
char s[100];
clrscr();
printf("Input a string:");
gets(s);
count(&a,&b,&c,&d,s);
printf("alphabets=%d \n digits=%d\nspaces=%d\nothers=%d",a,b,c,d);
getch();
}
 
OP
ajaybc

ajaybc

Youngling
I have done little modification now the programme is running perfectly.


plz check s!='\0' has to be zero and u had put a o sign

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
count(int *a,int *b,int *c,int *d,char s[])
{
int i;
for(i=0;s!='\0';i++)
{
if(isalpha(s))
*a=*a+1;
else if(isdigit(s))
*b=*b+1;
else if(isspace(s))
*c=*c+1;
else
*d=*d+1;

}
}

main()
{
int a=0,b=0,c=0,d=0;
char s[100];
clrscr();
printf("Input a string:");
gets(s);
count(&a,&b,&c,&d,s);
printf("alphabets=%d \n digits=%d\nspaces=%d\nothers=%d",a,b,c,d);
getch();
}


Thank you amit Bhai the problem is solved:grin:

I have one more doubt
Kindly debug th following program too.It is for concatenating two strings and displaying the result.
The problem is that when I enter the two strings eg."ajay" and "balachandran" it displays "abalachandran"


#include<stdio.h>
main()
{
char x[100],y[100];
clrscr();
printf("Input first string\n");
gets(x);
printf("Input second string\n");
gets(y);
cat(x,y);
printf("Concatenated string is\n");
puts(x);
getch();
}

cat(char *p,char *q)
{
while(*p!='\o')
{
p++;
while(*q!='\o')
{
*p=*q;
p++;
q++;
}
*p='\o';
}
}

Please make less modifications as I have written this prog in my record book already:( and more corrections will make it look nasty.I swear I will not copy someones record book without testing the program and writing in observation book hereafter
 

Pathik

Google Bot
Not much change. Just a wrong bracket and \o instead of \0
Code:
#include<stdio.h>
main()
{
char x[100],y[100];
clrscr();
printf("Input first string\n");
gets(x);
printf("Input second string\n");
gets(y);
cat(x,y);
printf("Concatenated string is\n");
puts(x);
getch();
}

cat(char *p,char *q)
{
while(*p!='\0')
{
p++;
}
while(*q!='\0')
{
*p=*q;
p++;
q++;
}
*p='\0';
}
 
OP
ajaybc

ajaybc

Youngling
Not much change. Just a wrong bracket and \o instead of \0
Code:
#include<stdio.h>
main()
{
char x[100],y[100];
clrscr();
printf("Input first string\n");
gets(x);
printf("Input second string\n");
gets(y);
cat(x,y);
printf("Concatenated string is\n");
puts(x);
getch();
}

cat(char *p,char *q)
{
while(*p!='\0')
{
p++;
}
while(*q!='\0')
{
*p=*q;
p++;
q++;
}
*p='\0';
}


It worked:D Thanks mate.Thanks to all.

Although Digit now sucks Digit forum rocks!
 
OP
ajaybc

ajaybc

Youngling
Please debug these two programs

1."Copy a text file to another in such a way that upper case characters are converted to lowercase vice versa giving the file name of the first file as arguments."

#include<stdio.h>
main(int argc,char *argv[])
{

FILE *fone,*ftwo;
char a;
clrscr();
fone=fopen(argv[1],"w");
printf("Enter the contents of %s:\n",argv[1]);

while((a=getchar())!=EOF)
{
fputc(a,fone);
}

fclose(fone);


fone=fopen(argv[1],"r");
ftwo=fopen(argv[2],"w");
while((a=fgetc(fone))!=EOF)
{
if(isupper(a))
fputc(tolower(a),ftwo);

else if(islower(a))
fputc(toupper(a),ftwo);

}

fclose(fone);
fclose(ftwo);
ftwo=fopen(argv[2],"r");
while((a=fgetc(ftwo)!=EOF))
{
printf("%c",a);
}
fclose(ftwo);
getch();
}




everything in this program works fine.it creates two files and copies the contents and converts uppercase and lower case.
The problem comes only when it is displaying the contents of the second file.It shows some rubbish.

Second one:

Please debug this program for finding the sine series upto n terms:

#include<stdio.h>
#include<math.h>
main()
{
int i,j,k,x,n,sign=-1,f=1,p=1,m;
clrscr();
printf("Enter the angle: ");
scanf("%d",&x);
printf("Enter number of terms: ");
scanf("%d",&n);
x=x*(3.14/180);
m=x;
for(i=1,j=3;i<=n;i++,j=j+2)
{
f=1;
for(k=1;k<=j;k++)
{
f=f*k;
}
p=pow(x,j);
m=m+((p/f)*sign);
sign=sign*(-1);
}
printf("%d",m);
getch();
}


I get the correct answer(which is 1) for the angle 90degree only.for all others I get 0.So I changed the int values to float.Then the answer for all angles changes to 0 which is wrong.


Please help me.
Iam in desperate need for help.
 

khattam_

Fresh Stock Since 2005
Edited Code.. See comments within the program for more:
Code:
#include<stdio.h>
#include<math.h>
#include<conio.h>
/* added conio.h for clrscr() and getch(). The C program will run.. but still.... */
/* edited some more to make the program more readable..*/
main()
{
	int i,j,k,n,sign=-1;/*p=1 and f=1 are not necessary, so removed.. later removed f as i edited the code such that f was not necessary*/
	float x,m;/*made these floats.. otherwise while converting angle, it wud not be accurate*/
	float p;/*made this float as well to change the code later for accuracy*/
/*	long unsigned int f; /* turned float into long unsigned int so that large values of f are supported.. but later removed to make the code more efficient.. coz f will be very large and we can't go with larger values of n.. later removed f altogether*/
	clrscr();
	printf("Enter the angle: ");
	scanf("%f",&x);
	printf("Enter number of terms: ");
	scanf("%d",&n);
	x=x*(3.14159/180);
	m=x;
	for(i=1,j=3;i<=n;i++,j=j+2){
/*		f=1;*/
		p=pow(x,j);
		for(k=1;k<=j;k++)	{
		   /*	f=f*k;*/
			p=p/k; /*removed the f thing and did the p/f here itself.. so that we can avoid large values for f*/
		}
		m=m+(p*sign); /* Corrected p/f to (float)p/f.. otherwise integer division was going on here.... Later after changing the code above, replaced p/f with p only*/
		sign=sign*(-1);
	}
	printf("%f",m);
	getch();
}

Suggestion: you don't actually need to input n if you can edit the code.. Hint: you can check for the number of digits after decimal you need and terminate the loop when the value of the term is negligible... coz inputting n for finding the value of sine of x does not make much sense to the user :)

Hope this helps...
 
Last edited:

aritrap

Mobile Addict
Why do u even use '\0' when the same thing can be done by replacing '\0' with NULL(without any quotes). It serves the same purpose but is easier to remember.
 
OP
ajaybc

ajaybc

Youngling
Why do u even use '\0' when the same thing can be done by replacing '\0' with NULL(without any quotes). It serves the same purpose but is easier to remember.

Thanx for the tip.Dint know that.


By the way thanx everyone mission accomplished.Record book submitted:D
 

rochela

Right off the assembly line
hi every body here i have a big problem with a software that is coded by thunder t parse and i have been trying to decode the software but i dont have the software to decode it. or if i can even find someone who can help me with the code the software is comersus crack. please move this if this is the wrong place for me to post thanks
 

dheeraj_kumar

Legen-wait for it-dary!
@rochela
hey.. what you're asking is illegal in this forum i believe.

are you asking for a crack or a serial for comersus asp shopping application? its definitely not allowed here. and what you are asking may be ThunRT which may mean the software is coded using VB. I would suggest that you google.
 
Status
Not open for further replies.
Top Bottom