-2

I need to delete all words that contain digits from the string.

E.g. if input is abdgh 67fgh 32ghj hj dfg43 11 fg, output should be abdgh hj fg.

I thought of using while( text[i] != ' '), but I don't know how to continue it for the rest of the string (after the first whitespace).

I don't have any other idea, and couldn't find anything by googling. Please, help me!

Haris
  • 12,120
  • 6
  • 43
  • 70
  • You could parse your strings using delimiters. – Code_Jamer Mar 15 '14 at 20:45
  • A good start might be to split the input line into "words", separated by space. – Some programmer dude Mar 15 '14 at 20:45
  • And then check in each word character by character whether it's a digit. If so, just go to the next word without saving the current one. This could be at least your first idea. – Stefan Marinov Mar 15 '14 at 20:48
  • Take a look at using `strtok`, as described here: http://stackoverflow.com/questions/266357/tokenizing-strings-in-c Once you can tokenize, you can just not output any words with digits. – merlin2011 Mar 15 '14 at 20:48
  • @Joachim Pileborg I'm a beginner in programming, and I'm not quiet sure how to do that. Could you, please, explane in more details. Thanks – user3331279 Mar 15 '14 at 20:48
  • I would personally tokenize it into individual elements in a linked list and just remove any element which has a digit in it using isdigit(), then reconstruct a new string with what's left over. If you want to use that method, I'll leave the implementation up to you – ciphermagi Mar 15 '14 at 20:48
  • Split the input into words separated by spaces -possibly using `strtok`, and then choose a function `has_digit` and use it to exclude words with digits. – Filipe Gonçalves Mar 15 '14 at 20:49
  • 1
    You might want to read about the [`strtok`](http://en.cppreference.com/w/c/string/byte/strtok) and [`isdigit`](http://en.cppreference.com/w/c/string/byte/isdigit) functions. – Some programmer dude Mar 15 '14 at 20:51
  • @Filipe Gonçalves how to split the input without strtok? I'm a student, and we've not covered it yet, so I'm not sure whether we can use it. – user3331279 Mar 15 '14 at 20:57
  • Well, if you don't want to split the string, you could always use array notation to left-shift everything over the top of portions of the string containing words with digits. That would be a terrible thing to do, in terms of processing time and optimization (O(2^n) if I'm not mistaken), but in a program as small as yours it wouldn't really matter. Keep in mind, though, that a lot of instructors teach students by forcing the students to do independent research in order to find a good method on their own. – ciphermagi Mar 15 '14 at 21:00

3 Answers3

0

Here, i gave it a try. Works just fine for me. I tried to explain the logic throughout the code via comments. Hope it helps.

#include <stdio.h>
#include <string.h>


int containsNum(char * str);

int main()
{

  char  str[] = "abdgh 67fgh 32ghj hj dfg43 11 fg"; // input string

  char newstr[100] = ""; //new string to create with filtered data

  char * pch; //temp string to use in strtok

  printf("given string : %s\n",str    );

  pch = strtok (str," ");
  while (pch != NULL)
  {
    if(!containsNum(pch))// creation of new string  with strcat
    {                    // if the current word not contains any number
        strcat(newstr,pch);
        strcat(newstr," ");  //adding a space between words for readability
    }
    pch = strtok (NULL, " ");
  }

    printf("modified string : %s\n", newstr );

   return 0;
}
//function containsNum
//gets a string and checks if it has any numbers in it
//returns 1 if so , 0 otherwise
int containsNum(char * str)
{
    int i,
        size =strlen(str),
        flag=0;
    for(i=0; i<size ; ++i)
    {
        if((int)str[i] >=48 && (int)str[i] <=57 ){
           flag =1;
           break;
        }
    }
    return flag;
}

Regards

Alegria
  • 11
  • 2
0

Algorithm:

1-You will have to break your input string into smaller components which are also called as tokens. For example: for the string abdgh 67fgh 32ghj hj dfg43 11 fg the tokens could be abdgh, 67fgh, 32ghj, hj, dfg43, 11 and fg.

2- These smaller strings or tokens can be formed using the strtok function which is defined as
char * strtok ( char * str, const char * delimiters );. Thestr in the first argument is the input sting which in the code presented below is string1. The second argument called the delimiters is what actually defines when to divide the input string into smaller pieces(tokens). For instance, a whitespace as a delimiter will divide the input string whenever a whitespace is encountered, which is how the string is being divided in the code.

3-Since, your program needs to delete those words in the input string which contain digits we can use the isdigit() function to check exactly that.

WORKING CODE:

#include <cstring>
#include <ctype.h>
#include<stdio.h>
int main ()
{
 char output[100]=""; 
 int counter;
 int check=0; /* An integer variable which takes the value of "1" whenever a digit 
         is encountered in one of the smaller strings or tokens.
         So, whenever check is 1 for any of the tokens that token is to be ignored, that is, 
         not shown in the output string.*/

 char string1[] = "abdgh 67fgh 32ghj hj dfg43 11 fg";

 char delimiters[] = " ";//A whitespace character functions as a delimiter in the program

 char * token;//Tokens are the sub-strings or the smaller strings which are part of the input string.

 token=strtok(string1,delimiters);/*The first strktok call forms the first token/substring which for the input
                             given would be abdgh*/

 while(token!=NULL)/*For the last substring(token) the strtok function call will return a NULL pointer, which
              also indicates the last of the tokens(substrings) that can be formed for a given input string.
              The while loop finishes when the NULL pointer is encountered.*/

 {
 for(counter=0;counter<=strlen(token)-1;counter++)/*This for loop iterates through each token element. 
                                                  Example: In case of abdgh, it will first check for 'a', 
                                                  then 'b', then 'd' and so on..*/


 {
if(isdigit((int)token[counter])>0)/*This is to check if a digit has been encountered inside a token(substring).
                                  If a digit is encountered we make check equal to 1 and break our loop, as
                                  then that token is to be ignored and there is no real need to iterate
                                  through the rest of the elements of the token*/
    {
    check=1;
    break;
    }
    }
if(check==1)    /* Outside the for loop, if check is equal to one that means we have to ignore that token and
                it is not to be made a part of the output string. So we just concatenate(join) an 
                empty string ( represented by " " )with the output string*/
{
    strcat(output,"");  
    check=0;
}
else            /*If a token does not contain any digit we simply make it a part of the output string
                by concatenating(joining) it with the output string. We also add a space for clarity.*/
{
    strcat(output,token);
    strcat(output," ");
}
   token = strtok( NULL, delimiters ); /*This line of code forms a new token(substring) every time it is executed
                                 inside the while loop*/
}
printf( "Output string  is:: %s\n", output ); //Prints the final result

return 0;
}
Code_Jamer
  • 913
  • 2
  • 9
  • 21
  • @user3331279 Please review the answer above. I have added detailed comments so that the actual code becomes easier to understand. Unfortunately earlier I had misread the question. Hopefully, this works for you. – Code_Jamer Mar 16 '14 at 02:11
-1
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

char *filter(char *str){
    char *p, *r;
    p = r = str;
    while(*r){
        char *prefetch = r;
        bool contain_digit = false;
        while(!isspace(*prefetch) && *prefetch){
            if(contain_digit)
                ++prefetch;
            else if(isdigit(*prefetch++))
                contain_digit = true;
        }
        if(contain_digit){
            r = prefetch;
        }else {
            while(r < prefetch){
                *p++ = *r++;
            }
        }
        if(!*r)
            break;
        if(p[-1] == *r)
            ++r;
        else
            *p++ =*r++;
    }
    *p = '\0';
    return str;
}

int main(void) {
    char text[] = "abdgh 67fgh 32ghj hj dfg43 11 fg";

    printf("%s\n", filter(text));//abdgh hj fg

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70