Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

segmentation error

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Picking random number 8 178
Where is my core dump file in Ubuntu? 12 453
why "." vs "->" 23 120
change colour of repeater control in asp.net c# 7 111
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 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 for-loops in the C programming language.

808 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