Use of the strings in a code (c++)

Now I'm working on the c++...exactly on the strings, and I have fe

Page 1 of 1

6 Replies - 853 Views - Last Post: 18 November 2009 - 07:10 AM Rate Topic: -----

#1 PizzaKiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-November 09

Use of the strings in a code (c++)

Posted 17 November 2009 - 11:50 AM

#include <cstdlib>
#include <iostream>
#include <cstring>

using namespace std;

int vocals_per_100(char s[]);

int main(int argc, char *argv[])
{
   
    char string[255];
   
    
  

 
                           cout<<"Insert the string"<<endl;
                           cin>>string;
                           
                           cout<<vocals_per_100(string);
                           
    
  
    system("PAUSE");
    return EXIT_SUCCESS;
}


int vocals_per_100(char s[]){
    int c=0;
    int l=strlen(s);
    char w[255];
    strupr(w);
    for(int i=0;i<l;i++)
    if(w[i]=='A'||w[i]=='E'||w[i]=='I'||w[i]=='O'||w[i]=='U')
    c++;
    return c;
}



This code should calculate the percentage of vocals in a string
If I push F9 it runs correctly,it asks me to insert the string but the result
of the function is always 0 ...

Is there someone who can help me ?

Thanks !

*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 17 November 2009 - 12:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Use of the strings in a code (c++)

#2 GenHornet18  Icon User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

Re: Use of the strings in a code (c++)

Posted 17 November 2009 - 12:34 PM

you may have a problem with that variable named string, your better of going with string1. Any errors with your code?
Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Use of the strings in a code (c++)

Posted 17 November 2009 - 12:35 PM

I don't understand what it is supposed to do. I get the wrong output as you do, but, What is it you are trying to do?

EDIT:: I think you're trying to count the vowels in a sentence... not the "vocals"?

EDIT EDIT::
#include <cstdlib>
#include <iostream>
#include <cstring>

using namespace std;

int vocals_per_100(char s[]);

int main(int argc, char *argv[])
{
   
	char string[255];
   
	
  

 
						   cout<<"Insert the string"<<endl;
						   cin >> string;
						   
						   cout<<vocals_per_100(string) << endl;
						   
	
  
	return EXIT_SUCCESS;
}


int vocals_per_100(char s[]){
	int c = 0;
	int l = strlen(s); //string length, to be used in for loop
	char w[255];		/* What is this array holding? It appears
						   to me that it is holding nothing... 
						   you never stored anything in it, and 
						   therefore, when you go to sort through
						   the array in your for loop, nothing is 
						   there, and that is why you get 0. No data
						   to be sorted in the array...*/
	
	strupr(w);			/* Here you are clearly doing an upper conversion,
						   but there is nothing in w so nothing will be
						   converted to upper*/
	for(int i = 0; i < l; i++){
		if(w[i]=='A'||w[i]=='E'||w[i]=='I'||w[i]=='O'||w[i]=='U') {
			c++;
		}
	}
	return c;
}

This post has been edited by IngeniousHax: 17 November 2009 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Use of the strings in a code (c++)

Posted 17 November 2009 - 01:18 PM

I solved the mystery, however I took out the function call because it was a useless function call.

Unless you were trying to practice with functions, or unless it was part of an assignment. Either way, it works now.
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int vocals_per_100(char s[]);

int main(int argc, char *argv[])
{
	char string[255];
	int c = 0, l = 0;
   
	 cout << "Insert the string:";
	 cin >> string;
	 cout << endl;
	l = strlen(string);
	strupr(string);
		for(int i = 0; i < l; i++){
			if(string[i]=='A'||string[i]=='E'||string[i]=='I'||string[i]=='O'||string[i]=='U') {
				c++;
			}
		}
		cout << "There are: " << c << " vowels in the string" <<  endl;

  
	return EXIT_SUCCESS;
}


Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Use of the strings in a code (c++)

Posted 17 November 2009 - 02:31 PM

You dont need to use cstring header for this, just create too arrays and then compare the connents

#include <cstdlib>
#include <iostream>

using namespace std;
int vocals_per_100(char s[]);
int main()
{	 
	char string[255];	   
	cout<<"Insert the string"<<endl;	 
	cin>>string;								 
	cout<<vocals_per_100(string);							  
	system("PAUSE");   
	return 0;
}

int vocals_per_100(char *s) // pass pointer from array
{ 
	int c=0;	 
	char vowels[5] = {'A','E','I','O','U'}; // vowel array
	for(int i=0;i<255;i++)   //loop the contents of s
		for(int v=0; v<5; v++) // loop vowel array
			if(toupper(s[i]) == vowels[v]) // compare letters
				c++;   //add 1 if found
	return c;
}


Was This Post Helpful? 0
  • +
  • -

#6 PizzaKiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-November 09

Re: Use of the strings in a code (c++)

Posted 18 November 2009 - 07:01 AM

View PostGenHornet18, on 17 Nov, 2009 - 11:34 AM, said:

you may have a problem with that variable named string, your better of going with string1. Any errors with your code?


I tried to change the name from "string" to "string1" but nothing appened ... It's the same
Was This Post Helpful? 0
  • +
  • -

#7 PizzaKiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-November 09

Re: Use of the strings in a code (c++)

Posted 18 November 2009 - 07:10 AM

View PostImaSexy, on 17 Nov, 2009 - 01:31 PM, said:

You dont need to use cstring header for this, just create too arrays and then compare the connents

#include <cstdlib>
#include <iostream>

using namespace std;
int vocals_per_100(char s[]);
int main()
{	 
	char string[255];	   
	cout<<"Insert the string"<<endl;	 
	cin>>string;								 
	cout<<vocals_per_100(string);							  
	system("PAUSE");   
	return 0;
}

int vocals_per_100(char *s) // pass pointer from array
{ 
	int c=0;	 
	char vowels[5] = {'A','E','I','O','U'}; // vowel array
	for(int i=0;i<255;i++)   //loop the contents of s
		for(int v=0; v<5; v++) // loop vowel array
			if(toupper(s[i]) == vowels[v]) // compare letters
				c++;   //add 1 if found
	return c;
}




Your code is correct on my Dev, but I need to know the percentage of vowels(sorry for traduction :D) in the string...I'm working on that
Thanks a lot !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1