如何在c中找到同一个字符串内第一个重复出现的字符

时间:2017-12-05 12:57:19

标签: c++ c

如何让第一次出现的重复字符只打印那个字符。它无关紧要,应该区分大小写或不敏感。 例如,输入=" opuott"答案= O / O         输入=" kuttu"答案= T / T

  /* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package try.try;

public final class R {
    public static final class attr {
    }
    public static final class id {
        public static final int textureView1=0x7f040000;
    }
    public static final class layout {
        public static final int main=0x7f020000;
    }
    public static final class string {
        public static final int app_name=0x7f030000;
    }
}

2 个答案:

答案 0 :(得分:1)

在C中,区分大小写的解决方案可能是这样的:

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

    int repeated_char(char arr[],bool char_arr[],size_t len){
       int i;
       for(i=0;i<len;i++){
             if (char_arr[arr[i]] == true)
                 return arr[i];
             else
                 char_arr[arr[i]] = true;
        }
       return -1;
    }

    int main(){

       char arr[100];
       bool char_arr [256];

       printf("Enter the string to check for: ");
      scanf("%100s",&arr);
      size_t arr_len = strlen(arr);
      memset(char_arr,false,sizeof(char_arr));
      char ans = repeated_char(arr,char_arr,arr_len);
      if (ans == -1)
          printf("No repeated chars were found");
      else
          printf("%c",ans);
    }

输入: “<强>名词 ABC- = 1 <强>名词 ABC”

输出:N

答案 1 :(得分:0)

这样的东西
std::string input;
std::cin>>input;
std::regex re("(.).*\\1");
std::smatch m;
if (std::regex_search(input,m,re)){
    std::cout << "Repeated " << m[0].str()[0] << "\n";
else
    std::cout << "No repeat\n";