Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

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[size]="\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
0
anemos
Asked:
anemos
1 Solution
 
cookreCommented:
Don't you want if(argc==2) instead of if(argc=1)?

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?
0
 
arnondCommented:
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.
0
 
ozoCommented:
Another problem could occur if you gets more than size characters, or if curr_line contains no ' '
0
 
sumant032199Commented:
This is a good working program I can offer to you.
0
 
sumant032199Commented:
#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[size]="\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_line)==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);
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now