[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

segmentation error

Posted on 2000-02-26
5
Medium Priority
?
302 Views
Last Modified: 2010-04-02
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
Comment
Question by:anemos
5 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 2561240
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
 
LVL 3

Expert Comment

by:arnond
ID: 2561603
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
 
LVL 85

Expert Comment

by:ozo
ID: 2561699
Another problem could occur if you gets more than size characters, or if curr_line contains no ' '
0
 
LVL 3

Accepted Solution

by:
sumant032199 earned 600 total points
ID: 2562286
This is a good working program I can offer to you.
0
 
LVL 3

Expert Comment

by:sumant032199
ID: 2562288
#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

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question