Remove a letter from a string

This is a discussion on Remove a letter from a string within the C++ Programming forums, part of the General Programming Boards category; I searched the forums and the internet but I haven't been able to find anything for what I want to ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    Remove a letter from a string

    I searched the forums and the internet but I haven't been able to find anything for what I want to do. Is there any easy yet begginer(ish) way to remove the last char of a string.
    For example:
    If I have a string: canyou

    I want to do this in a loop..


    canyou
    -----
    canyo
    -----
    cany
    -----
    can
    ----
    ca
    ----
    c

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Declare a second string that's 1 element shorter. Get the value from strlen() and use it in a loop to read in all of the data from the original string to the second string, stopping before you get to the element you want to erase. Tack a null character on to the end, and you're all set.

    Just a tip, don't use strlen() in the loop itself, otherwise it gets called every iteration, and your program slows down. Rather, assign it's return value to a variable, and use that variable in the loop.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Instead of modifying the string itself, just output what you want to output.

    output all n letters
    output (n-1) letters
    .
    .
    .
    output 2 letters
    output 1 letter

    No speed worries here

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    how do you just output 2 letters or 1 letter of string though.

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    char* str="foo";
    cout << "Here are the first two letters: ";
    for(int i=0; i < 2; ++i)
      cout << str[i];
    cout << endl;
    cout << "Here's the first letter: " << str[0] << endl;

  6. #6
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    /******************************************************
    
        Truncating a string by one each time
      
    ******************************************************/
    
    
    #include <iostream>
    #include <string.h>
    #include <math.h>
    #include <ctype.h>
    
    int truncate(char[],int);
    
    using namespace std;
    
    int main()
    {
        char array[81]={"FoobarisGerman?"};
        
        int size_of=strlen(array);
        
        //call function truncate 
        //passing the array and it's size into it
        truncate(array,size_of);
        int stop;
        cin>>stop;
        
        
    }
    
    
    //Declare function truncate
    int truncate(char array[],int size)
    {
        int k=size;
        for (int a=0; a<size; a++)
        {
            for (int b=0; b<k; b++)
            {
                
                cout<<array[b];
               
            }
             k=k-1;
             cout<<""<<endl;
        }
       
    }

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    A appreciate the help everyone. I got it.

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. help using strings and mapping
    By trprince in forum C Programming
    Replies: 29
    Last Post: 12-01-2007, 03:01 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  5. please help remove blanks from string
    By cjtotheg in forum C Programming
    Replies: 2
    Last Post: 10-24-2001, 12:21 PM

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