如何从字符串中删除插入的字母?

时间:2015-03-24 12:58:44

标签: c string

我找不到合适的解决方案。问题是。

我有一个字符串char availableLetters[ ]="abcdefghijklmnopqrstuvwxyz";和另一个char lettersGuessed[ ];。用户给我一个字母,该字母将被分配到lettersGuessed字符串中,然后从availableLetters字符串中删除。

这是Hangman游戏代码的一部分。

4 个答案:

答案 0 :(得分:1)

    char word[] = "abcdef";

    int index = 2; //index of letter to be deleted 

    memmove(&word[index], &word[index + 1], strlen(word) - 1);
    //1 in the third argument is the number of characters you wish to remove

之前:" abcdef"

之后:" abdef"

如果您能够获得要删除的信件的索引,那么您的工作就完成了!

请参阅链接以获取有关memmove功能的更多详细信息!

http://www.tutorialspoint.com/c_standard_library/c_function_memmove.htm

希望这有帮助!干杯!

答案 1 :(得分:-1)

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

void remCharacter( char * str, char character );

int main() {
    char myString[] = "any string";
    removeChar( myString, 'y' );
    printf( "Updated string: %s\n", myString );
    getchar();
    return 0;
}

void remCharacter( char * str, char character ) {
    for( unsigned int i = 0; i < strlen( string ); i++ )

    if( string[i] == letter )
        strcpy( string + i, string + i + 1 );
}

不要使用conio.hgetch()

希望这会对你有所帮助

答案 2 :(得分:-2)

方法-1

如果您希望字符数组char var中的s执行此操作 -

 void remove(char var,char *s)
    {
          int i;
          for(i=0;i<=strlen(s)-1 && s[i]!=var;i++)
               ;
          int k=i;
          for(i++;i<=strlen(s)-1;s[k++]=s[i++])
               ;
          s[k]=`\0';
    }

方法-2

只需取一个数组int A[26]。最初所有元素都是1.现在,当用户给出输入时,在数组中生成相应的条目。

for(int i=0;i<20;i++) A[i]=0;// initialization
char c;
scanf("%c",&c);
A[c-'a']=0;  // This character can't be used
//insert c in guessedstring;

那就是它。你不必为此制作一个字符串。

答案 3 :(得分:-2)

以下是一种如何完成的方法

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

char * remove_letter( char *s, char c )
{
    char *p = strchr( s, c );

    if ( p != NULL && *p != '\0' )
    {
        do { *p = *( p + 1 ); } while ( *p++ );
    }

    return s;
}

int main(void) 
{
    char availableLetters[ ] = "abcdefghijklmnopqrstuvwxyz";
    size_t n =  sizeof( availableLetters ) - 1;

    srand( ( unsigned int )time( NULL ) );

    for ( size_t i = 0; i < n; i++ )
    {
        char c = availableLetters[rand() % ( n - i )];

        printf( "removed character : %c, result string: \"%s\"\n",
                c, remove_letter( availableLetters, c ) );
    }

    return 0;
}

输出

removed character : y, result string: "abcdefghijklmnopqrstuvwxz"
removed character : s, result string: "abcdefghijklmnopqrtuvwxz"
removed character : j, result string: "abcdefghiklmnopqrtuvwxz"
removed character : b, result string: "acdefghiklmnopqrtuvwxz"
removed character : c, result string: "adefghiklmnopqrtuvwxz"
removed character : r, result string: "adefghiklmnopqtuvwxz"
removed character : w, result string: "adefghiklmnopqtuvxz"
removed character : f, result string: "adeghiklmnopqtuvxz"
removed character : o, result string: "adeghiklmnpqtuvxz"
removed character : g, result string: "adehiklmnpqtuvxz"
removed character : m, result string: "adehiklnpqtuvxz"
removed character : i, result string: "adehklnpqtuvxz"
removed character : d, result string: "aehklnpqtuvxz"
removed character : e, result string: "ahklnpqtuvxz"
removed character : l, result string: "ahknpqtuvxz"
removed character : x, result string: "ahknpqtuvz"
removed character : h, result string: "aknpqtuvz"
removed character : k, result string: "anpqtuvz"
removed character : u, result string: "anpqtvz"
removed character : v, result string: "anpqtz"
removed character : a, result string: "npqtz"
removed character : t, result string: "npqz"
removed character : p, result string: "nqz"
removed character : n, result string: "qz"
removed character : q, result string: "z"
removed character : z, result string: ""

很有趣的是,字母&#39; z&#39;是最后一个被删除的。:)但是如果要多次运行该程序那么就没有必要写这封信&#39; z&#39;将被删除。:)