Hello! Im having trouble with reversing string in this program:

#include <iostream>
#include <string>
using namespace std;
int numChars(string besedilo[])
{
    int znaki=0;
    int vrstica=0;
    for(int a=0;a<10;a++)
    {
        vrstica=besedilo[a].length();
        znaki=znaki+vrstica;
    }
    return znaki;
}
int numWords(string besedilo[])
{
    int besede=0;
    string vrsta;
    for(int a=0;a<1;a++)
    {
        vrsta=besedilo[a];
        for(int b=0;b<besedilo[a].length();b++)
        {
            if(vrsta[b]==' ' || vrsta[b]=='\n')
            {
                besede++;
            }
        }
        besede++;
    }
    return besede;
}

int reverseString(string stavek)
{
    std::string str = stavek;

    std::string rstr = str;
    std::reverse(rstr.begin(), rstr.end());
    std::cout << rstr << std::endl;
    
}

int main()
{
    string vrstica[10];
    string ime;
    int meni;
    cout<<"Vnesi besedilo: "<<endl<<"---------------"<<endl<<endl;
    for(int a=0;a<1;a++)
    {
        getline(cin,vrstica[a]);
    }
        
    cout<<endl<<"-----------------------"<<endl;
    do
    {
        cout<<"+++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
        cout<<"  MENU                                          +"<<endl;
        cout<<"1. Izpise stevilo vnesenih znakov               +"<<endl;
        cout<<"2. Izpise stevilo vnesenih besed                +"<<endl;
        cout<<"3. Obrne niz                                    +"<<endl;
        cout<<"0. Izhod                                        +"<<endl;
        cout<<"+++++++++++++++++++++++++++++++++++++++++++++++++"<<endl<<endl;
        cin>>meni;
        if(meni==1)
        {
            cout<<"Vneseno je bilo "<<numChars(vrstica)<<" znakov!"<<endl;
        }
        if(meni==2)
        {
            cout<<endl<<"Vneseno je bilo "<<numWords(vrstica)<<" besed!"<<endl;
        }
        if(meni==3)
        {            
            cout<<reverseString(vrstica);
        }
    }
    while(meni!=0);
    return 0;
}

Can anyone help me with this problem? Thanks.

You need to include the algorithm header for std::reverse(). You're also trying to pass an array of strings to reverseString() when it only accepts a single string object.

I tried like this and no luck :(

#include <iostream>
#include <string>
using namespace std;
int numChars(string besedilo[])
{
    int znaki=0;
    int vrstica=0;
    for(int a=0;a<10;a++)
    {
        vrstica=besedilo[a].length();
        znaki=znaki+vrstica;
    }
    return znaki;
}
int numWords(string besedilo[])
{
    int besede=0;
    string vrsta;
    for(int a=0;a<1;a++)
    {
        vrsta=besedilo[a];
        for(int b=0;b<besedilo[a].length();b++)
        {
            if(vrsta[b]==' ' || vrsta[b]=='\n')
            {
                besede++;
            }
        }
        besede++;
    }
    return besede;
}

int reverseString(string besedilo[])
{
    char temp1[5];
    
    for (int i = 5; i > -1; i--)
    {
        string temp1 = besedilo[i];
        besedilo[i] = temp1;
        cout<<besedilo[i];
    }
    cout<<endl;
    
}

int main()
{
    string vrstica[10];
    string besedilo;
    int meni;
    cout<<"Vnesi besedilo: "<<endl<<"---------------"<<endl<<endl;
    for(int a=0;a<1;a++)
    {
        getline(cin,vrstica[a]);
    }
        
    cout<<endl<<"-----------------------"<<endl;
    do
    {
        cout<<"+++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
        cout<<"  MENU                                          +"<<endl;
        cout<<"1. Izpise stevilo vnesenih znakov               +"<<endl;
        cout<<"2. Izpise stevilo vnesenih besed                +"<<endl;
        cout<<"3. Obrne niz                                    +"<<endl;
        cout<<"0. Izhod                                        +"<<endl;
        cout<<"+++++++++++++++++++++++++++++++++++++++++++++++++"<<endl<<endl;
        cin>>meni;
                
        if(meni==1)
        {
            cout<<"Vneseno je bilo "<<numChars(vrstica)<<" znakov!"<<endl;
        }
        if(meni==2)
        {
            cout<<endl<<"Vneseno je bilo "<<numWords(vrstica)<<" besed!"<<endl;
        }
        if(meni==3)
        {         
            cout<<endl<<reverseString(vrstica)<<endl;
        }
    }
    while(meni!=0);
    return 0;
}

Now reverseString() doesn't do any reversing. All it does is make a copy of strings in the array and print them as-is...

Can you give me any pointers please? I can't figure it out...

Replace reverseString() in your original code with this:

int reverseString(string a[])
{
    for (int i = 0; i < 1; i++)
    {
        std::string rstr = a[i];
        std::reverse(rstr.begin(), rstr.end());
        std::cout << rstr << std::endl;
    }
}

I'd offer other suggestions, but I don't want to overwhelm you.

compile by vc2010, coding style of C++11?

#include<algorithm> //for std::for_each, std::reverse and std::copy
#include<array> //for std::array
#include<iostream> //for std::cout, std::cin.get
#include<iterator> //for std::ostream_iterator
#include<string>

template<typename T>
void reverseString(T &value)
{  
  std::for_each(value.begin(), value.end(),
  [](std::string &fir)
  {
    std::reverse(fir.begin(), fir.end());
  });
}

int main()
{    
  std::array<std::string, 3> value = {"one", "two", "three"};
  reverseString(value);
  std::copy(value.begin(), value.end(), std::ostream_iterator<std::string>(std::cout,"\n"));
  std::cin.get();

  return 0;
}

compile by vc2010, coding style of C++98?

#include<algorithm>
#include<iostream>
#include<string>

//I don't know how to deduce the number of the element at compile time
//without passing a parameter "num", please tell me how to
//do it if you know, thanks
void reverseString(std::string value[], size_t num)
{
  for(size_t i = 0; i != num; ++i)
    std::reverse(value[i].begin(), value[i].end() );
}

int main()
{    
  std::string value[] = {"one", "two", "three"};
  reverseString(value, sizeof(value)/ sizeof(*value) );
  std::copy(value, value + 3, std::ostream_iterator<std::string>(std::cout,"\n"));
  std::cin.get();

  return 0;  
}

the implementation of std::reverse are quite simple
I only post the one with random access iterator(a smart pointer which similar to pointer)

template<typename RandomItr>
void Reverse(RandomItr begin, RandomItr end)
{
  while(begin < end)
  {
    std::iter_swap(begin, --end);
    ++begin;
  }
}

another solution

#include<algorithm>
#include<iostream>
#include<iterator>
#include<string>

template<typename ForwardItr>
void reverseString(ForwardItr begin, ForwardItr end)
{
  while(begin != end)
  {
    std::reverse(begin->begin(), begin->end());
    ++begin;
  }
}

int main()
{
  std::string value[] = {"one", "two", "three"};
  enum {Size = sizeof(value)/ sizeof(*value)};
  reverseString(value, value +  Size);
  std::copy(value, value + Size, std::ostream_iterator<std::string>(std::cout,"\n"));
  std::cin.get();

  return 0;
}

you could place your std::string into std::vector or std::list either

#include<algorithm>
#include<iostream>
#include<iterator>
#include<string>
#include<vector>

template<typename ForwardItr>
void reverseString(ForwardItr begin, ForwardItr end)
{
  while(begin != end)
  {
    std::reverse(begin->begin(), begin->end());
    ++begin;
  }
}

int main()
{
  char const *str[] = {"one", "two", "three"};
  std::vector<std::string> value(str, str + sizeof(str) / sizeof(*str) );
  reverseString(value.begin(), value.end());
  std::copy(value.begin(), value.end(), std::ostream_iterator<std::string>(std::cout,"\n"));
  std::cin.get();

  return 0;
}

Thanks everyone... It works now... really thanks again everyone...

Now that i don't open new topic I will ask here:

I have to count how many c,s,z are there in that array. I did it like this and i can't figure it out... Thanks again for help...

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int numChars(string besedilo[])
{
    int znaki=0;
    int vrstica=0;
    for(int a=0;a<10;a++)
    {
        vrstica=besedilo[a].length();
        znaki=znaki+vrstica;
    }
    return znaki;
}
int numWords(string besedilo[])
{
    int besede=0;
    string vrsta;
    for(int a=0;a<1;a++)
    {
        vrsta=besedilo[a];
        for(int b=0;b<besedilo[a].length();b++)
        {
            if(vrsta[b]==' ' || vrsta[b]=='\n')
            {
                besede++;
            }
        }
        besede++;
    }
    return besede;
}

int reverseString(string a[])
{
    for (int i = 0; i < 1; i++)
    {
        string rstr = a[i];
        reverse(rstr.begin(), rstr.end());
        cout << rstr << endl;
    }
}

int countChar( char * str , char ch )
{
    int countChar = 0;
    
    for ( int i = 0; str[i] != '\0'; i++)
    {
        if ( str[i] == ch )
            countChar = countChar + 1;
    }
    
    return countChar;
}

int main()
{
    string vrstica[10];
    string ime;
    int meni;
    cout<<"Vnesi besedilo: ";
    cout<<endl<<"-----------------------"<<endl;
    for(int a=0;a<1;a++)
    {
        getline(cin,vrstica[a]);
    }
    
    cout<<"-----------------------"<<endl;
    do
    {
        cout<<"+++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
        cout<<"+    MENU                                       +"<<endl;
        cout<<"+ 1. Izpise stevilo vnesenih znakov             +"<<endl;
        cout<<"+ 2. Izpise stevilo vnesenih besed              +"<<endl;
        cout<<"+ 3. Obrne niz                                  +"<<endl;
        cout<<"+ 4. Prešteje število sičnikov                  +"<<endl;
        cout<<"+ 0. Izhod                                      +"<<endl;
        cout<<"+++++++++++++++++++++++++++++++++++++++++++++++++"<<endl<<endl;
        cin>>meni;
        if(meni==1)
        {
            cout<<"Stevilo znakov: "<<numChars(vrstica)<<endl;
        }
        if(meni==2)
        {
            cout<<endl<<"Stevilo besed: "<<numWords(vrstica)<<endl;
        }
        if(meni==3)
        {            
            cout<<reverseString(vrstica);
        }
        if(meni==4)
        {
            cout << countChar( vrstica , 's' ) << endl;
        }
    }
    while(meni!=0);
    return 0;
}

In this I "did" it for s only...

Thanks again

I have to count how many c,s,z are there in that array. I did it like this and i can't figure it out... Thanks again for help...

you mean your countChar has some problem?
Your logics looks good for me, but you have to pass the std::string like this

size_t countChar( vrstica.c_str() , 's' );

and change the interface to

size_t countChar( char const *str , char ch );

besides, you don't need to pass the string by array and write a for loop
you could just do it like this

void reverseString(string const &a)
{    
    string rstr = a;
    reverse(rstr.begin(), rstr.end());
    cout << rstr << endl;    
}

There are many ways to do the same job, you could try another solutions if you like
example : make the codes become shorter and save a temporary variable i

size_t countChar( char const *str , char ch )
{
    size_t countChar = 0;

    //generally speaking, postfix++ is not a good idea, but it
    //is ok for primitive types like char
    while(*str) if(*str++ == ch) countChar += 1;          

    return countChar;
}

you could solve this problem by stl too

template<typename ForwardItr>
size_t countChar( ForwardItr begin , ForwardItr end, char ch)
{
  size_t sum = 0;  
  std::for_each(begin, end, [&](char fir){ if(fir == ch) ++sum;} );

  return sum;
}

I did it like this and it works, but there is a problem, one input is string and one an array... How can i fix it, that i wouldn't have to type input twice?

Code:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int numChars(string besedilo[])
{
    int znaki=0;
    int vrstica=0;
    for(int a=0;a<10;a++)
    {
        vrstica=besedilo[a].length();
        znaki=znaki+vrstica;
    }
    return znaki;
}
int numWords(string besedilo[])
{
    int besede=0;
    string vrsta;
    for(int a=0;a<1;a++)
    {
        vrsta=besedilo[a];
        for(int b=0;b<besedilo[a].length();b++)
        {
            if(vrsta[b]==' ' || vrsta[b]=='\n')
            {
                besede++;
            }
        }
        besede++;
    }
    return besede;
}

int reverseString(string a[])
{
    for (int i = 0; i < 1; i++)
    {
        string rstr = a[i];
        reverse(rstr.begin(), rstr.end());
        cout << rstr << endl;
    }
}

int numZ( string str )
{
    int countChar = 0;
    
    for (int i = 0; str[i] != '\0'; i++)
    {
        if ( str[i] == 'z' )
            countChar++;
    }
    
    return countChar;
}

int numS( string str )
{
    int countChar = 0;
    
    for (int i = 0; str[i] != '\0'; i++)
    {
        if ( str[i] == 's' )
            countChar++;
    }
    
    return countChar;
}

int numC( string str )
{
    int countChar = 0;
    
    for (int i = 0; str[i] != '\0'; i++)
    {
        if ( str[i] == 'c' )
            countChar++;
    }
    
    return countChar;
}

int main()
{
    string vrstica[10];
    string ime;
    string besedilo;
    int meni;
    cout<<"Vnesi besedilo: ";
    cout<<endl<<"-----------------------"<<endl;
    for(int a=0;a<1;a++)
    {
        cin >> vrstica[a];
    }
    cin >> besedilo;
    
    cout<<"-----------------------"<<endl;
    do
    {
        cout<<"+++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
        cout<<"+    MENU                                       +"<<endl;
        cout<<"+ 1. Izpise stevilo vnesenih znakov             +"<<endl;
        cout<<"+ 2. Izpise stevilo vnesenih besed              +"<<endl;
        cout<<"+ 3. Obrne niz                                  +"<<endl;
        cout<<"+ 4. Prešteje število sičnikov                  +"<<endl;
        cout<<"+ 5. Izbris vejic                               +"<<endl;
        cout<<"+ 0. Izhod                                      +"<<endl;
        cout<<"+++++++++++++++++++++++++++++++++++++++++++++++++"<<endl<<endl;
        cin>>meni;
        if(meni==1)
        {
            cout<<"Stevilo znakov: "<<numChars(vrstica)<<endl;
        }
        if(meni==2)
        {
            cout<<endl<<"Stevilo besed: "<<numWords(vrstica)<<endl;
        }
        if(meni==3)
        {            
            cout<<reverseString(vrstica);
        }
        if(meni==4)
        {
            cout << "Stevilo S-ov: " << numS(besedilo) << endl;  
            cout << "Stevilo C-jev: " << numC(besedilo) << endl; 
            cout << "Stevilo Z-jev: " << numZ(besedilo) << endl; 
        }
    }
    while(meni!=0);
    return 0;
}

Thanks in advance...

I don't really get your question
you could save all of the string into a vector rather
than save them into an old style array
Then iterate through all of the string in the vector

//don't pass by value but pass by reference
//remember to add const if you don't want the str to be changed
int numS( string str )
{
    int countChar = 0;
    
    for (int i = 0; str[i] != '\0'; i++)
    {
        if ( str[i] == 's' )
            countChar++;
    }
    
    return countChar;
}

you could make your life easier by template

template<char ch>
size_t num( std::string const &str )
{
    int countChar = 0;

    for (int i = 0; str[i] != '\0'; i++)
    {
        if ( str[i] == ch )
            countChar++;
    }

    return countChar;
}

typedef num<'z'> numZ;
typedef num<'s'> numS;
typedef num<'c'> numC;

I don't know why are you want to design something like numA, numZ and so on
would it better than countChar?

I did it with your help! Really thanks.

Just one more thing and it is the last really... I want to create function that moves chars for one spot, for example a -> b, b -> d ... is it possible to do that without an array?

Code looks now like that:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int numChars(string besedilo);
int numWords(string besedilo);
int reverseString(string besedilo);
int numC( string str );
int numZ( string str );
int numS( string str );
void removeDots(string besedilo);

int numChars(string besedilo)
{
    int znaki=0;
    int vrstica=0;

        vrstica=besedilo.length();
        znaki=znaki+vrstica;
    
    return znaki;
}
int numWords(string besedilo)
{
    int besede=0;
    string vrsta;
    
        vrsta=besedilo;
        for(int b=0;b<besedilo.length();b++)
        {
            if(vrsta[b]==' ' || vrsta[b]=='\n')
            {
                besede++;
            }
        }
        besede++;
    
    return besede;
}

int reverseString(string besedilo)
{
        string reverseBesedilo = besedilo;
        reverse(reverseBesedilo.begin(), reverseBesedilo.end());
        cout << reverseBesedilo << endl;
}

int numZ( string str )
{
    int countChar = 0;
    
    for (int i = 0; str[i] != '\0'; i++)
    {
        if ( str[i] == 'z' )
            countChar++;
    }
    
    return countChar;
}

int numS( string str )
{
    int countChar = 0;
    
    for (int i = 0; str[i] != '\0'; i++)
    {
        if ( str[i] == 's' )
            countChar++;
    }
    
    return countChar;
}

int numC( string str )
{
    int countChar = 0;
    
    for (int i = 0; str[i] != '\0'; i++)
    {
        if ( str[i] == 'c' )
            countChar++;
    }
    
    return countChar;
}

void removeDots(string besedilo)
{
    for(int i = besedilo.find(",", 0); i != string::npos; i = besedilo.find(",", i))
    {
        besedilo.erase(i,1);
        i++;
    }
    for(int i = besedilo.find(".", 0); i != string::npos; i = besedilo.find(".", i))
    {
        besedilo.erase(i,1);
        i++;
    }
    for(int i = besedilo.find("!", 0); i != string::npos; i = besedilo.find("!", i))
    {
        besedilo.erase(i,1);
        i++;
    }
    for(int i = besedilo.find(":", 0); i != string::npos; i = besedilo.find(":", i))
    {
        besedilo.erase(i,1);
        i++;
    }
    for(int i = besedilo.find(";", 0); i != string::npos; i = besedilo.find(";", i))
    {
        besedilo.erase(i,1);
        i++;
    }
    for(int i = besedilo.find("-", 0); i != string::npos; i = besedilo.find("-", i))
    {
        besedilo.erase(i,1);
        i++;
    }
    cout<<besedilo<<endl;
}

int main()
{
    string vrstica;
    string ime;
    int meni;
    
    cout<<"Vnesi besedilo: ";
    
    cout<<endl<<"-----------------------"<<endl;

        getline(cin,vrstica);
    
    cout<<"-----------------------"<<endl;
    
    do
    {
        cout<<"+++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
        cout<<"+    MENI:                                      +"<<endl;
        cout<<"+ 1. Izpise stevilo vnesenih znakov             +"<<endl;
        cout<<"+ 2. Izpise stevilo vnesenih besed              +"<<endl;
        cout<<"+ 3. Obrne niz                                  +"<<endl;
        cout<<"+ 4. Prešteje število sičnikov                  +"<<endl;
        cout<<"+ 5. Izbriše ločila                             +"<<endl;
        cout<<"+ 0. Izhod                                      +"<<endl;
        cout<<"+++++++++++++++++++++++++++++++++++++++++++++++++"<<endl<<endl;
        cin>>meni;
        if(meni==1)
        {
            cout<<"Stevilo znakov: "<<numChars(vrstica)<<endl;
        }
        if(meni==2)
        {
            cout<<endl<<"Stevilo besed: "<<numWords(vrstica)<<endl;
        }
        if(meni==3)
        {            
            cout<<reverseString(vrstica);
        }
        if(meni==4)
        {
            cout << "Stevilo S-ov: " << numS(vrstica) << endl;  
            cout << "Stevilo C-jev: " << numC(vrstica) << endl; 
            cout << "Stevilo Z-jev: " << numZ(vrstica) << endl; 
        }
        if(meni==5)
        {
            removeDots(vrstica);
        }
    }
    while(meni!=0);
    return 0;
}

I want to create function that moves chars for one spot, for example a -> b, b -> d ... is it possible to do that without an array?

yes, you could do it by list, deque or hash table, but array would be the best choice
in most of the cases.

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.