My current code outputs an extra dot and fails a test case I'm trying to pass:
I realize that having extra input in my output implies that I didn't put the closing '\0' in the correct location. Here's my code:
else if (isdigit(c))
{
value[0] = (char)c;
c = fgetc(input);
int count = 0;
int index = 1;
bool isRealNumber = false;
while (isdigit(c) || c == '.')
{
if (c == '.')
{
isRealNumber = true;
c = fgetc(input);
if (c == '.')
{
value[index] = '.';
index++;
value[index + 1] = '\0';
c = ungetc(c, input);
break;
}
else
{
value[index] = '.';
index++;
value[index + 1] = '\0';
}
}
value[index] = (char)c;
c = fgetc(input);
count += 1;
index += 1;
}
c = ungetc(c, input);
if (isRealNumber)
T.id = SQL_REAL_LITERAL;
else
T.id = SQL_INT_LITERAL;
T.line = *lineNumber;
T.col = *colNumber;
*colNumber = *colNumber + count + 1;
value[index] = '\0';
return T;
}
I've tried setting my null value ('\0') as one before but it failed to fix anything.
Revealed after indenting your code: In case of two consecutive dots you have two ungetc(). The standard guarantees for "unget" only one character, and your system seems to follow this.
Related
I am working on a question which requires me to print a string given a field-number at that position. The strings should be read from a file.
file.txt
C is a language.
lex lexical analyser
(blank line)
gcc is good
If the field-number is 2 (i.e the second word in the sentence). The program should output
is
lexical
(NULL)
is
I wrote a function but don't think its the correct way and that it would work for all cases. It should handle extra blanks or newlines.
while (fgets(buffer, MAX, file) != NULL) {
for (int i = 1; i < strlen(buffer); i++) {
if (count == field_number - 1) {
int j = i;
while (j < strlen(buffer) && buffer[j] != ' ') {
printf("%c", buffer[j++]);
}
printf("\n");
count = 0;
break;
}
if (buffer[i] == ' ' && buffer[i - 1] != ' ') {
count++;
}
}
}
I am a beginner. This code should be easy to understand.
This should work for all the cases,
int main() {
//FILE* file = fopen(__FILE__, "r");
//int field_number = 2;
int new_line = 0; // var to keep track of new line came or not
int word = 0;
int count = 0;
char c, prev_c;
while ((c = fgetc(file)) != EOF) {
// printf("[%c]", c);
// if a new line char comes it means you entered a new line
if(c == '\n') {
// you have to print the new line here on the output to handle
// empty line cases
printf("\n");
new_line = 1; // when line changes
word = 0; // no word has come in this new line so far
count = 0; // count becomes 0
} else if( c == ' ' && prev_c != ' ') {
if(word)
count++;
if(count == field_number) // if count exceeds field_number
new_line = 0; // wait till next line comes
} else if (new_line && count == field_number - 1) {
printf("%c", c);
} else {
word = 1; // fi a not new line or non space char comes, a word has come
}
prev_c = c;
}
return 0;
}
I'm stuck with an unidentified Segmentation Fault.
My erroneous function receives a string text. It should convert it into a document and return it. A document is made of paragraphs(separated by '\n') which is made of sentences(separated by '.') which is made of words(separated by ' '). You may refer the complete problem statement here.
Here is the relevant part of my code:
char**** get_document(char* text) {
int p = 0, s = 0, w = 0, c = 0;
char**** document;
document = malloc(sizeof(char***));
document[0] = malloc(sizeof(char**));
document[0][0] = malloc(sizeof(char*));
document[0][0][0] = malloc(sizeof(char));
while (*text)
{
if (*text == ' ')
{
c = 0;
++w;
document[p][s] = realloc(document[p][s], sizeof(char*) * (w + 1));
}
else if (*text == '.')
{
c = 0;
w = 0;
++s;
document[p] = realloc(document[p], sizeof(char**) * (s + 1));
}
else if (*text == '\n')
{
c = 0;
w = 0;
s = 0;
++p;
document = realloc(document, sizeof(char***) * (p + 1));
}
else
{
++c;
document[p][s][w] = realloc(document[p][s][w], sizeof(char) * (c + 1));
document[p][s][w][c - 1] = *text;
document[p][s][w][c] = '\0';
}
++text;
}
return document;
}
After debugging, I came to know that the program crashes when
w = 1 at document[p][s][w][c - 1] = *text;
I have no idea why this is happening. I checked the values of p, s, w, and c before the execution of that statement, and if the realloc statements were executing properly.
But in vain!
What might be going wrong in my code?
You need to allocate memory for the new paragraphs, sentences and words. By reallocation you increased the actual dimension size, but the new element was a null pointer which caused the segfault.
char**** get_document(char* text) {
int p = 0, s = 0, w = 0, c = 0;
char**** document;
document = malloc(sizeof(char***));
document[0] = malloc(sizeof(char**));
document[0][0] = malloc(sizeof(char*));
document[0][0][0] = malloc(sizeof(char));
while (*text)
{
if (*text == ' ')
{
c = 0;
++w;
document[p][s] = realloc(document[p][s], sizeof(char**) * (w + 1));
document[p][s][w] = malloc(sizeof(char*));
}
else if (*text == '.')
{
c = 0;
w = 0;
++s;
document[p] = realloc(document[p], sizeof(char**) * (s + 1));
document[p][s] = malloc(sizeof(char**));
document[p][s][w] = malloc(sizeof(char*));
}
else if (*text == '\n')
{
c = 0;
w = 0;
s = 0;
++p;
document = realloc(document, sizeof(char****) * (p + 1));
document[p] = malloc(sizeof(char***));
document[p][s] = malloc(sizeof(char**));
document[p][s][w] = malloc(sizeof(char*));
}
else
{
++c;
document[p][s][w] = realloc(document[p][s][w], sizeof(char) * (c + 1));
document[p][s][w][c - 1] = *text;
document[p][s][w][c] = '\0';
}
++text;
}
return document;
}
Moreover, your printing method in the main does not work, because you did not save the spaces (you needn't anyways). So, I fixed it:
int main()
{
char* text = "New word.No space before a sentence.\nThis is a new paragraph.";
char**** doc = get_document(text);
int p = 0, s = 0, w = 0, c = 0;
char ch;
while (ch = *text)
{
if (ch == ' ')
{
putchar(' ');
c = 0;
++w;
}
else if (ch == '.')
{
putchar('.');
c = 0;
w = 0;
++s;
}
else if (ch == '\n')
{
putchar('\n');
c = 0;
w = 0;
s = 0;
++p;
}
else putchar(doc[p][s][w][c++]);;
text++;
}
return 0;
}
The output seems correct:
New word.No space before a sentence.
This is a new paragraph.
I think you don't have to free the doc because you are returning from the main after it and Hackerrank will handle that if needed. But just note that you should take care of it otherwise.
The following program runs without printing anything on the screen, maybe because the loop goes over the null character. I can't understand why and how this happens, and how to fix it.
//program to find how many word in the text doesn't contain p char
#include<stdio.h>
#include<stdbool.h>
#define space ' '
void find_word(char s[]) {
bool wordfound = false;
int i, j = 0, word = 0;
i = 0;
while (s[i]) { //s[i]!='\0' does not
if (s[i] != 'p' && s[i + 1] != space) { //for the first word
wordfound = true;
word++;
}
wordfound = false;
if (s[i] == space && s[i + 1] != space) { //for more than one word in the text
for (j = i + 1; s[j] != space; j++)
if (s[j] != 'p' && s[j + 1] != space)
wordfound = true;
}
if (wordfound) {
word++;
}
wordfound = false;
i = j;
i++;
} //end while loop
printf("Number of words not contain p character%d\n\n", word);
}
int main(void) {
char s[] = {"pppp zzzz ppp ssss dfg sfsfdsf"};
find_word(s);
return 0;
}
There are a few problems with this code, but the main one is that inside the loop you assign j to i which causes the infinite loop as the while(s[i]) condition is never met. Why don't you try to make it simple, like so:
//program to find how many word in the text doesn't contain p char
#include<stdio.h>
#include<stdbool.h>
#define space ' '
void find_word(char s[]) {
bool is_in = false;
short words_count = 0, i = 0;
while (s[i]) {
if (s[i] == 'p') { // if this letter is a 'p', mark the word
is_in = true;
}
if (s[i] == space) { // if it's end of word
if (!is_in) { // check if 'p' is present and increase the count
words_count++;
}
is_in = false;
}
i++;
}
if (!is_in) { // check if the last word has 'p'
words_count++;
}
printf("no. of words without p is %d\n", words_count);
}
int main(void) {
char s[] = {"pppp zzzz ppp ssss dfg sfsfdsf"};
find_word(s);
return 0;
}
You appear to have your for-loop terminating condition set to be unsatisfiable given your input.
if (s[i] == space && s[i + 1] != space) { //for more than one word in the text
for (j = i + 1; s[j] != space; j++)
if (s[j] != 'p' && s[j + 1] != space)
wordfound = true;
}
Here you are checking for a leading space in your input string. If you find it you then increment your index checking until you reach another space. What if your string doesn't have a trailing space?
Instead try to have a second condition for null and space to terminate the loop:
if (s[i] == space && s[i + 1] != space) { //for more than one word in the text
for (j = i + 1; s[j] != '\0' && [j] != space; j++)
if (s[j] != 'p' && s[j + 1] != space)
wordfound = true;
}
And then you set:
wordfound = false;
i = j;
i++;
} //end while loop
This will keep re-setting your loop, I'm not clear on your reasoning for this but that will run your loop indefinitely.
If you make these edits your code terminates:
#include<stdio.h>
#include<stdbool.h>
#define space ' '
void find_word(char s[]) {
bool wordfound = false;
int i, j = 0, word = 0;
i = 0;
while (s[i]) { //s[i]!='\0' does not
if (s[i] != 'p' && s[i + 1] != space) { //for the first word
wordfound = true;
word++;
}
wordfound = false;
if (s[i] == space && s[i + 1] != space) { //for more than one word in the text
for (j = i + 1; s[j] && s[j] != space; j++)
if (s[j] != 'p' && s[j + 1] != space)
wordfound = true;
}
if (wordfound) {
word++;
}
wordfound = false;
i++;
} //end while loop
printf("Number of words not contain p character%d\n\n", word);
}
int main(void) {
char s[] = {"pppp zzzz ppp ssss dfg sfsfdsf"};
find_word(s);
return 0;
}
Output:
Number of words not contain p character24
Before reading this question please note that my question pertains to a school assignment. For this assignment the only function we are allowed to use is malloc(). Everything else must be done without the use of other libraries.
I'm calling a function ft_split_whitespaces. This function takes a string as input and "splits" it into words. Words are separated spaces, tabs and line breaks.
#include <stdio.h>
char **ft_split_whitespaces(char *str);
int main(void)
{
char *str = "what is";
char **test = ft_split_whitespaces(str);
}
With respect to the example above the result at each index should be
test[0] = "what"
test[1] = "is"
test[2] = NULL
However, I am only able to print the results of test[0]. All other indices do not get printed to display. Heres an example of some code that I assume should print the results of my function.
int i = 0;
while(test[i] != NULL)
{
printf("%s", test[i]);
i++;
}
When this portion of code is ran, only test[0] is printed to the output. I've been sitting here trying to debug my code for hours. If anyone has some spare time and doesn't mind looking over my code, I'd appreciate it tremendously. I have a feeling it may be an issue with how I'm using malloc, but I still cant figure it out.
#include <stdlib.h>
#include <stdio.h>
int count_whitespaces(char *str)
{
int space_count;
int i;
space_count = 0;
i = 0;
while(str[i] != '\0')
{
if(str[i] == ' ' || str[i] == 9 || str[i] == '\n')
{
if(str[i+1] != ' ' && str[i+1] != 9 && str[i+1] != '\n')
space_count++;
}
i++;
}
return (space_count);
}
int check_whitespace(char c)
{
if (c == ' ' || c == 9 || c == '\n' || c == '\0')
{
return (1);
}
else
return(0);
}
int count_characters(char *str, int i)
{
int char_count;
char_count = 0;
while(str[i])
{
if(str[i+1] != ' ' && str[i+1] != 9 && str[i+1] != '\n')
char_count++;
else
break;
i++;
}
return (char_count);
}
char **ft_split_whitespaces(char *str)
{
int i;
int j;
int k;
char **word;
int space;
i = 0;
j = 0;
k = 0;
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
while(str[i] != '\0')
{
if (check_whitespace(str[i]) == 1)
i++;
else
{
if((word[j] = malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL)
return (NULL);
while (check_whitespace(str[i]) == 0)
{
word[j][k] = str[i];
i++;
k++;
}
j++;
}
}
j++;
word[j] = NULL;
j = 0
return word;
}
You forgot to reset k. The outer while loop in ft_split_whitespaces should look like that
while (str[i] != '\0') {
if (check_whitespace(str[i]) == 1){
i++;
}
else {
if ((word[j] =
malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL){
return (NULL);
}
while (check_whitespace(str[i]) == 0) {
word[j][k] = str[i];
i++;
k++;
}
word[j][k] = '\0';
j++;
k = 0; // reset k
}
}
So I actually figured out what was going wrong with my code as I finished typing this question (thanks stack overflow!). I decided to post it anyways, because I thought it might be a good learning experience for coding newbies such as myself.
This is where my issue was occurring.
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
while(str[i] != '\0')
{
if (check_whitespace(str[i]) == 1)
i++;
else
{
if((word[j] = malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL)
return (NULL);
while (check_whitespace(str[i]) == 0)
{
word[j][k] = str[i];
i++;
k++;
}
j++;
}
}
I malloc'd my char **word outside of the while loop using the following code
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
And then within the actual while loop I malloc'd it again using
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
Using malloc multiple times on the same variable was causing all sorts of weird issues. So within the while loop I ended up using a new variable I declared as char *words. Here is that portion of the while loop with the correct code.
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
while(str[i] != '\0')
{
if (check_whitespace(str[i]) == 1)
i++;
else
{
words = malloc(sizeof(char) * (count_characters(str, i) + 1));
k = 0;
while (check_whitespace(str[i]) == 0)
{
words[k] = str[i];
i++;
k++;
}
words[k] = '\0';
word[j] = words;
j++;
}
}
I would like to ask, if it is possible to use stdin ended with EOF more than one time. I have something like that:
int ColumnCounter = 0;
int Space = 1;
long IterationCounter = 0;
do
{
while ((Char = getchar()) != EOF)
{
if ((Char != ' ') && (Space == 1))
{
ColumnCounter++;
Space = 0;
}
else if (Char == ' ')
{
Space = 1;
}
else if (Char == '\n' || Char == '\0')
{
putchar('\n');
Space = 0;
ColumnCounter = 1;
continue;
}
if (ColumnCounter == NumberOfCol)
{
putchar(Char);
}
}
ColumnCounter = 0;
Space = 1;
IterationCounter = NumberOfCol++;
IterationCounter++;}
while (IterationCounter < EndingNumberOfCol + 1);
Continue in commentar below.
Yes.Just as you've been doing it almost correctly! Use the following code:
int repeat=0; // Don't forget to initialize repeat
int Znak;
do
{
while ((Znak = getchar()) != EOF)
{ ... }
repeat++; //repeat not reapeat here
}
while (repeat<5); //loop until repeat is less than 5
EDIT: I see you've edited your code.So,Simply implement the above logic into your program.