【问题标题】:Functions being ignored inside else if block在 else if 块中被忽略的函数
【发布时间】:2021-05-24 06:08:54
【问题描述】:

我一直在尝试编写代码来反转 Visual Studio 中 c 代码中的字符串。我实际上正在制作一个菜单驱动的程序,用户可以在其中决定是否要输入单个字符或带空格的字符串。问题是当他们选择第二个选项时,没有任何功能起作用。就好像它只是滑过一样。请检查以下代码:

/*
Question:
Develop and implement a function that receives some string value as a parameter and reverses it.
In the implementation, you cannot use any special functions from external string libraries.
*/


#define _CRT_SECURE_NO_WARNINGS     // Removes secure warnings
#include <stdio.h> 
#include <stdlib.h> 
# define size 20

void reverse(char* a); //function for reversing string
int len(char* a); // function for finding the length of string


void reverse(char* a) //function for reversing string
{
    int i = 0, length_string;
    char tmp;

    length_string = len(a);
    printf(" \n\n\tThe length of the string is = %d\n", length_string);

    while (i < (length_string)) //reversing the array.......
    {
        tmp = *(a + length_string - 1);
        *(a + length_string - 1) = *(a + i);
        *(a + i) = tmp;
        i++;
        length_string--;
    }

}

int len(char* a) // function for finding the length of string
{
    int len = 0, k;
    for (k = 0; *(a + k) != '\0'; k++)
    {
        len++;
    }
    return len;
}

int main()
{
    char Answer;

    printf("\n\t==========Welcome to Reverse string function==========\n");
    printf("\n\tWhat kind of input would you like to give the Console?:\n\t\ta) Individual Characters\n\t\tb) Stream of Characters/String/Line\n ");
    printf("Answer (a or b ?) : ");
    scanf_s("%c", &Answer);

    switch (Answer) {
    case 'a':
    {
        printf("\n\t==========Welcome to Reverse string function==========\n");

        int i, char_num;
        printf("\n\tHOW many characters do you want to input: ");
        scanf_s("%d", &char_num);

        char* Input_char = (char*)malloc(sizeof(char) * char_num);

        if (Input_char == NULL) // if empty array
        {
            printf("\n\nError! Memory not allocated\n");
            return 0;
        }

        printf("\n");

        for (i = 0; i < char_num; i++)
        {
            printf("Please Enter one character : ");
            scanf(" %c", (Input_char + i));

        }
        printf("\n\nThe reversed Character Stream is :  ");


        for (i = 0; i < char_num; i++)
        {
            printf("%c", *(Input_char + char_num - i - 1));

        }
        printf("\n");

        printf("\n\t=====================End of Function=================\n\n");

        free(Input_char);
        break;
    }
    case 'b':
    {
    char Input_string[size];

    printf("\n\tEnter a string (It can also be a line :) ) : ");
    //gets_s(Input_string); // We can also use scanf, but in that case we can not take a blank space as input


    fgets(Input_string, size, stdin);

    reverse(Input_string); // Will call the function to reverse the string

    printf("\n\n\tThe reversed string is : ");
    printf("%s\n\n", Input_string);

    printf("\n\t=====================End of Function=================\n\n");
    }

                              }
    return 0;
}

【问题讨论】:

  • 问题出在scanf(),它在流中留下一个尾随换行符,稍后由fgets() 使用。
  • 对于这个例子,在fgets()之前调用getchar()
  • 鉴于您的b 选项简单明了[您仍然需要删除换行符fgets 将留在缓冲区中],更复杂的a 选项的目的是什么?另外,我不确定您的reverse 是否正确
  • 这段代码中没有else。请修改标题(或检查您发布的代码)
  • @alex01011 成功了。非常感谢!!!!!!!

标签: c string function if-statement


【解决方案1】:

您的main 函数(选项a)有点复杂。

您的选项b 不会删除fgets 留下的换行符。

您的reverse 使用:

*(a + length_string - 1)

这应该是:

*(a + length_string - 1 - i)

代码可以简化。 reverse 函数可以找到字符串本身的结尾。

字符串反转使用指针稍微简单一些。

这是一些重构的代码:

/*
Question:
Develop and implement a function that receives some string value as a parameter and reverses it.
In the implementation, you cannot use any special functions from external string libraries.
*/

#define _CRT_SECURE_NO_WARNINGS         // Removes secure warnings
#include <stdio.h>
#include <stdlib.h>

#define size 20

// function for reversing string
void
reverse(char *a)
{
    char tmp;

    // find end of string
    char *b = a;
    for (;  *b != 0;  ++b);

    // point to last char
    --b;

    // reversing the array.......
    for (;  a < b;  ++a, --b) {
        tmp = *a;
        *a = *b;
        *b = tmp;
    }
}

int
main()
{
    char *cp;
    char Input_string[size];

    printf("\n\tEnter a string (It can also be a line :) ) : ");
    // We can also use scanf, but in that case we can not take a blank space
    // as input
    // gets_s(Input_string);

    cp = fgets(Input_string, size, stdin);
    if (cp != NULL) {
        // strip newline
        for (;  *cp != 0;  ++cp) {
            if (*cp == '\n') {
                *cp = 0;
                break;
            }
        }

        // Will call the function to reverse the string
        reverse(Input_string);

        printf("\n\n\tThe reversed string is : ");
        printf("%s\n\n", Input_string);
    }

    printf("\n\t=====================End of Function=================\n\n");

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多