string manipulation

This is a discussion on string manipulation within the C++ Programming forums, part of the General Programming Boards category; i have a problem: i need to take the first character from a string and place it at the end ...

  1. #1
    Unregistered
    Guest

    string manipulation

    i have a problem:

    i need to take the first character from a string and place it at the end of the string...how can i do this?

    example:
    input "hello"
    output "elloh"

    i've tried setting a char variable equal to the first character in the string, but then how do i put it at the end? strcat() gives an illegal operation on the second parameter, saying i need a (const char *), instead of a (char).

    can someone help?

  2. #2
    0x01
    Join Date
    Sep 2001
    Posts
    88

    Try.

    #include "stdio.h"
    #include "conio.h" // getch ()
    #include "windows.h" // strlen()

    int get_strlen(char * string);
    Code:
    int main()
    {
    	char input[80] = { NULL }, // { NULL } sets all the subscript to NULL
    	     ch;
    	int     idx = 0;
    
    	printf("\n Input : ");
    	gets(input);
    
    	printf("\n input = [ %s ] ",input);
    	
    	// strlen() returns the number of chars in input
    	idx = strlen(input);
    	// idx = get_strlen(input);
    
    	// right here is where it's done - swap first and last char
    	ch = input[--idx];
    	input[idx] = input[0];
    	input[0] = ch;
    
    	printf("\n input = [ %s ] ",input);
    	printf("\n ------\n\n");
    
    	getch(); // press any key to continue
    
    	return 0;
    }
    
    // Use this is you don't have strlen() or windows.h
    int get_strlen(char * str)
    {
    	int nbr = 0;
    
    	// \0 = NULL
    	while(str[nbr] != '\0')
    		nbr++;
    
    	return nbr;
    }
    I posted another thing similiar to this one on the board.
    * Note : written in C - i hope you can get ideas from it.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    he didn't ask how to swap the first and last characters! he asked how to move the first character to the end of the string!

    Code:
    int index = strlen(input);
    char first = index[0];
    
    for(int i = 1; i < index; i++)
    {
       input[i - 1] = input[i];
    }
    
    input[index - 1] = first;
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    0x01
    Join Date
    Sep 2001
    Posts
    88

    ...

    oh... ok. sorry

  5. #5
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    knave seems to know everything

    i think i found my new god!
    Paro

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    14

    Thumbs up Nice code, Uraldor but...

    int index = strlen(input);
    char first = index[0];
    (...)
    That should be:
    char first = input[0];
    instead of:
    char first = index[0];

    right?
    Dharius

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    knave:
    Just a few nitpicks on your code, I hope you don't mind.
    >// Use this is you don't have strlen() or windows.h
    strlen() is defined in string.h and cstring which are both standard header files. Every compiler must have this function so there's no need to rewrite it. strlen() is also included in windows.h for use with Windows programs, but I've found that it's best to use the standard headers unless you're writing an API or MFC program for Windows.

    >char input[80] = { NULL }
    Yes it works, no it's not really a good idea. This will flag warnings on some compilers because you're assigning an integer value to the array and only setting the first element to that value. This construct exploits a feature of the language, but technically shouldn't be used for this particular purpose. It works so it's up to you though.

    >gets(input);
    gets is evil, never use it. I don't usually say never to use something but this is one of those exceptions where it has been proven unsafe in every case.

    >getch();
    getch is nonstandard, not everyone will have it. getchar() works just as well.

    >// \0 = NULL
    This is very very very nitpicky, but '\0' = NUL, not NULL.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest
    thanks to everyone who has helped...now i have this function:

    char *convert(char *theString)
    {
    int index = strlen(theString);
    char first = theString[0];
    for(int i = 1; i < index; i++)
    {
    theString[i - 1] = theString[i];
    }

    theString[index - 1] = first;

    return theString;
    }

    this gives me no compile errors, but my program crashes with an illegal operation. any ideas?

  9. #9
    Unregistered
    Guest
    since theString is passed to the function as a parameter it doesn't need to be returned in the fomral sense.

    be sure you have the header file for strlen() included in your project.

  10. #10
    Unregistered
    Guest
    thanks again for the reply...i included the header for strlen(), and changed the function so it doesn't return the string, but i still get the illegal operation...

    here is the entire program:

    #include <iostream>
    using std::endl;
    using std::cout;
    using std::cin;

    #include "string.h"

    void convert(char *theString);

    int main()
    {
    convert("string");
    return 0;
    }

    void convert(char *theString)
    {
    int index = strlen(theString);
    char first = theString[0];
    for(int i = 1; i < index; i++)
    {
    theString[i - 1] = theString[i];
    }

    theString[index - 1] = first;
    }

  11. #11
    0x01
    Join Date
    Sep 2001
    Posts
    88

    heh,

    Prelude,

    Yeah, I think am getting to used to my compiler ... I should have known that strlen() was defined in string.h, heh.
    And yes I know "input[80] = { 0 };" is not a good idea. (But it works for me)

    I didn't know that about gets() (why?) , but I hate useing scanf(), it doesn't look that great.

  12. #12
    Registered User
    Join Date
    Feb 2002
    Posts
    14

    nothing wrong with your function

    The problem is that "string" is passed to the function convert();

    change this:
    Code:
    int main() 
    { 
      char strTest[] = "string";
    
      cout <<"\nBefore: " << strTest << endl;
    
      convert(strTest); 
      cout <<"\nAfter: " << strTest << endl;
    
      return 0; 
    }
    Since convert() changes the string, you cannot pass a constant like "annoying string"
    Dharius

  13. #13
    Unregistered
    Guest
    unfortunately "string" is not a char * so you can't pass it to convert.

    Try doing this:
    #include <cstring> //the header file in your compiler likely to have strlen() in it

    instead of this:

    #include "string.h" // the "" syntax is for header files in the same directory as the .exe file of your program, and I doubt that's where you would find string.h, even if your compiler had it, which it probably doesn't if you don't need to use the .h syntax for precompiled headers included in your programs.

    then in main() do this:

    char dummy[10] = "string";
    convert(dummy);

    and recompile.

    Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 10:41 AM

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