I neeed a program to perform the following tasks:
• Read in a c-string from the user.
• Reverse the string (e.g. “ABC” would become “CBA”).
• Make all of the characters uppercase.
• Remove all the vowels from the string
• Print out the new c-string.
See the screenshot below as a guide:
Please enter a string of at most 30 characters:
The quick brown fox...
Here is your vowel-free string!
...XF NWRB KCQ HT
Press any key to continue . . .
the program i have does not work properly but it compiles and run and i dont know whats wrong with it
5 Replies - 1007 Views - Last Post: 20 January 2009 - 11:41 AM
#1
There is a program that does not work properly, but compiles and runs
Posted 20 January 2009 - 09:15 AM
Replies To: There is a program that does not work properly, but compiles and runs
#2
Re: There is a program that does not work properly, but compiles and runs
Posted 20 January 2009 - 09:16 AM
[rules][/rules]
#3
Re: There is a program that does not work properly, but compiles and runs
Posted 20 January 2009 - 09:28 AM
Show us the code Maria
#4
Re: There is a program that does not work properly, but compiles and runs
Posted 20 January 2009 - 11:08 AM
#include <iostream> #include <cstdlib> using namespace std; int main() {//count will hold the number of characters in the character array //number will mark the end of the characters after the vowels are removed //myString is the string(character array) that the user enters //targetString is storage for the uppercase version //noVowel will store the vowel eliminated string int i,count, number; char myString[31], targetString[31], noVowel[31]; cout<<"Please enter a string of at most 30 characters: "<<endl; cin.get(myString,31); //first need to find the length of the string i=2; while (myString[i]!='\0') i++; count=i; targetString[count]='\0'; //loop to uppercase and reverse the string for (i=count-1;i>=0;i--) targetString[count]=toupper(myString[i]); number=0; //strip all the vowels out for(i=0;i<=count;i++){ if(targetString[i]!='A' && targetString[i]!='E' && targetString[i]=='I' && targetString[i]!='O'&&targetString[i]!='U') { noVowel[number]= targetString[i]; number++; }//end if }//end for //null terminate the string noVowel[number]='\0'; cout<<"Here is your vowel-free string!"; cout<<endl; cout<<targetString[i]<<endl; system("PAUSE"); return 0; }
This post has been edited by maria_isabel: 20 January 2009 - 11:12 AM
#5
Re: There is a program that does not work properly, but compiles and runs
Posted 20 January 2009 - 11:34 AM
what is it printing?
cause i see you're printing just one element from the targetstring. dunno if that's the problem.
cause i see you're printing just one element from the targetstring. dunno if that's the problem.
#6
Re: There is a program that does not work properly, but compiles and runs
Posted 20 January 2009 - 11:41 AM
i was quite confused but i've made some changes:
the changes:
you can use strlen to find the length of string,
in the loop to reverse and uppercase, you had not reduced the value of count,
mostly, you gave targetString[i]=='i'
while outputting, to get the whole string just say cout<<noVowel and not the with "i"
#include <iostream> #include <cstdlib> using namespace std; int main() { int i, count, number; char myString[31], targetString[31], noVowel[31]; cout << "Please enter a string of at most 30 characters: " << endl; cin.get(myString, 31); /* //first need to find the length of the string i=2; while (myString[i]!='\0') i++; count=i; targetString[count]='\0';*/ count = strlen(myString); targetString[count+1]='\0'; for (i = count;i >= 0;i--,count--) targetString[count] = toupper(myString[i]); number = 0; for (i = 0;i <= strlen(myString);i++) { if (targetString[i] != 'A' && targetString[i] != 'E' && targetString[i] != 'I' && targetString[i] != 'O' && targetString[i] != 'U') { noVowel[number] = targetString[i]; number++; } } noVowel[number] = '\0'; cout << "Here is your vowel-free string:"; cout << endl; cout << noVowel << endl; system("PAUSE"); return 0; }
the changes:
you can use strlen to find the length of string,
in the loop to reverse and uppercase, you had not reduced the value of count,
mostly, you gave targetString[i]=='i'

while outputting, to get the whole string just say cout<<noVowel and not the with "i"
Page 1 of 1