Sunday, August 2, 2009

Need help with a turbo C program?

Well you see I am trying to make a program with pointers in which it counts the number of occurrences of any two vowels in succession in a line of text by using pointers.For example


"Please read this application and give me gratuity". Such occurences are ea,ea,ui.


And in case anyone knows from where this program has been taken,please do tell:)

Need help with a turbo C program?
You didn't explain whether u wanted 'aaee' to be counted as 2 or 3. I assume it to be 3 as aa,ae,ee. The code assuming this is:








#include%26lt;stdio.h%26gt;


#include%26lt;string.h%26gt;


int isVowel(char a)


{


if(a%26gt;64%26amp;%26amp;a%26lt;91)


a+=32;


if(a=='a'||a=='e'||a=='i'||a=='o'||a=='u...


return 1;


else return 0;


}


int main()


{


char *a="Please read this application and give me gratuity";


int i=0, count=0;


while(a[i+1]!='\0')


{


if(isVowel(a[i])%26amp;%26amp;isVowel(a[i+1]))


{printf("Occurence no %d: %c%c",i+1,a[i],a[i+1]);


count++;}


i++;


}


printf("The desired count is %d", count);


return 0;


}
Reply:if you dont like programming and would rather cheat than even attempt it, drop the class or get a new major like English Lit :)
Reply:Please look at the comments. Try to follow the logic. Plug in a sample word or phrase. Notice how the program processes the input at each stage of its execution.





Note: Below, I did not know whether you want a user to input a string, or just program so that the line of text is directly assigned to a pointer:





char * copy_str = "Please read this application and give me gratuity"





I allowed for user input of text, which I assigned to a character array. I subsequently copied that array into a character pointer—for further processing.





If you wish, you can replace all code located between the two lines of asteriks (*'s) with the above * copy_str assignment statement. Simply place the assignment immediately above the While clause.


___________________________





#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;


int main()


{


int i;


int count = 0;


int singleVowel = 0;





/*************************************...


/* 50 is space enough for text and terminating character. */


char buffer[50];





printf("Enter a line of text: ");


fgets(buffer, sizeof(buffer), stdin);





/* Copy input into a character pointer for further processing. */


/* First, allocate memory; and, then do the copying. */


char * copy_str = malloc( strlen(buffer) + 1);


strcpy(copy_str, buffer);


/*************************************...





/* Now, lets iterate through each character in the string. */


while (*copy_str++ != '\0')


{


switch(*copy_str)


{


/* Check for a vowel. */


case 'a': case 'e': case 'i': case 'o': case 'u':


case 'A': case 'E': case 'I': case 'O': case 'U':


/* We have found a vowel; */


/* so, check to see if the preceding character was a vowel */


if (singleVowel = =1)


/* Increase the count of consecutive vowels */


count++;


else


/* Here, the preceding character was not a vowel */


/* If the next character is a vowel, */


/* then it will counts as being a consecutive vowel. */


singleVowel = 1;


default:


/* This particular character is not a vowel. */


/* So, any next vowel will not be a consecutive one */


singleVowel = 0;


}


}


printf("In text, we have %d consecutive vowels.\n", count);


return 0;


}





________________________


No comments:

Post a Comment