Vowels in string C++

天涯浪子 提交于 2021-02-20 03:48:55

问题


I should type in string and my program should reorganize elements so Serbian vowels (a,o,i,e,u) should be first elements and then consonants .My idea was to copy vowels in second string and consonants in third and then make one string (using strcat and vowels and consonant string) but that didn't work as planned... Any ideas what to change or how to do it ?

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

int main()
{
    char s[50],s2[50];
    char vowels[50];
    char consonant[50];
    int k=0,f=0; 

    printf("Type string:\n");
    gets(s);


    for(int i;i<strlen(s);i++)
    {
        if(s[i]={'A','O','I','E','U','a','o','i','e','u'}) s[i]=vowels[k],k++;
        else  s[i]=consonant[f],f++;
    }

    strcat(vowels,consonant);

     printf("%s",vowels);

    return 0;

}

回答1:


if(s[i]={'A','O','I','E','U','a','o','i','e','u'}) is not a legal C++

Consider using std::strchr, and use your C -style logic.

A better way of partioning (vowels and consonants) could be : by using std::partition (since its tagged for C++ )

bool is_vowel(char ch) { return std::strchr("aeiouAEIOU", ch) != NULL ; }

std::string s; //use std::string
std::string::const_iterator p =
        std::partition(s.begin(), s.end(), 
                       std::ptr_fun(is_vowel));

See demo here

Also, avoid using gets, use std::getline with C++




回答2:


Though you targeted your question with C++ it seems that you deal with C.

Try the following code.

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

int main( void )
{
    const size_t N = 50;
    const char vowels[] = { 'A', 'O', 'I', 'E', 'U', 'a', 'o', 'i', 'e', 'u', '\0' };
    char s[N], s1[N], s2[N];

    printf( "Type string: " );
    fgets( s, N, stdin );

    char *p1 = s1,*p2 = s2;
    for ( char *q = s; *q; ++q )
    {
        if ( strchr( vowels, *q ) )
        {
            *p1++ = *q;
        }
        else
        {
            *p2++ = *q;
        }
    }

    *p1 = '\0';
    *p2 = '\0';

    strcpy( s, s1 );
    strcat( s, s2 );

    printf( "result string: \"%s\"", s );

    return 0;
}

If your compiler does not support C99 then change the loop the following way

    char *q = s, *p1 = s1,*p2 = s2;
    for ( ; *q; ++q )

And place all variable definitions in the beginning of main

Also substitute

const size_t N = 50;

for

#define N 50

As for your code then this construction

for(int i;i<strlen(s);i++)
{
    if(s[i]={'A','O','I','E','U','a','o','i','e','u'}) s[i]=vowels[k],k++;
    else  s[i]=consonant[f],f++;
}

is invalid and has no sense.




回答3:


Using strchr:

#include <cstring>
const char* vowel = "aeiouAEIOU";
//...
const char* p = strchr(vowel, str[i]);
if ( p )
{
    // p points to the vowel found
}



回答4:


I would just add a comment but have too little reputation... it seems to me that the:

s[i]=vowels[k], k++

is the wrong way round, surely it should be "vowels[k] = s[i], k++" which could be improved to "vowels[k++] = s[i]". And the same for consonant of course.

In addition, before you carry out the strcat you should add a terminating zero character to both strings:

vowels[k] = (char)0;
consonant[f] = (char)0;

as otherwise the strings are not correctly terminated - you cannot assume that char arrays are initialised to all zeroes.

And of course Paul's suggestion above is a much neater way to detect a vowel...



来源:https://stackoverflow.com/questions/21810895/vowels-in-string-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!