Thread: Deleting characters from strings

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    3

    Deleting characters from strings

    I'm trying to make a simple spelling corrector and I'm having some trouble.

    Let's say I have: "This is a spelling corrector."
    How can I write a function to delete the unnessecary spaces in the string? I tried strncpy but I don't know how to skipthe spaces while copying.

    And a second question.
    We have: "This is is a spelling corrector."
    How do I delete the second 'is'?

    I'm new with C so I would apprecciate it if you guys would keep the explanation simple.

    Thanks in advance.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    This isn't a C problem, this is a general logic problem.

    For the space, you can copy to a new string character by character. If there's a space, do nothing and move on to the next. For the "is is" problem, you can break the sentence into tokens (meaning each word would be by itself) and compare all of those. Remove the doubles that are next to each other, then rebuild the sentence.

    If you're still unsure as to what needs to be done, perhaps you're trying to tackle a problem that's a bit to tough for you just yet (don't take that as an insult).

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    3
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define MAXLINE 128
    
    int read_a_line(char * line);
    int the_end(char*line);
    void add_punct(char*line);
    void capitalize(char*line);
    void misspelling(char*line);
    
    
    
    void main() {
    char line[MAXLINE +1];
    printf("---Welcome to my amazing spelling corrector---");
    printf("\n\n-> Type 'end' when finished.");
    printf("\n\nType line:");
    while (1){
    read_a_line(line);
    capitalize(line);
    misspelling(line);
    add_punct(line);
    if (the_end(line));
    break;
    
    }
    printf("Corrected: %s\n", line);
    }
    int read_a_line(char line[]){
    char c;
    int length=0;
    while ((c = getchar()) != '\n' && length  <=MAXLINE){
    line[length++]=c; }
    line[length]='\0';
    return length;
    }
    int the_end(char*line){
    return strcmp(line, "end") == 0;
    }
    void capitalize(char*line){
    line[0]=toupper(line[0]);
    }
    void misspelling(char*line){
    int j;
    for(j = 0; line[j] != '\0'; j++) {
    if(line[j-1] == 'e' && line[j] == 'b' && (isspace(line[j+1])  || line[j+1]=='\0')){
    line[j-1] = 'b'; line[j] = 'e';  }
    if(line[j-1] == 's' && line[j] == 'i' && (isspace(line[j+1])  || line[j+1]=='\0')){
    line[j-1] = 'i'; line[j] = 's';  }
    if(line[j-1] == 't' && line[j] == 'e' && line[j+1] == 'h' && (isspace(line[j+2])  || line[j+2]=='\0')){
    line[j-1] = 't'; line[j] = 'h'; line [j+1] = 'e';   }
      }
    }
    void add_punct(char*line){
    int i;
    for(i = 0; line[i] != '\0'; i++)
    if(!ispunct(line[i])&& line[i+1] == '\0'){
    line[i+1] = '.'; line[i+2] = '\0';
      }
    }
    This is what I got so far. Skorman can you write those two little functions so I can see how it should be done? The best way to learn something is seeing it .

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void erase( char *str, char chr );
    
    int main( void )
    {
        char test[] = "hello my brother's name is bob the builder!";
        
        printf( "%s\n", test );
        erase( test, 'b' );
        printf( "%s\n", test );
        getchar( );
        return 0;
    }
    
    void erase( char *str, char chr )
    {
        int i, pos = 0;
        for ( i = 0; str[i]; i ++ ) {
            if ( str [i] == chr ) {
                str[pos] = str[i + 1];
            } else {
                str[pos] = str[i];
                pos ++;
            }    
        }    
        str[pos] = '\0';
    }
    This erase function removes all occurances of a character from a string. When it encounters the character it simply overwrites it with the next character in the string. You might want to try indenting, and not using void main.

    (edit: oops fixed a bug with erase).
    Last edited by Brian; 11-09-2004 at 05:27 AM.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Or more simply (I think):
    Code:
    void erase(char *str, char chr)
    {
      while(*str)
      {
        if(*str == chr)
          memmove(str, str+1, strlen(str));
        else
          str++;
      }
    }
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by itsme86
    Or more simply (I think):
    Code:
    void erase(char *str, char chr)
    {
      while(*str)
      {
        if(*str == chr)
          memmove(str, str+1, strlen(str));
        else
          str++;
      }
    }
    It looks simpler, but is slower. Mine only does strlen(str) iterations. Without calling any external functions such as memmove or strlen, which in turn have their own loops.
    Last edited by Brian; 11-09-2004 at 07:12 AM.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    If you want to really chech the spelling you 1st have to separate the whole string in tokens, then compare each one with the next.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    Code:
    static char *
    erase(char *const string, const char c)
    {
            register char   *src;
            register char   *dst;
    
    
            for (src = dst = string; *src != '\0'; src++)
                    if (*src != c)
                            *dst++ = *src;
            *dst = '\0';
    
            return string;
    }

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    3
    Ok I got the spaces but how do you integrate the words erase in this code?

    Code:
      #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define MAXLINE 128
    
    int read_a_line(char * line);
    int the_end(char*line);
    void add_punct(char*line);
    void capitalize(char*line);
    void misspelling(char*line);
    void spaces(char*line);
    
    
    
    void main() {
    char line[MAXLINE +1];
    printf("---Welcome to Murat Soyal's amazing spelling corrector---");
    printf("\n\n-> Type 'end' when finished.");
    printf("\n\nType line:");
    while (1){
    read_a_line(line);
    capitalize(line);
    misspelling(line);
    add_punct(line);
    spaces(line);
    if (the_end(line));
    break;
    
    }
    printf("Corrected: %s\n", line);
    }
    int read_a_line(char line[]){
    char c;
    int length=0;
    while ((c = getchar()) != '\n' && length  <=MAXLINE){
    line[length++]=c; }
    line[length]='\0';
    return length;
    }
    int the_end(char*line){
    return strcmp(line, "end") == 0;
    }
    void capitalize(char*line){
    line[0]=toupper(line[0]);
    }
    void misspelling(char*line){
    int j;
    for(j = 0; line[j] != '\0'; j++) {
    if(line[j-1] == 'e' && line[j] == 'b' && (isspace(line[j+1])  || line[j+1]=='\0' || ispunct(line[j+1]))){
    line[j-1] = 'b'; line[j] = 'e';  }
    if(line[j-1] == 's' && line[j] == 'i' && (isspace(line[j+1])  || line[j+1]=='\0')){
    line[j-1] = 'i'; line[j] = 's';  }
    if(line[j-1] == 't' && line[j] == 'e' && line[j+1] == 'h' && (isspace(line[j+2])  || line[j+2]=='\0' || ispunct(line[j+1]))){
    line[j-1] = 't'; line[j] = 'h'; line [j+1] = 'e';   }
      }
    }
    void add_punct(char*line){
    int i;
    for(i = 0; line[i] != '\0'; i++)
    if(!ispunct(line[i])&& line[i+1] == '\0'){
    line[i+1] = '.'; line[i+2] = '\0';
      }
    }
    void spaces(char*line)
    {
        int i, pos = 0;
        for ( i = 0; line[i]; i ++ ) {
            if ( isspace(line[i])) {
                line[pos] = line[i + 1];
            } else {
                line[pos] = line[i];
                pos ++;
            }
        }
        line[pos] = '\0';
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-29-2008, 03:22 AM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. Check strings which should only contain some characters.
    By qingxing2005 in forum C Programming
    Replies: 2
    Last Post: 06-17-2008, 09:32 AM
  4. Converting C strings to list of characters
    By kazbo in forum C Programming
    Replies: 11
    Last Post: 02-14-2005, 10:17 AM
  5. Scanning characters & strings
    By rain_e in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 01:43 AM