this is my code i try to make a program but the code didn't work for me

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samimmu
    New Member
    • Mar 2007
    • 32

    this is my code i try to make a program but the code didn't work for me

    this is my code i made this code because to reverse the words and get the number or frequent characters.this my code below.



    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    void reverse(char *str, int count = 0);
    
    int main()
    {
      char *s1 = "This is a taste for you";
      char *s2 = "i like her smile her delicate talk i like her.";
    
      reverse(s1); // reverse entire string
      reverse(s2, 50);  // reverse 1st 7 chars
    
      cout << s1 << '\n';
      cout << s2 << '\n';
    
      return 0;
    }
    
    void reverse(char *str, int count)
    {
      int i, j;
      char temp;
      
      if(count == 0) count = strlen(str)-1;
    
      for(i = 0, j=count; i < j; i++, j--) {
        temp = str[ i ];
        str[ i ] = str[j];
        str[j] = temp;
     }
    }
    so the question ask me to input character, reverse the characters and get the number or frequent numbers. i am really grateful for you if you can help me, i need you to correct that post and send me back the code actually the due date is next friday.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    So what do you need help with?

    Is it reversing the strings correctly? Did you have to add anything?

    Comment

    • samimmu
      New Member
      • Mar 2007
      • 32

      #3
      thanks friend for responding. actually i try to compile it but it didn't work,besides that i want the input to be any kind of character, i don't want to specify the inputs,i want the use to input any words.as u can see in the code, i wish you can help in this case,thank you very much

      Is it reversing the strings correctly? Did you have to add anything?[/QUOTE]

      Comment

      • iLL
        New Member
        • Oct 2006
        • 63

        #4
        It compiles with VS…

        But your assigning char pointers to constant char[]. If you try and edit the string, it wont work. You need to do something like:
        Code:
        #include <iostream>
        #include <cstring>
        using namespace std;
        
        void reverse(char *str, int count = 0);
        
        int main()
        {
          char s1[] = "This is a taste for you";
          char s2[] = "i like her smile her delicate talk i like her.";
        
          reverse(s1); // reverse entire string
          reverse(s2, 7);  // reverse 1st 7 chars
        
          cout << s1 << '\n';
          cout << s2 << '\n';
        
          return 0;
        }
        
        void reverse(char * str, int count)
        {
        	int i, j;
        	char temp;
          
        	if(count == 0)
        	{
        		count = strlen(str)-1;
        	}
        
        	for(i = 0, j=count; i < j; i++, j--) 
        	{
        		temp = str[i];
        		str[i] = str[j];
        		str[j] = temp;
        	}
        }
        And if you really want to use pointers, you could do this:
        Code:
        #include <iostream>
        #include <cstring>
        using namespace std;
        
        void reverse(char *str, int count = 0);
        
        int main()
        {
          char *s1 = new char[100];
          char *s2 = new char[100];
        
          strcpy(s1,"This is a taste for you");
          strcpy(s2,"i like her smile her delicate talk i like her.");
        
          reverse(s1); // reverse entire string
          reverse(s2, 7);  // reverse 1st 7 chars
        
          cout << s1 << '\n';
          cout << s2 << '\n';
        
          delete s1;
          delete s2;
        
          return 0;
        }
        
        void reverse(char * str, int count)
        {
        	int i, j;
        	char temp;
          
        	if(count == 0)
        	{
        		count = strlen(str)-1;
        	}
        
        	for(i = 0, j=count; i < j; i++, j--) 
        	{
        		temp = str[i];
        		str[i] = str[j];
        		str[j] = temp;
        	}
        }

        Comment

        • samimmu
          New Member
          • Mar 2007
          • 32

          #5
          thank you very much, it works as i want .thanks a lot....

          But your assigning char pointers to constant char[]. If you try and edit the string, it wont work. You need to do something like:
          Code:
          #include <iostream>
          #include <cstring>
          using namespace std;
          
          void reverse(char *str, int count = 0);
          
          int main()
          {
            char s1[] = "This is a taste for you";
            char s2[] = "i like her smile her delicate talk i like her.";
          
            reverse(s1); // reverse entire string
            reverse(s2, 7);  // reverse 1st 7 chars
          
            cout << s1 << '\n';
            cout << s2 << '\n';
          
            return 0;
          }
          
          void reverse(char * str, int count)
          {
          	int i, j;
          	char temp;
            
          	if(count == 0)
          	{
          		count = strlen(str)-1;
          	}
          
          	for(i = 0, j=count; i < j; i++, j--) 
          	{
          		temp = str[i];
          		str[i] = str[j];
          		str[j] = temp;
          	}
          }
          And if you really want to use pointers, you could do this:
          Code:
          #include <iostream>
          #include <cstring>
          using namespace std;
          
          void reverse(char *str, int count = 0);
          
          int main()
          {
            char *s1 = new char[100];
            char *s2 = new char[100];
          
            strcpy(s1,"This is a taste for you");
            strcpy(s2,"i like her smile her delicate talk i like her.");
          
            reverse(s1); // reverse entire string
            reverse(s2, 7);  // reverse 1st 7 chars
          
            cout << s1 << '\n';
            cout << s2 << '\n';
          
            delete s1;
            delete s2;
          
            return 0;
          }
          
          void reverse(char * str, int count)
          {
          	int i, j;
          	char temp;
            
          	if(count == 0)
          	{
          		count = strlen(str)-1;
          	}
          
          	for(i = 0, j=count; i < j; i++, j--) 
          	{
          		temp = str[i];
          		str[i] = str[j];
          		str[j] = temp;
          	}
          }
          [/QUOTE]

          Comment

          Working...