简体   繁体   English

C语言如何从多个String中选择特定的String

[英]How to choose specific Strings from multiple Strings in C Language

I have four strings.我有四根弦。 The user have a choice to swap strings between them.用户可以选择在它们之间交换字符串。 But when I started creating the function, I can't find a way to avoid multiple if statements to cover all the situations.但是当我开始创建 function 时,我无法找到一种方法来避免多个 if 语句来涵盖所有情况。 So, I google it, looked on forums but I didn't find out if there is possible in C language to have four variables that somehow each one is linked to a string and then swapping the string between them using the variables.所以,我用谷歌搜索,查看了论坛,但我没有发现 C 语言是否有可能有四个变量,每个变量都以某种方式链接到一个字符串,然后使用变量在它们之间交换字符串。 Or is there another way?或者还有其他方法吗?

char string_one[]  = {"This is string 1"};
char string_two[]  = {"This is string 2"};
char string_three[]= {"This is string 3"};
char string_four []= {"This is string 4"};

void swap_strings(char *str1, char *str2){
//function to swap any 2 strings
  char *temp;
  temp=str1;
  str1=str2;
  str2=temp;
}

void swap_menu(){

  int choice1, choice2;

  printf("Which string do you want to swap: ");
  scanf("%i", &choice1);

  printf("Enter string o you want to swap with: ");
  scanf("%i", &choice2);

  if(choice1 == 1 && choice2 == 2)
    swap_strings(string_one, string_two );

  if(choice1 == 2 && choice2 == 3)
    swap_strings(string_two, string_three );


 if(choice1 == 3 && choice2 == 4)
    swap_strings(string_three, string_four );
  
  if(choice1 == 1 && choice2 == 3)
    swap_strings(string_one, string_three );

}

If you want to index the strings (as you do here) you can use an array of strings, rather than 4 array variables.如果你想索引字符串(就像你在这里所做的那样),你可以使用一个字符串数组,而不是 4 个数组变量。

To swap two of the strings, you need to modify the actual pointers rather than copies of them as you do in your code.要交换两个字符串,您需要修改实际指针而不是像在代码中那样修改它们的副本。

Here's a modified, working version of your code.这是您的代码的修改后的工作版本。 It even includes some basic error-checking, and prints out the strings before and after.它甚至包括一些基本的错误检查,并打印出之前和之后的字符串。

#include <stdio.h>

const char *strings[] = {
  "This is string 1",
  "This is string 2",
  "This is string 3",
  "This is string 4",
};

void swap_strings(const char **str1, const char **str2){
  const char *temp;
  temp = *str1;
  *str1 = *str2;
  *str2 = temp;
}

void swap_menu(){
  int choice1, choice2;
  printf("Which string do you want to swap: ");
  scanf("%i", &choice1);

  printf("Enter string do you want to swap with: ");
  scanf("%i", &choice2);

  if (choice1 < 1 || choice2 < 1 || choice1 > 4 || choice2 > 4) {
    printf("choices (%d, %d) are out of range\n", choice1, choice2);
    return;
  }
  swap_strings(&strings[choice1-1], &strings[choice2-1]);
}

int main(int argc, char **argv) {
  for (int i = 0; i < 4; i++) {
    printf("%d: %s\n", i+1, strings[i]);
  }
  swap_menu();
  for (int i = 0; i < 4; i++) {
    printf("%d: %s\n", i+1, strings[i]);
  }
}

I think you are trying to do something like:我认为你正在尝试做类似的事情:

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

char *string_one  = "This is string 1";
char *string_two  = "This is string 2";
char *string_three= "This is string 3";
char *string_four = "This is string 4";

void
swap_strings(char **str1, char **str2)
{
    char *temp = *str1;;
    *str1 = *str2;
    *str2 = temp;
}

void
swap_menu(void)
{
    char **d[] = { &string_one, &string_two, &string_three, &string_four };
    int c[2];
    printf("Which string do you want to swap: ");
    if( scanf("%i", c) != 1 ){
        fprintf(stderr, "Invalid input\n");
        exit(EXIT_FAILURE);
    }
    if( c[0] < 1 || c[0] > 4 ){
        fprintf(stderr, "Invalid entry\n");
        exit(EXIT_FAILURE);
    }
    printf("Enter string you want to swap with: ");
    if( scanf("%i", c + 1) != 1 ){
        fprintf(stderr, "Invalid input\n");
        exit(EXIT_FAILURE);
    }
    if( c[1] < 1 || c[1] > 4 || c[0] == c[1] ){
        fprintf(stderr, "Invalid entry\n");
        exit(EXIT_FAILURE);
    }
    swap_strings(d[c[0] - 1], d[c[1] - 1]);
}

int
main(void)
{
    swap_menu();
    printf("%s\n", string_one);
    printf("%s\n", string_two);
    printf("%s\n", string_three);
    printf("%s\n", string_four);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM