Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Suppose I have a word and I need to rotate it until the first vowel. For example: prestige --> estigepr.
C#
for (i = 0; i < length; i++)
{
    switch (word[i])
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            printf("%s", word);
            break;
}

How can I do it? The loop should stop when word[i] = vowel. If there are no vowels, the whole word is rotated; i.e. the word isn't changed.
Posted
Updated 28-Apr-15 6:42am
v2
Comments
PIEBALDconsult 28-Apr-15 12:46pm    
Are you _still_ trying to do Pig Latin?
http://www.codeproject.com/Questions/985815/From-English-to-Pig-Latin?arn=0
You're trying to do it the hard way; please rethink your algorithm.
Sergey Alexandrovich Kryukov 28-Apr-15 12:49pm    
What have you tried so far?
—SA

There can be many ways to do this.

One of the ways is: create a circular singly-linked list and remember the length of the string, say, N. This way, a pointer to any of the elements will give you the sequence of "rotated" letters if you pick next N elements. If you remember the original pointer, get back to it, to one iteration forward to next letter and pick another N elements, you will have next rotation of the string. Repeat this shift N − 1 times, and you obtain all the rotations.

—SA
 
Share this answer
 
v2
Comments
nv3 28-Apr-15 18:12pm    
My 5 for answering this one at all. Looking at his previous questions it is clear that OP wants even the tiniest details of his homework solved by others.
Sergey Alexandrovich Kryukov 28-Apr-15 18:16pm    
Well, thank you. I know... I took a risk of potential waste of time by adding a pretty short but thorough answer. And it if is not "tiny enough" for the inquirer — guess who's the looser... :-)
—SA
Andreas Gieriet 29-Apr-15 0:52am    
My 5 too! ;-)
Cheers
Andi
Sergey Alexandrovich Kryukov 29-Apr-15 1:24am    
Thank you, Andi.
—SA
yngwie123 30-Apr-15 16:14pm    
nv3 I don't want a thing. Just asking Q and if you want to A you're welcome. This is an open forum. Please go back to your lonley life behind the screen. Thank you.
How about the following:
C
char buf[BUFSIZE];
strncpy(buf, "...", BUFSIZE-1);
buf[BUFSIZE-1] = '\0';
size_t len = length(buf);
size_t i = 0;
while(i < len && !is_vowel(buf[i])) i++;
if (i < len) rotate(buf, i);

is_vowel(...) and rotate(...) is left as exercise.
Cheers
Andi
 
Share this answer
 
v2
Comments
nv3 28-Apr-15 18:10pm    
Good answer. Particularly the last sentence.
Sergey Alexandrovich Kryukov 28-Apr-15 18:17pm    
What do you mean? Last sentence is "Andi". Well, it's good enough by my taste; I voted 5. :-)
—SA
nv3 28-Apr-15 18:23pm    
I meant the "... is left as exercise" :-)
Sergey Alexandrovich Kryukov 28-Apr-15 18:36pm    
I know, I know... :-)
—SA
Andreas Gieriet 29-Apr-15 0:51am    
Thanks for your 5!
Andi
You can use the following code:-

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

int main(){
	char str[100],rtStr[100];
	int len,i,flag=0,vowel_pos,c=0;
	
	printf("Enter the string: ");
	scanf("%s",&str);
	
	for(i=0;i<strlen(str);i++){>
		if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U'){
			vowel_pos=i;
			flag=1;
			break;
		}
	}
	
	//Vawel is found and string will be arrange
	if(flag==1){
		 while (c < (strlen(str)-vowel_pos)) {
	    	rtStr[c] = str[vowel_pos+c];
	      	c++;
	    }
		
	    for(i=0;i<vowel_pos;i++){>
	    	rtStr[c]=str[i];	    	
	    	c++;
	    }
	    rtStr[c] = '\0';
	    printf("Rotate String is: %s",rtStr);
	}
	//Vawel is not found, so the string will be unchanged
	else{
		printf("Rotate String is: %s",str);
	}
	
	return 0;
}
 
Share this answer
 
v2
Comments
Andreas Gieriet 8-May-15 15:43pm    
For my taste, you use strlen(str) a bit careless. Remember: strlen(str) always loops over the string to determine the position of the first \0 character. You better cache that in the variable for repeated use knowing that you don't modify the content of str.
Instead of using the elaborate if statement to determine if str[i] is a vowel, you could employ strchr, e.g. if (strchr("aeiou", tolower(str[i])) { ... }.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900