【问题标题】:Is it possible to compare strings from the same variable in C?是否可以在 C 中比较来自同一变量的字符串?
【发布时间】:2014-12-20 23:41:28
【问题描述】:

我想尝试比较来自同一变量的字符串的字符,以便在比较其他几个字符串时使用 for

#include <stdio.h>
#include <string.h>

int main () {

    char this_string[2][10] = {"Jason", "jason"},
    string1[6] = "Jason",
    string2[6] = "jason",
    ans[6];

    int x;

    for (x=0; x<5; x++) {
        if (string1[x]!=string2[x]) {
            strcpy(ans, string2);
            ans[x] = '-';
        }   
    }

    printf("%s\n", ans);

}

输出将是“-ason”。但是有没有办法对 this_string 做同样的事情?

【问题讨论】:

  • 他们的 strcmp 可用吗?
  • 我希望比较 string1 和 string2 之间的字符?
  • @Jongware 再次检查它是string1[x] != string2[x];
  • @Sample 这里 Shane 比较的是字符而不是字符串
  • @Jongware 你的方法是什么?

标签: c string string-comparison


【解决方案1】:

是的,这样做:

 for (x=0; x<5; x++) {
        if (this_string[0][x]!=this_string[1][x]) {
            strcpy(ans, this_string[1]);
            ans[x] = '-';
        }   
    }

【讨论】:

    猜你喜欢
    • 2022-10-15
    • 2013-10-22
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2021-10-14
    相关资源
    最近更新 更多