473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

input a string in gcc

following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int main (void)
  6. {
  7. char str[50];
  8. printf ("Enter string:\n");
  9. fgets (str, sizeof str, stdin);
  10. //further code
  11. }
  12.  
if anybody can help me enter the string, thanx!
Jun 27 '08 #1
21 3905
In article <e5**********************************@f24g2000prh. googlegroups.com>,
yogicoder <za*****@gmail.comwrote:
>following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.
>#include <stdio.h>
#include <string.h>
#include <stdlib.h>
>int main (void)
{
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);
That part looks okay to me.
//further code
}
You are not returning a value from main(), which leads to undefined
behaviour in C89/C90.

But I suspect the error is in the "//further code" part.
--
"Allegories are in the realm of thoughts, what ruins are in
the realm of things." -- Walter Benjamin
Jun 27 '08 #2
On Sun, 08 Jun 2008 07:38:48 +0000, Walter Roberson wrote:
In article
<e5**********************************@f24g2000prh. googlegroups.com>,
yogicoder <za*****@gmail.comwrote:
>>following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.
>>#include <stdio.h>
#include <string.h>
#include <stdlib.h>
>>int main (void)
{
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);

That part looks okay to me.
> //further code
}

You are not returning a value from main(),
If you treat //further code as just a comment, then you already know this
is not a C90 program. If you treat it as an indicator that there is more
code, that code may very well return a value from main().
which leads to undefined
behaviour in C89/C90.
No, it doesn't. It leads to an undefined return value, but not undefined
behaviour, in C90.
But I suspect the error is in the "//further code" part.
Agreed.
Jun 27 '08 #3
yogicoder <za*****@gmail.comwrites:
following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.
The error must be in the // further code part, as others have pointed
out. Compile your code with the -g flag and use valgrind or gdb to
see the offending line. After that try to fix it and, if you can't
(but TRY first) ask for help again.
--

- Filipe Cabecinhas

char* params[] = {"fil", "cab", "@", "gmail", ".", "com"};
puts("Real signature:");
for (int i = 0; i < sizeof(params)/sizeof(*params); ++i)
printf("%s", params[i]);
putchar('
');
Jun 27 '08 #4
In article <e5**********************************@f24g2000prh. googlegroups.com>,
yogicoder <za*****@gmail.comwrote:
>following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.
Does this very program get a segmentation fault, or only when you
put in the "further code"?

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Jun 27 '08 #5

"yogicoder" <za*****@gmail.comwrote in message
news:e5**********************************@f24g2000 prh.googlegroups.com...
following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.
How soon exactly? Enough time for your computer to execute a hundred million
lines of code following the fgets perhaps?

If you put in a printf or puts statement just before your '//further code'
you can establish whether the problem is with the fgets, or with the
'further code'.

--
Bartc
Jun 27 '08 #6
yogicoder <za*****@gmail.comwrites:
following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. int main (void)
  5. {
  6.     char str[50];
  7.     printf ("Enter string:\n");
  8.     fgets (str, sizeof str, stdin);
  9.         //further code
  10. }
  11.  

if anybody can help me enter the string, thanx!
use a debugger and find where it happens.
Jun 27 '08 #7
On Jun 8, 4:10 pm, "Bartc" <b...@freeuk.comwrote:
"yogicoder" <zape...@gmail.comwrote in message

news:e5**********************************@f24g2000 prh.googlegroups.com...
following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.

How soon exactly? Enough time for your computer to execute a hundred million
lines of code following the fgets perhaps?

If you put in a printf or puts statement just before your '//further code'
you can establish whether the problem is with the fgets, or with the
'further code'.

--
Bartc
Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.
Jun 27 '08 #8
yogicoder wrote:
On Jun 8, 4:10 pm, "Bartc" <b...@freeuk.comwrote:
>"yogicoder" <zape...@gmail.comwrote in message

news:e5**********************************@f24g200 0prh.googlegroups.com...
>>following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.
How soon exactly? Enough time for your computer to execute a hundred million
lines of code following the fgets perhaps?

If you put in a printf or puts statement just before your '//further code'
you can establish whether the problem is with the fgets, or with the
'further code'.

--
Bartc

Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.
There must be something else going on. The following works fine here.

#include <stdio.h>

int main(void)
{
char str[50];
printf("Enter string:\n");
fgets(str, sizeof str, stdin);
fputs(str, stdout);
return 0;
}
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 27 '08 #9

"yogicoder" <za*****@gmail.comwrote in message
news:c4**********************************@u6g2000p rc.googlegroups.com...
On Jun 8, 4:10 pm, "Bartc" <b...@freeuk.comwrote:
>"yogicoder" <zape...@gmail.comwrote in message

news:e5**********************************@f24g200 0prh.googlegroups.com...
following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.

How soon exactly? Enough time for your computer to execute a hundred
million
lines of code following the fgets perhaps?

If you put in a printf or puts statement just before your '//further
code'
you can establish whether the problem is with the fgets, or with the
'further code'.

--
Bartc

Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.
Thanks, that's useful clarification. So in this program (I've added \n to
the printf):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);
printf("test\n");
}

after you hit Enter (for test purposes, don't type anything else), it does
not print "test"? I'm asking because on my machine (and I suspect many
others) it works as expected.

What happens if you comment out the fgets line?

--
Bartc
Jun 27 '08 #10
"Bartc" <bc@freeuk.comwrites:
"yogicoder" <za*****@gmail.comwrote in message
news:c4**********************************@u6g2000p rc.googlegroups.com...
>On Jun 8, 4:10 pm, "Bartc" <b...@freeuk.comwrote:
>>"yogicoder" <zape...@gmail.comwrote in message

news:e5**********************************@f24g20 00prh.googlegroups.com...

following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the 'enter' key.

How soon exactly? Enough time for your computer to execute a hundred
million
lines of code following the fgets perhaps?

If you put in a printf or puts statement just before your '//further
code'
you can establish whether the problem is with the fgets, or with the
'further code'.

--
Bartc

Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.

Thanks, that's useful clarification. So in this program (I've added \n to
the printf):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);
printf("test\n");
}

after you hit Enter (for test purposes, don't type anything else), it does
not print "test"? I'm asking because on my machine (and I suspect many
others) it works as expected.

What happens if you comment out the fgets line?
use a debugger. get a backtrace.

Jun 27 '08 #11
yogicoder <za*****@gmail.comwrites:
Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.
Install valgrind, run your program with: valgrind ./prog
then show us the errors.

--

- Filipe Cabecinhas

char* params[] = {"fil", "cab", "@", "gmail", ".", "com"};
puts("Real signature:");
for (int i = 0; i < sizeof(params)/sizeof(*params); ++i)
printf("%s", params[i]);
putchar('\n');
Jun 27 '08 #12
In article <c4**********************************@u6g2000prc.g ooglegroups.com>,
yogicoder <za*****@gmail.comwrote:
>Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.
When you're asking other people to help fix your code, it's not a good
idea to rely on what *you* think is important. After all, if you knew
what was important you'd be able to find the bug yourself.

How do you know the printf("test") statement is not being executed?
Do you really mean that "test" isn't being printed? Normally terminal
output is line-buffered, so it won't get printed until you output
a newline.

If you want help, send a complete program that fails, not a cut-down
program that you haven't tested.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Jun 27 '08 #13
Richard Tobin wrote:
In article <c4**********************************@u6g2000prc.g ooglegroups.com>,
yogicoder <za*****@gmail.comwrote:
>Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.

When you're asking other people to help fix your code, it's not a good
idea to rely on what *you* think is important. After all, if you knew
what was important you'd be able to find the bug yourself.
Anytime somebody introduces a debugging problem
by saying where the problem isn't,
that's where I look first.

--
pete
Jun 27 '08 #14
On Jun 8, 7:17 pm, pete <pfil...@mindspring.comwrote:
Richard Tobin wrote:
In article <c4d253ec-5885-48b8-a6f3-b992f9010...@u6g2000prc.googlegroups.com>,
yogicoder <zape...@gmail.comwrote:
Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.
When you're asking other people to help fix your code, it's not a good
idea to rely on what *you* think is important. After all, if you knew
what was important you'd be able to find the bug yourself.

Anytime somebody introduces a debugging problem
by saying where the problem isn't,
that's where I look first.

--
pete
well yes people you all were right. There is some error in the code
after fgets. What was happening was that the printf immediately after
fgets was also not getting executed because of some error further away
in my code. This was my first post so please don't castigate me for
this.
So here's a detailed explanation:
Basically what my code is supposed to do is accept a string from user
& reverse the position of the words, for e.g.
Input: this is a string
Output: string a is this
I have to do this in O(n) complexity.
Here's my code :
[code]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{

void change (char []);
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);
change (str);
printf ("\nRequired string is:\n");
fputs (str, stdout);
return 0;
}

void change (char str[50])
{
char *arr[20], *a;
int i, j, k;
i = j = 0;
while (str[j] != '\n') {
*arr[i] = str[j];
if (str[j] == ' ')
++i;
str[++j];
arr[i]++;
}
*arr[i] = '\0';
k = j = i;
i = 0;
while (i < j) {
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
++i;
--j;
}
i = j = 0;
while (*arr[i] != '\0') {
str[j] = *arr[i];
if (*arr[i] == ' ')
++i;
str[++j];
arr[i]++;
}
str[j] = *arr[i];
return;
}

There's some error with pointers i know, but i am not able to
understand it. You guys have really been helpful so thanks a lot! I've
spent a lot of time on this so help would be greatly appreciated.
Jun 27 '08 #15
yogicoder <za*****@gmail.comwrites:
On Jun 8, 7:17 pm, pete <pfil...@mindspring.comwrote:
>Richard Tobin wrote:
In article <c4d253ec-5885-48b8-a6f3-b992f9010...@u6g2000prc.googlegroups.com>,
yogicoder <zape...@gmail.comwrote:
>Well i didn't consider further code important because i had already
used a printf("test") statement immediately after fgets & it's not
getting executed. I get seg fault eror the instant i hit the enter
key.
When you're asking other people to help fix your code, it's not a good
idea to rely on what *you* think is important. After all, if you knew
what was important you'd be able to find the bug yourself.

Anytime somebody introduces a debugging problem
by saying where the problem isn't,
that's where I look first.

--
pete

well yes people you all were right. There is some error in the code
after fgets. What was happening was that the printf immediately after
fgets was also not getting executed because of some error further away
in my code. This was my first post so please don't castigate me for
this.
So here's a detailed explanation:
Basically what my code is supposed to do is accept a string from user
& reverse the position of the words, for e.g.
Input: this is a string
Output: string a is this
I have to do this in O(n) complexity.
Here's my code :
[code]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{

void change (char []);
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);
change (str);
printf ("\nRequired string is:\n");
fputs (str, stdout);
return 0;
}

void change (char str[50])
{
char *arr[20], *a;
int i, j, k;
i = j = 0;
while (str[j] != '\n') {
*arr[i] = str[j];
if (str[j] == ' ')
++i;
str[++j];
arr[i]++;
}
*arr[i] = '\0';
k = j = i;
i = 0;
while (i < j) {
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
++i;
--j;
}
i = j = 0;
while (*arr[i] != '\0') {
str[j] = *arr[i];
if (*arr[i] == ' ')
++i;
str[++j];
arr[i]++;
}
str[j] = *arr[i];
return;
}

There's some error with pointers i know, but i am not able to
understand it. You guys have really been helpful so thanks a lot! I've
spent a lot of time on this so help would be greatly appreciated.
Run your code in gdb or similar. It will then show you the line of the
error. It really is that simple and you will learn a lot by stepping
through the code and examining your variables.
Jun 27 '08 #16
yogicoder wrote, On 08/06/08 15:46:

<snip>
well yes people you all were right. There is some error in the code
after fgets. What was happening was that the printf immediately after
fgets was also not getting executed because of some error further away
in my code. This was my first post so please don't castigate me for
this.
So here's a detailed explanation:
Basically what my code is supposed to do is accept a string from user
& reverse the position of the words, for e.g.
Input: this is a string
Output: string a is this
I have to do this in O(n) complexity.
Here's my code :
[code]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{

void change (char []);
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);
fgets returns a value for a reason. You should check it.
change (str);
printf ("\nRequired string is:\n");
fputs (str, stdout);
return 0;
}

void change (char str[50])
You should be aware that this does not mean the function takes an array.
It is the same as
void change(char *str)
{
char *arr[20], *a;
Where are your checks that you don't write beyond element arr[19]?
Sensible names would also help.
int i, j, k;
i = j = 0;
while (str[j] != '\n') {
What if there is no new line in the string? There are situations where
you won't have one.
*arr[i] = str[j];
Urm, where does arr[i] point to? Hint, you have not assigned anything to
it yet.
if (str[j] == ' ')
++i;
str[++j];
What is the above meant to achieve? All it will actually achieve is
incrementing j.

<snip>
There's some error with pointers i know, but i am not able to
understand it. You guys have really been helpful so thanks a lot! I've
spent a lot of time on this so help would be greatly appreciated.
Well, I've given you a few hints, see if you can do any better now.
--
Flash Gordon
Jun 27 '08 #17
yogicoder <za*****@gmail.comwrote:
On Jun 8, 7:17 pm, pete <pfil...@mindspring.comwrote:
well yes people you all were right. There is some error in the code
after fgets. What was happening was that the printf immediately after
fgets was also not getting executed because of some error further away
in my code.
No, you told us that you put in there

printf( "Test" );

and, if you have a closer look you will see that there's no
'\n' at the end of the string you print. But per default the
stdout stream is line buffered, i.e. when there's no '\n'
it doesn't get passed on to the console but stays in the
internal buffer of printf() until a '\n' is found later (or
until that buffer gets full). To remedy the situation either
do the obvious, i.e. add a '\n' or call 'fflush( stdout );'
to force the buffer to get flushed (or print to stderr which
per default isn't buffered).
So here's a detailed explanation:
Basically what my code is supposed to do is accept a string from user
& reverse the position of the words, for e.g.
Input: this is a string
Output: string a is this
I have to do this in O(n) complexity.
Here's my code :
[code]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void)
{
void change (char []);
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);
change (str);
printf ("\nRequired string is:\n");
fputs (str, stdout);
return 0;
}
void change (char str[50])
{
char *arr[20], *a;
int i, j, k;
i = j = 0;
while (str[j] != '\n') {
*arr[i] = str[j];
Here you got a serious problem. arr is an array of 20 pointers
to char. And all the elements of this array aren't initialized,
i.e. they point to ramdom places in memory. And here you now
try to write a char into such a location, which is a perfect
recipe for getting a segmentation fault.

Think of pointers like of checks - if they aren't backed by
money in the bank (point to memory you own) you're in a lot
of trouble.

Perhaps you meant to write

arr[ i ] = str + j;

That would look more reasonable since that would set the
i-th element of arr to point to the j-th char in str.
if (str[j] == ' ')
++i;
str[++j];
That's a rather strange way of writing '++j;' since all
this is doing is incrementing 'j'.
arr[i]++;
}
I guess the whole purpose of the previous part is to built up
list of pointers to the starts of words in the string. Is
that about correct?
*arr[i] = '\0';
k = j = i;
i = 0;
while (i < j) {
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
++i;
--j;
}
This looks as if you want to reverse the arr array.
i = j = 0;
while (*arr[i] != '\0') {
str[j] = *arr[i];
If my above assumptions are correct then you again get into
trouble here (even if all the bugs from above would be corrected).
If arr is an array of pointers, pointing into str, then changing
str from the pointers in arr will mess up str completely. You
can't split up a string like that into pieces and rearrange the
pieces. Just take a piece of paper, write down the string and
indicate by arrows where the elements of arr point to. Then
try to use your :algorithm" by hand, exchanging the letters as
you try to do here, and you will see why it can't work - as long
as you remember that the elements of arr just point into the
string and aren't copies of the words of the string. Just try
it with a very simple sentence like "I am here".

Don't try to use a debugger here. It's absolutely useless
as long as you don't fully understand what is going one
here and will only get you more confused.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #18
yogicoder <za*****@gmail.comwrites:
<snip>
Basically what my code is supposed to do is accept a string from user
& reverse the position of the words, for e.g.
Input: this is a string
Output: string a is this
I have to do this in O(n) complexity.
Nice problem.
Here's my code :
[code]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{

void change (char []);
change is not a very good name. By the way, some people don't like
having function prototypes anywhere but at file scope. I don't mind.
Check what style you instructor expects.
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);
change (str);
printf ("\nRequired string is:\n");
fputs (str, stdout);
return 0;
}

void change (char str[50])
Your know the 50 has no effect, right?
{
char *arr[20], *a;
All of the 20 pointers in arr are left uninitialised. They don't
point anywhere valid.
int i, j, k;
i = j = 0;
while (str[j] != '\n') {
*arr[i] = str[j];
and here you use of them. Immediate "boom". I think you probably
intended something quite different, but I can't tell. You can put a
pointer into one quote safely, e.g. arr[i] = str, but you can
de-reference arr[i] with a * until it points somewhere.
if (str[j] == ' ')
++i;
str[++j];
arr[i]++;
}
*arr[i] = '\0';
k = j = i;
i = 0;
while (i < j) {
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
++i;
--j;
Hmmm... You need to do some more planning first. Here you are
swapping pointers around. I don't think that will help much.

If your plan is to point to the start of each word with arr[x], you
don't need to reverse the sequence of these in the array. Just access
it from the end rather than the start.
}
i = j = 0;
while (*arr[i] != '\0') {
str[j] = *arr[i];
if (*arr[i] == ' ')
++i;
str[++j];
arr[i]++;
}
str[j] = *arr[i];
return;
}
General points: It would be better if the function did not assume
there was a newline at the end. I would go for a solution that did
not have another fixed size in it (the 20 elements of arr). The test
for spaces is probably better done with isspace (from ctype.h) since
that includes other word-separators.

--
Ben.
Jun 27 '08 #19
yogicoder wrote:
) Well i didn't consider further code important because i had already
) used a printf("test") statement immediately after fgets & it's not
) getting executed. I get seg fault eror the instant i hit the enter
) key.

Try putting this instead:
fprintf(STDERR, "test\n");

(Or at least flush stdout after printing.)
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Jun 27 '08 #20

"yogicoder" <za*****@gmail.comwrote in message
news:52**********************************@w5g2000p rd.googlegroups.com...
well yes people you all were right. There is some error in the code
after fgets. What was happening was that the printf immediately after
fgets was also not getting executed because of some error further away
in my code. This was my first post so please don't castigate me for
this.
You didn't mention the call to change() between fgets() and printf()...
So here's a detailed explanation:
Basically what my code is supposed to do is accept a string from user
& reverse the position of the words, for e.g.

There are some problems with this code as others have said. Among other
things, limiting to only 20 words (a 50 character string could have up to 24
or so).

I put a version below that may or may not be helpful. There is no array of
pointers, but it does have to temporarily create a string the same size as
the input. But it has no other limits on string length or number of words.

Your input string has a \n at the end; where the string is unchanged (0 or 1
words) this \n stays.

The malloc allocation is a tight fit; an extra couple of bytes here won't
hurt.
/* reverse words in str in-place. separaters are spaces and tabs */
void change (char *str)
{
char *newstr;
char c, *p,*q, *r, *s;
int i,j,k,n,nwords;

if (str==NULL) return;
n=strlen(str);
if (n<=2) return; /* More than 2 words not possible, esp as last
char might be \n */

newstr=malloc(n+1); /* Build new string elsewhere */
if (newstr==NULL) return; /* Memory problem */

q=newstr; /* Point to start of dest string */
r=NULL; /* Point to last char of current word */
nwords=0;

for (p=str+n-1; p>=str; --p) {/* Scan source string starting from the end */
c=*p;
if (c==' ' || c=='\t' || c=='\n') {
if (r) { /* start of word encountered */
s=p+1;
for (i=0; i<(r-p); ++i) *q++ = *s++;
*q++=' ';
*q=0;
r=NULL;
++nwords;
}
}
else /* Assume c is part of word */
if (r==NULL) r=p; /* Remember last char of this word */
}

if (nwords) {
if (r) {
s=str;
for (i=0; i<(r-str+1); ++i) *q++ = *s++;
*q=0;
}
strcpy(str,newstr);
};

free(newstr);
return;
}

Jun 27 '08 #21
On 8 Jun, 17:39, Willem <wil...@stack.nlwrote:
yogicoder wrote:
) Well i didn't consider further code important because i had already
) used a printf("test") statement immediately after fgets & it's not
) getting executed. I get seg fault eror the instant i hit the enter
) key.

Try putting this instead:
*fprintf(STDERR, "test\n");
fprintf (stderr, "test\n");

--
Nick Keighley
Jun 27 '08 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gregg | last post by:
Hello all, I have been banging my head over a problem that I am having reading a comma seperated file (CSV) that can contain from 1 to 10,000 records. My code snipit is as follows: **Start...
3
by: edgekaos | last post by:
Is method 2 valid? Method 1: wstring input = L"STRING"; wstring output = input; transform(output.begin(), output.end(), output.begin(), towupper); Method 2: wstring input = L"STRING";...
8
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
0
by: Anonieko | last post by:
Are there any javascript codes there? Answer: Yes On the PageLoad event call InitialClientControsl as follows /// <summary> /// This will add client-side event handlers for most of the...
4
by: priyanka | last post by:
Hi, I want to input a string from command line. I use the following program to input the string. #include<stdio.h> int main(){ char input;
2
by: Killer42 | last post by:
The Input #1 statement simply reads in one line from a text file (in this case you INI file) and places the values from it into one or more variables. So what you are reading in this statement is...
13
by: cront | last post by:
I have a problem to work on: we will ask user to input anything and we will put that back onto the standard output with all set of brackets removed. We will not remove any single bracket e.g. ...
4
by: Newbie | last post by:
Hello I need to enter a string of the form abc (a string of characters followed by EOF) #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 100 int main(void) {
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
9
by: arnuld | last post by:
Earlier, I have posted a program like this, a month ago IIRC. I have created it again, without looking at the old program. Can I have your opinions on this: 1) I wanted my program to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.