I am trying to print each word in a single line of a given sentence. It worked perfectly fine but a '_' appears in end of line. please help me with it and also proper manar to write it.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char *s,i,check=0;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) 1);
for(i=0;i<1024;i ||check<=2)
{
if(*(s i)!=' ')
{
printf("%c",*(s i));
check=0;
}
else
{
printf("\n");
check ;
}
// fflush(stdin);
}
return 0;
}
Output: dkf fja fjlak d dkf fja fjlak d SER_
Output2: -for(i=0;i<20;i ||check<=2)- hello I am suraj Ghimire hello I am suraj Ghi
CodePudding user response:
I am not sure your code works as you say..
- The type of
i
is not achar *
, so it should be int. - You process the input string without considering the NULL terminating char, which leads to a lot of garbage prints.
- You do not release allocated memory.
I suggest this slightly modified version:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char *s, *p;
/* Allocate a new string and verify the allocation has succeeded. */
s = malloc(1024 * sizeof(char));
if (!s) {
printf("malloc failed\n");
return 1;
}
/* Read from user. */
scanf("%[^\n]", s);
/* Work on a copy of `s` (must simpler and faster than a indexed access). */
p = s;
while (*p) {
if (*p != ' ') {
printf("%c",*p);
}else{
printf("\n");
}
p ;
}
free(s);
return 0;
}
Example output:
$ ./a.out
abc def gh i j kmlm opqrst
abc
def
gh
i
j
kmlm
opqrst
CodePudding user response:
There are a couple issues with your code.
Firstly, you need to check that the for
loop does not exceed the bounds of the string.
Your for
loop is always set to true
because the logical OR operator ||
has a higher precedence than the comma operator. Because of this the loop will always run unless it gets stopped with break
Lastly your check
is never reset to 0 after it reaches a value of 2.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char *s,i,check=0;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) 1);
for(i=0; i<strlen(s); i ) {
if(*(s i) != ' ') {
printf("%c",*(s i));
check=0;
} else {
printf("\n");
check ;
if (check > 2) break;
}
}
return 0;
}
Output:
Hello, this is a test
Hello,
this
is
a
test
CodePudding user response:
for(i=0;i<1024;i ||check<=2)
There are two issues. One is length of string won't always be 1024, so it might be good to determine the length of string before print the string. The other is check<=2
, which have to put in the second part of the for loop, so the test will be evaluated. Also it is better to calculate the length of string once. So I store the length of string in len
.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *s, i, check = 0;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) 1);
size_t len = strlen(s);
for (i = 0; i < len || check <= 2; i ) {
if (*(s i) != ' ') {
printf("%c", *(s i));
check = 0;
} else {
printf("\n");
check ;
}
// fflush(stdin);
}
return 0;
}