By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

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

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



Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. void reverse(char *str, int count = 0);
  6.  
  7. int main()
  8. {
  9.   char *s1 = "This is a taste for you";
  10.   char *s2 = "i like her smile her delicate talk i like her.";
  11.  
  12.   reverse(s1); // reverse entire string
  13.   reverse(s2, 50);  // reverse 1st 7 chars
  14.  
  15.   cout << s1 << '\n';
  16.   cout << s2 << '\n';
  17.  
  18.   return 0;
  19. }
  20.  
  21. void reverse(char *str, int count)
  22. {
  23.   int i, j;
  24.   char temp;
  25.  
  26.   if(count == 0) count = strlen(str)-1;
  27.  
  28.   for(i = 0, j=count; i < j; i++, j--) {
  29.     temp = str[ i ];
  30.     str[ i ] = str[j];
  31.     str[j] = temp;
  32.  }
  33. }
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.
Mar 10 '07 #1
Share this Question
Share on Google+
4 Replies


Ganon11
Expert 2.5K+
P: 3,652
So what do you need help with?

Is it reversing the strings correctly? Did you have to add anything?
Mar 10 '07 #2

P: 32
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]
Mar 11 '07 #3

P: 63
iLL
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. void reverse(char *str, int count = 0);
  6.  
  7. int main()
  8. {
  9.   char s1[] = "This is a taste for you";
  10.   char s2[] = "i like her smile her delicate talk i like her.";
  11.  
  12.   reverse(s1); // reverse entire string
  13.   reverse(s2, 7);  // reverse 1st 7 chars
  14.  
  15.   cout << s1 << '\n';
  16.   cout << s2 << '\n';
  17.  
  18.   return 0;
  19. }
  20.  
  21. void reverse(char * str, int count)
  22. {
  23.     int i, j;
  24.     char temp;
  25.  
  26.     if(count == 0)
  27.     {
  28.         count = strlen(str)-1;
  29.     }
  30.  
  31.     for(i = 0, j=count; i < j; i++, j--) 
  32.     {
  33.         temp = str[i];
  34.         str[i] = str[j];
  35.         str[j] = temp;
  36.     }
  37. }
  38.  
And if you really want to use pointers, you could do this:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. void reverse(char *str, int count = 0);
  6.  
  7. int main()
  8. {
  9.   char *s1 = new char[100];
  10.   char *s2 = new char[100];
  11.  
  12.   strcpy(s1,"This is a taste for you");
  13.   strcpy(s2,"i like her smile her delicate talk i like her.");
  14.  
  15.   reverse(s1); // reverse entire string
  16.   reverse(s2, 7);  // reverse 1st 7 chars
  17.  
  18.   cout << s1 << '\n';
  19.   cout << s2 << '\n';
  20.  
  21.   delete s1;
  22.   delete s2;
  23.  
  24.   return 0;
  25. }
  26.  
  27. void reverse(char * str, int count)
  28. {
  29.     int i, j;
  30.     char temp;
  31.  
  32.     if(count == 0)
  33.     {
  34.         count = strlen(str)-1;
  35.     }
  36.  
  37.     for(i = 0, j=count; i < j; i++, j--) 
  38.     {
  39.         temp = str[i];
  40.         str[i] = str[j];
  41.         str[j] = temp;
  42.     }
  43. }
  44.  
Mar 11 '07 #4

P: 32
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. void reverse(char *str, int count = 0);
  6.  
  7. int main()
  8. {
  9.   char s1[] = "This is a taste for you";
  10.   char s2[] = "i like her smile her delicate talk i like her.";
  11.  
  12.   reverse(s1); // reverse entire string
  13.   reverse(s2, 7);  // reverse 1st 7 chars
  14.  
  15.   cout << s1 << '\n';
  16.   cout << s2 << '\n';
  17.  
  18.   return 0;
  19. }
  20.  
  21. void reverse(char * str, int count)
  22. {
  23.     int i, j;
  24.     char temp;
  25.  
  26.     if(count == 0)
  27.     {
  28.         count = strlen(str)-1;
  29.     }
  30.  
  31.     for(i = 0, j=count; i < j; i++, j--) 
  32.     {
  33.         temp = str[i];
  34.         str[i] = str[j];
  35.         str[j] = temp;
  36.     }
  37. }
  38.  
And if you really want to use pointers, you could do this:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. void reverse(char *str, int count = 0);
  6.  
  7. int main()
  8. {
  9.   char *s1 = new char[100];
  10.   char *s2 = new char[100];
  11.  
  12.   strcpy(s1,"This is a taste for you");
  13.   strcpy(s2,"i like her smile her delicate talk i like her.");
  14.  
  15.   reverse(s1); // reverse entire string
  16.   reverse(s2, 7);  // reverse 1st 7 chars
  17.  
  18.   cout << s1 << '\n';
  19.   cout << s2 << '\n';
  20.  
  21.   delete s1;
  22.   delete s2;
  23.  
  24.   return 0;
  25. }
  26.  
  27. void reverse(char * str, int count)
  28. {
  29.     int i, j;
  30.     char temp;
  31.  
  32.     if(count == 0)
  33.     {
  34.         count = strlen(str)-1;
  35.     }
  36.  
  37.     for(i = 0, j=count; i < j; i++, j--) 
  38.     {
  39.         temp = str[i];
  40.         str[i] = str[j];
  41.         str[j] = temp;
  42.     }
  43. }
  44.  
[/quote]
Mar 13 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.