(C++) String 1st NULL

Asked 2 years ago, Updated 2 years ago, 23 views

You are writing a program that removes the punctuation mark from each string by taking the string in spaces. In this process, if you change the punctuation code to NULL in a string like "computer" and remove it, the first character becomes NULL and the string itself is not output at all. People like you in "You" only come out like this. Is there a way to solve this?

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>

#define R 1001
using namespace std;

Char word[R]; // Variable that retrieves characters from a text file.
char WordDataSet[R][R]; // A two-dimensional array containing the received data.

int main() {

    FILE* f = fopen ("input.txt", "r"); // Accepts the file in read format. 

    If (f == NULL) printf("None file\n"); // Exception if file does not exist.
    inti = 0; // A variable that displays the index of received data.

    while (!feof(f)) {
        fscanf(f, "%s", WordDataSet[i]); // Receive a string from a text file.
        int size = strlen(WordDataSet[i]); // Find the length of the string in the text file.
        for (int k = 0; k < size; k++) {
            if (WordDataSet[i][k] >= 33 && WordDataSet[i][k] <= 47) {
                /*if (WordDataSet[i][k + 1] >= 44032 && WordDataSet[i][k + 1] <= 55203) {
                    for (int f = k; WordDataSet[i][f] == NULL; f++) {
                        WordDataSet[i][f] = WordDataSet[i][f + 1];
                    }
                }
                else*/ WordDataSet[i][k] = NULL;//Delete sentence code
            }
        }
        i++;
    }

    For (int j = 0; j < i; j++) { // Outputs a stored string.
        printf("%s\n", WordDataSet[j]);
    }

    return 0;
}

c++

2022-09-20 17:39

2 Answers

If you're just thinking about getting rid of text, you can do the following:

Replace for with while and remember the number of characters to skip by introducing a variable called d. If d is not 0, copy the characters in the k+d index to the k index location.


    while (!feof(f)) {
        fscanf(f, "%s", WordDataSet[i]); // Receive a string from a text file.
        int size = strlen(WordDataSet[i]); // Find the length of the string in the text file.
        int d = 0;
        int k = 0;
        while (1) {
            while (WordDataSet[i][k+d] >= 33 && WordDataSet[i][k+d] <= 47) {
                d++;
            }
            if(d) {
                WordDataSet[i][k] = WordDataSet[i][k+d];
            }
            if(WordDataSet[i][k+d] == 0) break;
            k++;
        }
        i++;
    }

It's the same thing, but using a pointer.


    while (!feof(f)) {
        fscanf(f, "%s", WordDataSet[i]); // Receive a string from a text file.
        int size = strlen(WordDataSet[i]); // Find the length of the string in the text file.
        char *s;
        char *d;

        for( d=s=WordDataSet[i] ; ; s++, d++) {
            while(*s >= 33 && *s <= 47) {
                s++;
            }
            *d = *s;
            if(*s == 0) break;
        }
        i++;
    }


2022-09-20 17:39

I've modified the code!

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>

#define R 1001
using namespace std;

Char word[R]; // Variable that retrieves characters from a text file.
char WordDataSet[R][R]; // A two-dimensional array containing the received data.
string str;

int main() {

    FILE* fo = fopen("input.txt", "r");
    if (fo == NULL) printf("None file\n");
    charoword[100];//variable to receive text files as they are
    fgets(oword, 100, fo);
    fclose(fo);
    printf("----------------------------------\n\n%s\n", oord);//text file source output

    FILE* f = fopen ("input.txt", "r"); // Accepts the file in read format. 

    If (f == NULL) printf("None file\n"); // Exception if file does not exist.
    inti = 0; // A variable that displays the index of received data.

    while (!feof(f)) {
        fscanf(f, "%s", WordDataSet[i]); // Receive a string from a text file.
        int size = strlen(WordDataSet[i]); // Find the length of the string in the text file.
        for (int k = 0; k < size; k++) {
            if (WordDataSet[i][k] >= 33 && WordDataSet[i][k] <= 47) {
                str = WordDataSet[i];
                str.erase(k, 1);
                (string)WordDataSet[i] = str;
            }
            else if (WordDataSet[i][k] == 63) {
                str = WordDataSet[i];
                str.erase(k, 1);
                (string)WordDataSet[i] = str;
            }
        }
        i++;
    }
    printf("-----------------------------------------------------\n");
    For (int j = 0; j < i; j++) { // Outputs a stored string.
        printf("%s\n", WordDataSet[j]);
    }

    return 0;
}

But when I changed it like this, the punctuation marks came out exactly the same. Is there a problem? When I touched the part that was annotated above a little more, the letters came out completely broken, so I changed it like this. String is easy to erase certain characters as a function.


2022-09-20 17:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.