LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Fedora (https://www.linuxquestions.org/questions/fedora-35/)
-   -   Problems with program (https://www.linuxquestions.org/questions/fedora-35/problems-with-program-370525/)

dludenar 10-07-2005 01:51 AM

Problems with program
 
Hello people

I'm starting programming in C, I am studying from this book "A book on C" of Al Kelley & Ira Pohl, but I have one problems with the following program:

#include <stdio.h>
#define MAXWORD 100

main ()

{
char *find_next(), word[MAXWORD];
int char_cnt = 0, word_cnt = 0, word_lenght = 0;

while (find_next(word) != NULL)
{
capitalize(word);
++word_cnt;
word_lenght = strlen(word);
char_cnt += word_lenght;
printf ("\n%12d %s", word_lenght, word);
}
printf ("\n\n%12d characters in %d words\n\n",
char_cnt, word_cnt);
}

char *find_next(word)
char word[];
{
int c, i;

while ((c = getchar()) == ' ' || c == '\n' || '\t')
; /* skip white space */
if (c != EOF)
{
i = 0;
while (c != ' ' && c != '\n' && c != '\t' && c != EOF)
{
word[i++] = c;
c = getchar ();
}
word[i] = '\0';
return (word);
}
else
return (NULL);
}

capitalize(p)
char *p;
{
for ( ; *p != '\0'; ++p)
if ('a' <= *p && *p <= 'z')
*p += 'A' - 'a';
}

When you input the following text "she sells sea shells by the seashore", at the moment of excecute the program, the result must be:

3 SHE
5 SELLS
3 SEA
6 SHELLS
2 BY
3 THE
8 SEASHORE

When I compilled there's no problem, no errors no warnings, but when I excecute and introduce the text it doesn't work, I hope you could help me, I am using Fedora Core 3.

Thanks

dludenar 10-07-2005 01:55 AM

Error when excecute program
 
Hello people

I'm beggining programming in C, well now I have problems with the following program:

#include <stdio.h>

main()

{
int c, i, letter[26];
FILE *fp, *fopen();

fp = fopen("chapter1", "r");
for (i = 0; i < 26; ++i) /* initialize array to cero */
letter[i] = 0;
while ((c = getc(fp)) != EOF)
if ('A' <= c && c <= 'Z')
++letter [c - 'A'];
for (i = 0; i < 26; ++i)
{
if (i % 6 == 0)
printf ("\n");
printf ("%5c:%5d", 'A' + 1, letter[i]);
}
printf ("\n\n");
}

When I compilled it no erros, no warnings, but when I excecuted, says: "Segmentation fault"

Can anyone help me to solve this problem??? I hope so.

Regards

dludenar

acid_kewpie 10-07-2005 03:18 AM

Please do not post the same thread in more than one forum. Picking the most relevant forum and posting it once there makes it easier for other members to help you and keeps the discussion all in one place.

http://www.linuxquestions.org/rules.php

XavierP 10-07-2005 06:06 AM

Closed.


All times are GMT -5. The time now is 02:25 PM.