Solved

segmentation error

Posted on 2000-02-26
5
291 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 84

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 200 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I recover data from my iphone 3g 4 45
Why this code doesn't work? 8 93
FILE closing 11 68
why "." vs "->" 23 116
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now