Hello, can anyone help me with my homework? I seem to be stucked with this problem.

"Write a program that reads three strings and prints only those strings beginning with two vowels."

example
input string 1: eavesdropping
input string 2: elevators
input string 3: either
Output: eavesdropping either

Thank you so much, i am such a newbie when it comes to string problems.

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

int main(){
	char input1, input2, input3;
	int i;

	printf("Please input 3 words :\n");
	scanf("%s%s%s", &input1, &input2, & input3);
	
	
	return 0;
}

This is all i can do ><". I don't know what to do next.

Well you need to declare arrays of chars in order to read in a simple string

Say

char input1[100];
scanf( "%s", input1 );

The next step would be to say create a function called countVowels(), which takes a string and returns the number of vowels found.
Calling this from main(), and using an if() statement to check the result would be the final step.

Let's start with the basics. A string in C is a sequence of zero or more characters followed by the '\0' character. By saying char input1 , you're effectively restricting yourself to only one character, and because strings must be terminated with a null character, you're only allowing yourself empty strings. That's not very useful.

So let's use an array instead, k? :) This program will allow you to enter one string and display it:

#include <stdio.h>

int main ( void )
{
  char buffer[256];

  if ( scanf ( "%255s", buffer ) == 1 )
    printf ( "%s\n", buffer );

  return 0;
}

Notice that buffer is an array with a size greater than or equal to the length that I expect most reasonable strings to be. However, because someone may type more than that, or redirect a file with more characters than that, also notice that the format modifier in scanf specifies a field width. Never, ever, ever use %s as a format modifier. Always specify a field width that's one less than the size of the array (to account for the null character).

You don't need to do that with printf because printf will stop at the null character. Keep in mind that any standard function that works on a string in C will expect the null character to be at the end. Finally, take note that I've checked the return value of scanf. scanf returns the number of items that were successfully converted, or EOF if the end of the stream is detected before any items are read. Put simply, if the result of scanf doesn't match the number of format modifiers you have, something went wrong.

Now that you've had a crash course on strings and scanf, you can read your three strings. And because strings are also arrays (if you want them to be useful), you can index the first two characters and check to see if they're vowels:

#include <stdio.h>

int main ( void )
{
  char buffer[256];

  if ( scanf ( "%255s", buffer ) == 1 ) {
    if ( is_vowel ( buffer[0] ) && is_vowel ( buffer[1] ) )
      printf ( "%s\n", buffer );
  }

  return 0;
}

But is_vowel isn't a standard function. You'll have to write it yourself. ;) And one more word on validation. The program above is broken in a subtle way. What happens if the length of the string is less than two characters? Make sure that you verify any assumptions you're making, otherwise crashes and unpredictable behavior await you:

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

int is_vowel ( const int ch )
{
  /* Your test if ch is a vowel */
}

int main ( void )
{
  char buffer[256];

  if ( scanf ( "%255s", buffer ) == 1 ) {
    if ( strlen ( buffer ) >= 2 ) {
      if ( is_vowel ( buffer[0] ) && is_vowel ( buffer[1] ) )
        printf ( "%s\n", buffer );
    }
  }

  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.