 |
16-03-2008, 03:20 PM
|
#1 (permalink)
|
|
Alpha Geek
Join Date: Feb 2007
Location: Cochin
Posts: 744
|
Please debug this C program!!!
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[i]!='\o';i++)
{
if(isalpha(s[i]))
*a=*a+1;
else if(isdigit(s[i]))
*b=*b+1;
else if(isspace(s[i]))
*c=*c+1;
else
*d=*d+1;
}
}
Please help bcoz i need the corrected one before tomorrow morning
__________________
Available for freelance web design and web development jobs.
You can view my portfolio here.
Ajay Balachandran
Please contact via PM
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
16-03-2008, 03:30 PM
|
#2 (permalink)
|
|
Alpha Geek
Join Date: Sep 2007
Location: hassan
Posts: 717
|
Re: Please debug this C program!!!
i think i found it..
type
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...
|
|
|
16-03-2008, 03:43 PM
|
#3 (permalink)
|
|
Apprentice
Join Date: May 2006
Posts: 96
|
Re: Please debug this C program!!!
I have done little modification now the programme is running perfectly.
plz check s[i]!='\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[i]!='\0';i++)
{
if(isalpha(s[i]))
*a=*a+1;
else if(isdigit(s[i]))
*b=*b+1;
else if(isspace(s[i]))
*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();
}
__________________
learn how to earn money online : http://howearnmoney.50webs.com
|
|
|
16-03-2008, 03:50 PM
|
#4 (permalink)
|
|
Alpha Geek
Join Date: Sep 2007
Location: hassan
Posts: 717
|
Re: Please debug this C program!!!
good find amit_at_stg..
null char is represented by '\0'(back slash zero) not '\o'
|
|
|
16-03-2008, 03:55 PM
|
#5 (permalink)
|
|
Alpha Geek
Join Date: Feb 2007
Location: Cochin
Posts: 744
|
Re: Please debug this C program!!!
Quote:
Originally Posted by amit_at_stg
I have done little modification now the programme is running perfectly.
plz check s[i]!='\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[i]!='\0';i++)
{
if(isalpha(s[i]))
*a=*a+1;
else if(isdigit(s[i]))
*b=*b+1;
else if(isspace(s[i]))
*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
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
__________________
Available for freelance web design and web development jobs.
You can view my portfolio here.
Ajay Balachandran
Please contact via PM
|
|
|
16-03-2008, 04:42 PM
|
#6 (permalink)
|
|
Google Bot
Join Date: Aug 2005
Posts: 9,772
|
Re: Please debug this C program!!!
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';
}
__________________
My new blog: www.pathikshah.com
|
|
|
16-03-2008, 05:19 PM
|
#7 (permalink)
|
|
Alpha Geek
Join Date: Feb 2007
Location: Cochin
Posts: 744
|
Re: Please debug this C program!!!
Quote:
Originally Posted by Pathik
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  Thanks mate.Thanks to all.
Although Digit now sucks Digit forum rocks!
__________________
Available for freelance web design and web development jobs.
You can view my portfolio here.
Ajay Balachandran
Please contact via PM
|
|
|
16-03-2008, 05:59 PM
|
#8 (permalink)
|
|
I have Yolks not Brains!
Join Date: Aug 2006
Location: Inside the shell
Posts: 743
|
Re: Please debug this C program!!!
Quote:
Originally Posted by ajaybc
It worked  Thanks mate.Thanks to all.
Although Digit now sucks Digit forum rocks!
|
Precisely, people here does!!
__________________
Y U NO ALLOW PICTURES IN SIGNATURES?
|
|
|
23-03-2008, 02:09 PM
|
#9 (permalink)
|
|
Alpha Geek
Join Date: Feb 2007
Location: Cochin
Posts: 744
|
Re: Please debug this C program!!!
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.
__________________
Available for freelance web design and web development jobs.
You can view my portfolio here.
Ajay Balachandran
Please contact via PM
|
|
|
23-03-2008, 08:23 PM
|
#10 (permalink)
|
|
Alpha Geek
Join Date: Feb 2007
Location: Cochin
Posts: 744
|
Re: Please debug this C program!!!
Please someone post the debugged program.Please............
__________________
Available for freelance web design and web development jobs.
You can view my portfolio here.
Ajay Balachandran
Please contact via PM
|
|
|
24-03-2008, 09:46 PM
|
#11 (permalink)
|
|
Fresh Stock Since 2005
Join Date: Feb 2005
Posts: 1,015
|
Re: Please debug this C program!!!
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...
__________________
http://www.khattam.info
Last edited by khattam_; 24-03-2008 at 09:54 PM.
|
|
|
25-03-2008, 04:06 PM
|
#12 (permalink)
|
|
Mobile Addict
Join Date: Aug 2006
Location: Kolkata
Posts: 165
|
Re: Please debug this C program!!!
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.
__________________
Please visit my site: hotshot05.page.tl or my blog: mobimad.co.cc for anything related to mobile reviews, games , applications and news. Please review my site.
|
|
|
28-03-2008, 12:46 PM
|
#13 (permalink)
|
|
Alpha Geek
Join Date: Feb 2007
Location: Cochin
Posts: 744
|
Re: Please debug this C program!!!
Quote:
Originally Posted by aritrap
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
__________________
Available for freelance web design and web development jobs.
You can view my portfolio here.
Ajay Balachandran
Please contact via PM
|
|
|
03-04-2008, 10:07 AM
|
#14 (permalink)
|
|
Broken In
Join Date: Jan 2006
Posts: 123
|
Re: Please debug this C program!!!
@ajaybc
try this "while((a=fgetc(ftwo))!=EOF)"
|
|
|
04-04-2008, 07:07 PM
|
#15 (permalink)
|
|
Right Off the Assembly Line
Join Date: Apr 2008
Posts: 1
|
Re: Please debug this C program!!!
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
|
|
|
05-04-2008, 12:07 AM
|
#16 (permalink)
|
|
Legen-wait for it-dary!
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
|
Re: Please debug this C program!!!
@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.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|