anemos
asked on
segmentation error
this is my code :
#include <stdio.h>
#include <string.h>
#define size 40
/* ========================== ========== === */
void copy(char prv[], const char curr[]){
/* copies string curr to string prv*/
int i;
for (i=0; (prv[i]=curr[i])!='\0'; i++);
}
/* ========================== ========== === */
void modify(char s[]){
int i;
for (i=0; s[i]!=' '; i++);
s[i]='\0';
}
/* ========================== ========== === */
int convert(const char c){
return c-48;}
/* ========================== ========== === */
void main (int argc, char *argv[]){
int counter=1,N=4;
char curr_line[size],prv_line[s ize]="\0";
/* current line ,previous line */
if (argc=1)
if (convert(*argv[1])>0)
N=convert(*argv[1]);
printf("N=%i\n",N);
gets(curr_line);
while (curr_line[0]!='\0' && curr_line[0]!=EOF) {
modify(curr_line);
if (strcmp(prv_line,"\0")!=0)
if (strcmp(curr_line,prv_line )==0)
++counter;
else
if (counter>=N){
printf("Anagram %s occured %i times\n",prv_line,counter) ;
counter=1;}
copy(prv_line,curr_line);
gets(curr_line);
} /* of while loop */
if (counter>=N) printf("%s %i",curr_line,counter);
/* the above statement prints any anagrams that were */
/* not produced because of the termination of input */
}
/* end of main funtion */
What it is supposed to do is read the first word of a line and store it into curr_line.
It then goes reading the first word of the next line and stores it again into curr_line. The previous words was stored in prv_line.
If the two words, prv_line and curr_line match then counter is incremented.
If counter is greater than N, which is specifid by a program paramater, then display the word and how many times it occured.
Although it sounds quite simple I keep getting segmentation errors and other times a different number of matches is used (N) although I have specified a different one.
thank you
#include <stdio.h>
#include <string.h>
#define size 40
/* ==========================
void copy(char prv[], const char curr[]){
/* copies string curr to string prv*/
int i;
for (i=0; (prv[i]=curr[i])!='\0'; i++);
}
/* ==========================
void modify(char s[]){
int i;
for (i=0; s[i]!=' '; i++);
s[i]='\0';
}
/* ==========================
int convert(const char c){
return c-48;}
/* ==========================
void main (int argc, char *argv[]){
int counter=1,N=4;
char curr_line[size],prv_line[s
/* current line ,previous line */
if (argc=1)
if (convert(*argv[1])>0)
N=convert(*argv[1]);
printf("N=%i\n",N);
gets(curr_line);
while (curr_line[0]!='\0' && curr_line[0]!=EOF) {
modify(curr_line);
if (strcmp(prv_line,"\0")!=0)
if (strcmp(curr_line,prv_line
++counter;
else
if (counter>=N){
printf("Anagram %s occured %i times\n",prv_line,counter)
counter=1;}
copy(prv_line,curr_line);
gets(curr_line);
} /* of while loop */
if (counter>=N) printf("%s %i",curr_line,counter);
/* the above statement prints any anagrams that were */
/* not produced because of the termination of input */
}
/* end of main funtion */
What it is supposed to do is read the first word of a line and store it into curr_line.
It then goes reading the first word of the next line and stores it again into curr_line. The previous words was stored in prv_line.
If the two words, prv_line and curr_line match then counter is incremented.
If counter is greater than N, which is specifid by a program paramater, then display the word and how many times it occured.
Although it sounds quite simple I keep getting segmentation errors and other times a different number of matches is used (N) although I have specified a different one.
thank you
also, when you do : "if (argc=1)..." you actually assign argc with the value '1' and that will always return 1 (which is TRUE).
Arnon David.
Arnon David.
Another problem could occur if you gets more than size characters, or if curr_line contains no ' '
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#include <process.h>
#include <stdio.h>
#include <string.h>
#define size 40
int convert(const char c)
{
return c-48;
}
void main (int argc, char *argv[])
{
int counter=1,N;
char curr_line[size],prv_line[s ize]="\0";
if (argc==2 && (strlen(argv[1])==1) && (convert(*argv[1]) > 0))
N=convert(*argv[1]);
else
{
perror("Invalid Parameter");
exit(0);
}
printf("N = %i\nTo end type ""ENTER"" twice.\n",N);
gets(curr_line);
strcpy(prv_line,curr_line) ;
gets(curr_line);
while (curr_line[0]!='\x0')
{
if (strcmp(prv_line,"\0")!=0)
{
if(strcmp(curr_line,prv_li ne)==0) counter++;
if(counter>=N)
{
printf("Anagram %s occured %i times\n",prv_line,counter) ;
counter=1;
prv_line[0]=curr_line[0]=' \x0';
}
}
strcpy(prv_line,curr_line) ;
gets(curr_line);
}
if (counter>=N)
printf("%s %i",curr_line,counter);
}
#include <stdio.h>
#include <string.h>
#define size 40
int convert(const char c)
{
return c-48;
}
void main (int argc, char *argv[])
{
int counter=1,N;
char curr_line[size],prv_line[s
if (argc==2 && (strlen(argv[1])==1) && (convert(*argv[1]) > 0))
N=convert(*argv[1]);
else
{
perror("Invalid Parameter");
exit(0);
}
printf("N = %i\nTo end type ""ENTER"" twice.\n",N);
gets(curr_line);
strcpy(prv_line,curr_line)
gets(curr_line);
while (curr_line[0]!='\x0')
{
if (strcmp(prv_line,"\0")!=0)
{
if(strcmp(curr_line,prv_li
if(counter>=N)
{
printf("Anagram %s occured %i times\n",prv_line,counter)
counter=1;
prv_line[0]=curr_line[0]='
}
}
strcpy(prv_line,curr_line)
gets(curr_line);
}
if (counter>=N)
printf("%s %i",curr_line,counter);
}
If argc==1, then no paramters were present. Don't forget that the program name is argv[0] and argc will always be at least one.
If you use one command line parm, argc will be two. So, in this case, N is left at its' original value of 4.
---
Instead of your copy func, why not use strcpy?
---
instead of:
if (strcmp(prv_line,"\0")!=0)
why not:
if (prv_line[0]=='\0')
---
The proper check for eof from gets is something like:
while (NULL!=gets())
---
All that aside, at what point to the errors occur?