#24218: C 為何會記憶體區段錯誤


candy13222 (茶)

學校 : 國立暨南國際大學
編號 : 73925
來源 : [27.242.73.12]
最後登入時間 :
2022-07-25 01:20:20
c044. 10008 - What's Cryptanalysis -- UVa10008 | From: [1.165.151.118] | 發表日期 : 2021-01-28 16:56

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#define MAX 1000

void swap(char *a, char *b, int *c, int *d){

    int sw;

    char chsw;

        sw = *c;

        *c = *d;

        *d = sw;

        chsw = *a;

        *a = *b;

        *b = chsw;

}

 

int main(){

    int size, i, j, strsize;

    char str[MAX];

    while(scanf("%d", &size) == 1){

        char let[27] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ\0"};

        int count[26] = {0};

        for(i = 0; i <= size; ++i){

            fgets(str, MAX, stdin);

            strsize = strlen(str) - 1;

            for(j = 0; j < strsize; ++j){

                if(islower(str[j]) != 0)

                    str[j] = toupper(str[j]);

                count[str[j] - 65]++;

            }

        }

        for(i = 0; i < 25; i++){

            for(j = 0; j < 25-i; ++j){

                if(count[j] < count[j+1])

                    swap(&let[j], &let[j+1], &count[j], &count[j+1]);

            }

        }

        for(i = 0; i < 26; ++i){

            if(count[i] != 0)

                printf("%c %d\n",let[i],count[i]);

        }

    }

    return 0;

}

 

 
#24221: Re:C 為何會記憶體區段錯誤


fire5386 (becaidorz)

學校 : 國立清華大學
編號 : 115822
來源 : [59.115.180.44]
最後登入時間 :
2024-05-03 16:46:17
c044. 10008 - What's Cryptanalysis -- UVa10008 | From: [61.230.2.126] | 發表日期 : 2021-01-28 18:08

#include

#include

#include

#include

#define MAX 1000

void swap(char *a, char *b, int *c, int *d){

    int sw;

    char chsw;

        sw = *c;

        *c = *d;

        *d = sw;

        chsw = *a;

        *a = *b;

        *b = chsw;

}

 

int main(){

    int size, i, j, strsize;

    char str[MAX];

    while(scanf("%d", &size) == 1){

        char let[27] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ\0"};

        int count[26] = {0};

        for(i = 0; i <= size; ++i){

            fgets(str, MAX, stdin);

            strsize = strlen(str) - 1;

            for(j = 0; j < strsize; ++j){

                if(islower(str[j]) != 0)

                    str[j] = toupper(str[j]);

                count[str[j] - 65]++;

            }

        }

        for(i = 0; i < 25; i++){

            for(j = 0; j < 25-i; ++j){

                if(count[j] < count[j+1])

                    swap(&let[j], &let[j+1], &count[j], &count[j+1]);

            }

        }

        for(i = 0; i < 26; ++i){

            if(count[i] != 0)

                printf("%c %d\n",let[i],count[i]);

        }

    }

    return 0;

}

 


            strsize = strlen(str) - 1; 

            for(j = 0; j < strsize; ++j){  //這行出問題

            改成for(int j = 0; j < strlen(str); ++j) {

            或是for(j = 0; j <= strsize; ++j){

 
ZeroJudge Forum