Hello i m suppose to make a function that fetch the strings according to num and it will stop when num is finished or it encounter , or ' ' or newline i made the function but its acting weird as if there is a \0 placed in

#include <stdio.h>
int ParseCh(char *N,char *Buffer,int n,char *STend) {
    int i,x=0,end=0;
    while(*N++) {
        end++;
        if(*N==',' || *N==' ' || *N=='\n'){
            STend=N+1;
            break;
        }
        else {
            end=0;
            STend='\0';
        }
    }

    if(!end)//if end is 0 then there wasnt any new line etc we just do normal copy
        for(i=n;Buffer[x]=N[i];i++,x++);
    else//if we found end bigger than 0 then we will just copy till end
        for(i=n;Buffer[x]=N[i] && Buffer[x]<end; i++,x++);

    return x;
}
int main(void)
{
    char Name[10];
    char *End;//last string after the , or new lines etc if its initlised to 0 it means there was no , new lines or tab inside the string
    ParseCh("NAME,M",Name,2,End);
    printf("Name after being parsed %s and it ended at %s\n",Name,End);
    return 0;
}

also my other function is acting weird i dunno what are those problems keep happening here when i incremented the num++ i get weird results but when i leave it i get it fine !!!! thats not even logical here look

#include <stdio.h>
#define DEBUG
char *Strchr(char *String,char c,int *num) {
    char *ptr;//whish will be ptr to first stuff we found after that char
    while(*String++) {
        #ifdef DEBUG//if this is added i get weird results;
        *num++;
        printf("num is now %d",*num);//this is weird too when i try print the number it prints as address not number
        #endif
        if(*String==c) {
            ptr=String;
            break;
        }
        else {
            ptr='\0';
            *num=0;
        }
    }
    return ptr;
}
int main(void)
{
    char name[]="Namee";
    int num=0;
    char *ptr;
    ptr=Strchr(name,'m',&num);
    if(ptr=='\0')
        puts("i m sorry we didnt find any shit");
    else
        printf("We found it at character num %d and we returned this string %s",num,ptr);
    return 0;
}

i dont know how simple *num++ can affect the string anyways when i remove it the function itself doesnt work it returns 0 character
but when i remove it work fine .....

Please try to phrase your question a little better.

From the looks of the signature of your ParseCh function (and the somewhat hazy definition of your function) it appears that you want to parse a string of tokens delimited by the ',' character and return the first and last tokens.

If this is the case, then perhaps you should try using the strtok() function.

weird is not an approved IT term. Be specific. Give details. What is the first question your mechanic would ask if you told him "my car is acting weird?"

i dont know how simple *num++ can affect the string anyways when i remove it the function itself doesnt work it returns 0 character
but when i remove it work fine .....

Well if you got it to work, than whats the problem? If it's not working try replacing *num++ with (*num)++.

You're talking syntax .. I was talking about the overall approach which IMO is far more important than syntax.

You're talking syntax .. I was talking about the overall approach which IMO is far more important than syntax.

OP never described what the code was suppost to do, so I never bothered to understand the overall approach, or even look at the code. He said that *num++ was not working. So that's what I assumed he wants to be fixed.
If it was still not working, he would have to follow WaltP suggestion and explain his code. Then I would try to fix his approch, or tell him what else might be the problem in his code.

yellowSnow> If this is the case, then perhaps you should try using the strtok() function.
strtok() is a poor designed function, which make this recommendation a poor choice.

nah it act as strcpy that will copy string according to number u give or when it has seen , newline or others and i did ++(*num); work but it only incremented number one time not according to how many characters it copied

it just returns 1 not more than that for int *num for first function

it just returns 1 not more than that for int *num for first function

How could *num go beyond 1 when your code does not allow it to go beyond that.

char *Strchr(char *String,char c,int *num) {
    char *ptr;//whish will be ptr to first stuff we found after that char
    while(*String++) {
        #ifdef DEBUG//if this is added i get weird results;
        *num++;
        printf("num is now %d",*num);//this is weird too when i try print the number it prints as address not number
        #endif
        if(*String==c) {
            ptr=String;
            break;
        }
        else {
            ptr='\0';
            [B]*num=0;[/B] // <--- you always reset your *num to 0
        }
    }
    return ptr;
}

i thought if it doesnt find string it intilise it to 1 or ? does it keep incrementing while loop is running and each character it doesnt find it it keep reseting to 1 thats y it turned to 1 coz it got incremented 1 time ?if so how can i fix this ??

i thought if it doesnt find string it intilise it to 1 or ? does it keep incrementing while loop is running and each character it doesnt find it it keep reseting to 1 thats y it turned to 1 coz it got incremented 1 time ?if so how can i fix this ??

while(*String++) , you increase your first character value by one that mean that if your first character 'A' then it will loop from 'A' to 'Z', to character 255 and then to character 0 and to 'A' repeatedly till it match c. So, how can we loop from the beginning of the string to the end of the string. Simple, every string end with '\0' character. So you can simply fix it by:

while (*String != '\0') {
   .....
   String++; // increase address by one step, not value
}

To count the position of the character you want to find in the String, you should do this way:

while (*String != '\0') {
   num++;
   if (*String == c) {
       ...... // whatever you want to do
       break;
   }
   String++;
}

but while(*string++) is same as (*string!=0) since it will alawys return true till it finds 0 in the end which will encounter when string end

but while(*string++) is same as (*string!=0) since it will alawys return true till it finds 0 in the end which will encounter when string end

It is difference. Let start that you string "Name". The first character of the string is 'N'. so *string++ is 'O' and next time *string++ will become 'P', 'Q', 'R', 'S'..... 'Z'....

What you want is getting next letter of the string, not the increment of your first letter.

no *string++ it increment the address of the string *++string increment each character inside

no *string++ it increment the address of the string *++string increment each character inside

Sorry, I was mistaken about the increment (I was at workplace where there was no compiler to test). I'm surprised that:

*b++ and *++b get the value of the next address and also increase the address. While, (*b)++ and ++*b increase the value by one. It is small matter that I have not noticed before.

Even though you are correct on this one, but you still get small mistake from that line of code. while (*String++) / Instead of starting to compare each letter with variable c from the first letter of your string, you start with the second letter of your string since you increase the first letter address before you start to compare.

yah I just noticed it too that it start at 2nd character also but it wont solve the problem since every time the loop runs it will set again *num to 0 coz its not = to the character maybe i should replace the else with an if but it will be same problem too.

yah I just noticed it too that it start at 2nd character also but it wont solve the problem since every time the loop runs it will set again *num to 0 coz its not = to the character maybe i should replace the else with an if but it will be same problem too.

As I have mentioned before, your code keep reset the *num to 0. To solve this problem, you need to remove the code where you reset *num:

char *Strchr(char *String, char c, int *num) 
{
   *num = 0;
   while (*String != '\0') {
      (*num)++;
      if (*String == c)
          break;
      String++; 
  }
  return String;
}

but if it doesnt find it num will be incremented i want only incremented when it finds it

maybe i got an idea like put a variable if it find it set it to 1 and after while loop ends i chk if its 1 or 0 if its 1 i will just return if its 0 then i will set num to 0 but i feel its kinda noobish dont you think ?

Now that I don't really understand what you want for your *num. Here is what I understand before:

If I have string "Hello World", and my c is 'W'. so the *num would be 7 because letter 'W' is the 7th letter in the string. Is it what you want or do I understand incorrectly?

and if there is no W it will return 0 anyways i fixed strchr function but i don't think its rlly a cool way to as a fix *

#include <stdio.h>
#define DEBUG
char *Strchr(char *String,char c,int *num) {
    int temp;
    char *ptr;//whish will be ptr to first stuff we found after that char
    while(*String!=0) {
        (*num)++;
        if(*String==c) {
            temp=1;
            ptr=String+1;
            break;
        }
        *String++;
    }
    if(!temp)
        *num=0;
    return ptr;
}
int main(void)
{
    char name[]="Namee";
    int num=0;
    char *ptr;
    ptr=Strchr(name,'m',&num);
    if(ptr=='\0')
        puts("i m sorry we didnt find any shit");
    else
        printf("We found it at character num %d and we returned this string %s\n",num,ptr);
    getchar();
    return 0;
}

now i have my other function which i get runtime error on i don't why though

#include <stdio.h>
#include <string.h>
int ParseCh(char *S,char *buff,int num,char *End) {
    int i,temp;
    for(i=0;S[i]!=0;i++) {
        if(S[i]==',' || S[i]=='\n' || S[i]=='\t' || i==num) {
            temp=1;
            End=S+1;
            S[i]='\0';
            break;
        }
    }
    if(temp)
        strcpy(buff,S);
    else {
        End=0;
        i=0;
    }
    return i;
}
int main(void)
{
    char Name[10];
    char *End;//last string after the , or new lines etc if its initlised to 0 it means there was no , new lines or tab inside the string
    ParseCh("NAME,M",Name,2,End);
    //printf("Name after being parsed %s and it ended at %s\n",Name,End);
    return 0;
}

I am at my workplace right now, so I don't have any compiler to test. However, I see several things that could cause some errors.

Firstly, End=S+1; , it should be End = &S[i] + 1 Secondly, int i,temp; , you should initialize the variable temp. int i,temp=0;

now i have my other function which i get runtime error on i don't why though

#include <stdio.h>
#include <string.h>
int ParseCh(char *S,char *buff,int num,char *End) {
    int i,temp;
    for(i=0;S[i]!=0;i++) {
        if(S[i]==',' || S[i]=='\n' || S[i]=='\t' || i==num) {
            temp=1;
            End=S+1;
            S[i]='\0';
            break;
        }
    }
    if(temp)
        strcpy(buff,S);
    else {
        End=0;
        i=0;
    }
    return i;
}
int main(void)
{
    char Name[10];
    char *End;//last string after the , or new lines etc if its initlised to 0 it means there was no , new lines or tab inside the string
    ParseCh("NAME,M",Name,2,End);
    //printf("Name after being parsed %s and it ended at %s\n",Name,End);
    return 0;
}

You're writing to a string literal which is read-only.

To modify a pointer in a called function, you'll need to pass a pointer to it (a pointer to a pointer).

alright it worked now but i got now one problem left that end doesnt specify anything

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

int ParseCh(char *S,char *buff,int num,char *End) {
    int i,temp=0;
    for(i=0;S[i]!=0;i++) {
        if(S[i]==',' || S[i]=='\n' || S[i]=='\t' || i==num) {
            puts("found it");
            temp=1;
            End=&S[i]+1;
            S[i]=0;
            break;
        }
    }
    if(temp)
        strcpy(buff,S);
    else {
        End=0;
        i=0;
    }
    return i;
}
int main(void)
{
    char Name2[]="NAME,M";
    char Name[10]={0};
    char *End;//last string after the , or new lines etc if its initlised to 0 it means there was no , new lines or tab inside the string
    ParseCh(Name2,Name,5,End);
    printf("Name after being parsed %s and it ended at %s\n",Name,End);
    return 0;
}
This question has already been answered. Start a new discussion instead.