can this small code be improved !!

This is a discussion on can this small code be improved !! within the C++ Programming forums, part of the General Programming Boards category; this code just circularly shifts the given string, n(=4) is the no. of charactars to be shifted input: sab ulta ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    can this small code be improved !!

    this code just circularly shifts the given string,
    n(=4) is the no. of charactars to be shifted

    input: sab ulta ho gya
    output: gyasab ulta ho

    now, can there any refinements !!!!!!!!
    suggestions or changes wd be appreciated.

    thanks

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    void main()
    {
    clrscr();
    char *s="sab ulta ho gya";
    cout<<s;
    char p;
    int n=4,i=0,k=0;
    int len;
    len=strlen(s);
    
    while(i<n) {
          k=0;
          p=s[len-1];
          while(len-1>0)
          {  s[len-1]=s[len-2];   
               len--; 
          };
          s[k]=p;
         i++;
         len=strlen(s);
     };
    cout<<"\n"<<s;
    getch();
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yes that code needs lots of improvements.

    I'd share some but whats the point? You won't listen and I know it.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > #include<iostream.h>
    Modern C++ just has
    #include <iostream>

    > #include<conio.h>
    Only old DOS compilers can be sure of having anything like this. To everyone else, it's useless.

    > void main()
    Very bad - main returns int, always has done, always will do.

    > clrscr();
    Ugh - more non-standard features. Next to making programs go "beep", newbies seem to like clearing the screen for some reason. Useful programs don't bother, mostly because it would become extremely annoying very quickly.

    > char *s="sab ulta ho gya";
    Given your attempts at s[len-1]=s[len-2], this code would fail horribly on machine which store string constants in read only memory. If you really want to change the string "in place", then use an array
    char s[] = "sab ulta ho gya";

    > cout<<s;
    Minor point, but you didn't flush the output.

    Code:
    while(i<n) {
          k=0;
          p=s[len-1];
          while(len-1>0)
          {  s[len-1]=s[len-2];   
               len--; 
          };
          s[k]=p;
         i++;
         len=strlen(s);
     };
    So rather than move the string 'n' places at once, you move it one char at a time.
    This is going to be horribly expensive on long strings.
    Not to mention that you keep recalculating the length of the string (it should be a constant), there should only be one call to strlen.

    Also, all those ; following a brace are unnecessary.

    Useful string manipulation functions don't modify the string "in place". Something like
    void rotate ( char *dest, const char *src, int places );
    would be a far more flexible approach.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Thantos dear, i wld love to read and accept ur comments

    and thank u Salem for replying.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    So rather than move the string 'n' places at once, you move it one char at a time.
    i think i am playing with one character at a time..............

    and what if main() returns nothing !!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by samirself
    i think i am playing with one character at a time..............
    Thats what your code dose at the moment.
    Quote Originally Posted by samirself
    and what if main() returns nothing !!
    Main SHOULD return zero to indicate the program exited without error and none-zero to indicate an error. Using void main causes mains return value to be undefined.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    currently i am using Turbo C++ Version 3.0, salem comments hint that i shd upgrade
    so what shd i do...........

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    it's not exactly your compiler....a lot of c/c++ compilers will let you get away with a lot of stuff. i don't know if turbo c++ is a command line compiler or not. either why, invoke the compiler with the switch "-ansi" to ensure you only use standard c++
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    invoke the compiler with the switch "-ansi"

    ?????????????????sorry but how?

  10. #10
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    To do that you'll have to start the compiler from the command-line. If I recall, the executable for TC++ 3.0 is tcc.exe, so go to the command line, navigate to the "bin" directory, and type, "tcc -ansi"

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    This program only needs to be a few lines if you make use of the what's already available to you in the STL:

    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        string str = "sab ulta ho gya";
    
        cout << str << endl;
        rotate(str.begin(),?????,str.end());
        cout << str << endl;
    
        return 0;
    }
    You figure out what needs to take the place of the ????? and you will have another solution.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    i tried this on Dev-C++

    rotate(str.begin(),?????,str.end());
    and

    error:syntax error before ? token

    rotate(str.begin(),"gya",str.end());
    error:12 no matching function for call to `rotate(__gnu_cxx::__normal_iterator<char*
    correct it sir...

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Well, you need to find the correct bit of code to put in place of the ????? as I indicated. Hint: It has to do with an iterator.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with this small code
    By axe in forum C Programming
    Replies: 10
    Last Post: 01-06-2006, 08:04 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21