打印返回的char *值

时间:2019-12-16 11:16:39

标签: c string char word

我有这段代码,该函数必须返回没有某些符号的给定字符串。问题是我在打印badWords函数的返回值时遇到问题。打印“ ????”我也不知道。

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

char * badWord(int, char*);

int main(int argc, char **argv){
    char s[100]="O joao` foi as compra's, comprou um umb'igo! falante por £233";
    int n = sizeof(s);

    char* word = badWord(n, s);
    printf("%s\n", word);
}

char * badWord(int n, char s[n]){
    char palavra[n];
    int index=0;
    for(int i=0; i<n; i++){
        char c = s[i];
        char c2 = s[i+1];

        if(isalnum(c) || (ispunct(c) && isalpha(c2))){
            palavra[index]=c;
            index++;
        }
    }
    return palavra;
}

1 个答案:

答案 0 :(得分:0)

解决了!阅读评论,我将本地数组“ palavra”转换为全局数组,并且成功了!

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

char * badWord(int, char*);

int main(int argc, char **argv){
    char s[100]="O joao` foi as compra's, comprou um umb'igo! falante por £233";
    int n = sizeof(s);

    char* word = badWord(n, s);
    printf("%s\n", word);
}
char palavra[10000000];
char * badWord(int n, char s[n]){
    int index=0;
    for(int i=0; i<n; i++){
        char c = s[i];
        char c2 = s[i+1];

        if(isalnum(c) || (ispunct(c) && isalpha(c2))){
            palavra[index]=c;
            index++;
        }
    }
    return palavra;
}