Hello,
I've got a question how to change this function, because it should read
f.e. two lines:
33333n5rr door 3333is closed 3333333
n5rrr nanana tt4tt wall t6tt6t

then transform to:
nrr dooris closed
nrrr ttnananatt twallttwallt

(writing word instead of a digit was: nice ttt5555ttt //to: tttnicettt,deleting digits in first word and deleting alone digits)
here is

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//-------------------Function--------------------------------------------
char AD(char *record, char *record_out)
{
    char *p1 = record;
    char *p2 = record_out;

     while( *p1 )
    {
        // use temp pointer to locate
        // end of digits
        char* p3 = p1;
        while( isdigit(*p3) )
            p3++;
        // is there a space following the digits ?
        if( *p3 == ' ')
            // yes, then skip the space and move on to the next char
            p1 = p3+1;
        else
        {
            // no, then was the last char put into temp buffer
            // a space
            if( *(p2-1) == ' ')
                // yes, then overwrite that space with a new character
                p2--;
            p1 = p3;
        }
        // copy all characters up to the next digit
        while(*p1 && !isdigit(*p1) )
            *p2++ = *p1++;
    }
   
}
//------------------------------------------------------------------------------
int main()
{
    char *DELIMITERS = ".";  
    char *str,record[100],*o;
    int len; 
    char line[]=" 33333n5rr door 3333is closed .n5rrr nanana tt4tt wall t6tt6t";
    str = strtok(line, DELIMITERS);
    char recordd[sizeof(record)] = {0};
    
    while (str)
    { 
        AD(str, recordd);
        printf("%s\n",recordd);
        str = strtok(NULL, DELIMITERS);
    }
    system("pause");
}

Don't do this: char recordd[sizeof(record)] [b]= {0}[/b]; It isn't doing what you think it is.

As another hint, you should compile using the -Wall option. I did and it told me, right off the bat without having to look at your code:

  • You are missing a header (since the compiler is assuming isdigit is type int isdigit() )
  • Both of your functions are missing return statements.
  • You have declared two variables that you don't use: o and len.

Penultimately, while the compiler doesn't complain, don't use C++ comments. (Though I like your style.)

And lastly, I regret having ever told anyone that system(PAUSE) is good even for homework assignments. Don't use it.

Here is your code cleaned up:

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

/*-------------------Function--------------------------------------------*/
void AD(char *record, char *record_out)
{
    char *p1 = record;
    char *p2 = record_out;

    while ( *p1 )
    {
        // use temp pointer to locate
        // end of digits
        char* p3 = p1;
        while( isdigit(*p3) )
            p3++;
        // is there a space following the digits ?
        if( *p3 == ' ')
            // yes, then skip the space and move on to the next char
            p1 = p3+1;
        else
        {
            // no, then was the last char put into temp buffer
            // a space
            if( *(p2-1) == ' ')
                // yes, then overwrite that space with a new character
                p2--;
            p1 = p3;
        }
        // copy all characters up to the next digit
        while(*p1 && !isdigit(*p1) )
            *p2++ = *p1++;
    }
}

/*-----------------------------------------------------------------------*/
int main()
{
    char *DELIMITERS = ".";
    char *str,record[100];
    char line[]=" 33333n5rr door 3333is closed .n5rrr nanana tt4tt wall t6tt6t";
    str = strtok(line, DELIMITERS);
    char recordd[sizeof(record)];

    memset(recordd, '\0', sizeof(recordd));
    
    while (str)
    { 
        AD(str, recordd);
        puts(recordd);
        str = strtok(NULL, DELIMITERS);
    }

    puts("Press ENTER to continue");
    while (!iscntrl( getchar() ));

    return 0;
}

Hope this helps.

> char recordd[sizeof(record)] = {0};
> It isn't doing what you think it is.
It's doing the same as your memset.

So, how to change this function to get this reslut:

nrr dooris closed
nrrr ttnananatt twallttwallt

:

/*-------------------Function--------------------------------------------*/
void AD(char *record, char *record_out)
{
    char *p1 = record;
    char *p2 = record_out;

    while ( *p1 )
    {
        // use temp pointer to locate
        // end of digits
        char* p3 = p1;
        while( isdigit(*p3) )
            p3++;
        // is there a space following the digits ?
        if( *p3 == ' ')
            // yes, then skip the space and move on to the next char
            p1 = p3+1;
        else
        {
            // no, then was the last char put into temp buffer
            // a space
            if( *(p2-1) == ' ')
                // yes, then overwrite that space with a new character
                p2--;
            p1 = p3;
        }
        // copy all characters up to the next digit
        while(*p1 && !isdigit(*p1) )
            *p2++ = *p1++;
    }
}

/*-----------------------------------------------------------------------*/

It's doing the same as your memset.

Hmm... I thought that was a C++ thing. Well, I've learned something.

Alas, sorry for the late night quick diagnosis.

Your algorithm assumes that the very first character in the string is a space. If you change your input string to char line[]=" 33333n5rr door 3333is closed . n5rrr nanana tt4tt wall t6tt6t"; you'll see it work just fine.

The other solution would be to change p2's initialization to char *p2 = record_out +1; Barring those, I'd rethink the algorithm... (which I didn't want to think about yesterday --sorry again).
:$

are you just trying to delete the numeral characters from a string?

then do something like this

void deleteNumerals(char *mystring)
{
    char *str_ptr = mystring;
    while(*str_ptr)
    {
        if (*str_ptr<'0' || *str_ptr>'9')
            *mystring++ = *str_ptr;
        str_ptr++;
    }
    *mystring = '\0';
}

just note that the string being pointed to will be modifed in place, destroying the original. if you need to keep the original string, save a copy of it before calling the function.

.

His assignment is to combine every two words thus:
<word0> <word1> --> <word0 replaces each consecutive sequence of digits in word1>

I really don't know what are you doing here but, lately due to fact that
I wasn't able to change this function, I've just "invented" a function which can easily count words,because some people in this forum haven't considered a possibility that line may have more than one space between words !!! ;strtok is EASY, TOO EASY;
line :" ffff (many spaces) f fff ffff . ffff "

int count (char *line)
{
  int word = 0;
  char *p1=line; 
  
  while(*p1)
  {
   while(isspace(*p1))
   p1++;  
   
   while (isdigit(*p1)) 
   p1++; 
          
   if( isalpha(*p1) )                
      
      word++;     

      while(isalpha(*p1))
      {
      p1++;
      }      
}             
  return word;
}

P.s. Mondragon I've no idea how to change(probably it would took more than 45 min...) it and I don't care any more :D

looking back at this now, it wasnt exactly clear as to what the heck the OP wanted.

I thought he was exceptionally clear. He posted his exact problem; gave nice, color-enhanced examples; and listed a nice, short code example that can be compiled.

I love this part when people try to help you, delete the whole function,create 10 times smaller and 10 times simpler ,and then suggest to use it, well reaction:
AAAAAArrrrrrrrGGGGRRRRRR......(not extraordinary happy, but still thanks :) )
PS. you ask : can you change function , you get: well I found some mistakes in main....
reaction -//-
fun in here :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.