如何在C中的字符串中获取井号(或哈希)符号?

时间:2015-04-08 05:37:12

标签: c

我正在编写一个程序,这样我就可以练习/记住每一首吉他上的音符。我看待它的方式,我可以通过编写一个实用的程序进行一些练习(说实话,我的"实验室"并非所有这些都具有挑战性),以及它何时开始实施。完成后,我将有一个有用的工具来帮助我记住我的吉他音符。在来到这里之前我搜索谷歌一段时间,显然我是唯一一个遇到过这个问题的人。

无论如何,我用它来定义西方音乐中的所有音符:

char *notes[12] = {"A","A#","B","C","C#","D","D#","E","F","F#","G","G#"};

问题在于,当我列出所有元素时,磅(或哈希)不会显示出来。由于音乐 包含意外音符,我需要在我的程序中使用这些音符。那么我该如何解决这个问题?

我真的不知道这是否重要,但我通过引用将数组发送到另一个函数中:

int notePosition(char *notes[], char *strings[], int *string)
{
    int i;
    for (i = 0; i <= 11; i++)
    {
        printf("%c", *notes[i]);
    };
    return 0;
};

Sample output: AABCCDDEFFGG

说实话,我仍然不完全确定如何使用指针,而且我最好通过引用传递,但这似乎有效。我只是不知道为什么这些符号不在我的字符串中。

4 个答案:

答案 0 :(得分:2)

%c适用于单个字符。 %s是您想要的,它将输出整个字符串。当您更改为%s时,您将不再需要取消引用notes[]字符串元素:

printf("%s", notes[i]);

答案 1 :(得分:1)

您需要使用%s格式说明符而不是%c%c用于字符文字,%s用于字符串文字。所以试试这个:

printf("%s", notes[i]);

答案 2 :(得分:1)

感谢大家帮忙回答我原来的问题。这是一个愚蠢的错误,我认为其他地方存在问题,我没有考虑其他可能导致问题的事情。无论如何,我完成了程序,所以我想分享它,也许其他人想要使用它。

我也想知道是否有人看到我将来可以做些什么来改进我的节目。我知道scanf很糟糕,可以用来导致溢出,但是我并没有真正得到fgets,当用户输入的字符多于定义的输入数组时,有一些奇怪的行为,所以我只使用scanf代替。在我找到解决方案之前,请保持输入为9个字符(对于此程序,您只需要最大两个字符作为答案)。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

/*
Author: DreadedEntity
Date: 4/8/15
*/

int calculateNote(char *notes[], char *string[], int fret);
int notePosition(char *notes[], char *string[]);

int main()
{
    srand(time(NULL));

    char input[10];

    int playerPoints = 0;
    int compPoints = 0;

    int string;
    int fret;

    int round = 1;

    int answer;

    char *notes[12] = {"A","A#","B","C","C#","D","D#","E","F","F#","G","G#"};
    char *strings[6] = {"E","B","G","D","A","E"};

    printf("Let's play a game!\n\nI will give you a string and a fret on a guitar and you tell me what note it is!\nIf you answer correctly, you get a point, but if you answer incorrectly I get a point.\n\nWhoever has the most points after 20 rounds wins!\n");
    system("pause");

    do
    {
        string = (rand() % 6) + 1;
        fret = (rand() % 24);

        printf("%d. Tell me what the note is on string %d, fret %d: ", round, string, fret);
        //printf("\n%s\n", strings[string-1]);

        answer = calculateNote(notes, strings[string-1], fret);

        //printf("%s", notes[answer]); //take the comment away from this line to get all the answers

        scanf("%s", &input);

        if (strcmp(input, notes[answer]) == 0)
        {
            printf("Wow, you got that one! One point for you!\n");
            playerPoints++;
        } else
        {
            printf("Sorry, that's not right! One point for me!\n");
            compPoints++;
        };

        round++;
    } while (round <= 20);

    printf("\nThe game is over! Let's take a look at our points!\n\nYou: %d\nMe:  %d\n\n", playerPoints, compPoints);

    system("pause");

    if (playerPoints > compPoints)
    {
        printf("\nYou beat me! I'll get you next time! ");
    } else
    {
        if (playerPoints == compPoints)
        {
            printf("\nWow a tie, great job! ");
        } else
        {
            printf("\nBetter luck next time! ");
        };
    };

    system("pause");

    return 0;
}

//get position of the actual note on the fret for "notes" array
int calculateNote(char *notes[], char *string[], int fret)
{
    int position = notePosition(notes, string);

    position += fret;

    while (position >= 12)
    {
        position -= 12;
    };

    return position;
};

//get position of the note of the input string(unfretted) in "notes" array
int notePosition(char *notes[], char *string[])
{
    int i = 0;

    while (strcmp(notes[i], string) != 0)
    {
        i++;
    };

    return i;
};

答案 3 :(得分:0)

声明

printf("%c", *notes[i]);

将为您提供位于notes[i]指定地址的字符。换句话说,字符串的第一个字符。

如果您想要整个字符串,,您应该使用:

printf ("%s", notes[i]);

您可以在以下程序中看到差异:

#include <stdio.h>
int main (void) {
    char *notes[] = {
        "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"
    };
    for (int i = 0; i < sizeof(notes) / sizeof(*notes); i++)
        printf("bad = %c, good = %s\n", *notes[i], notes[i]);
    return 0;
}

输出:

bad = A, good = A
bad = A, good = A#
bad = B, good = B
bad = C, good = C
bad = C, good = C#
bad = D, good = D
bad = D, good = D#
bad = E, good = E
bad = F, good = F
bad = F, good = F#
bad = G, good = G
bad = G, good = G#