well bascially here i started with K&R excerises with remove tabes and spaces i m not sure why my programs works in my head it does but even in puesdo code i did it does but arr[j] sometimes get intilised to values before

#include <stdio.h>
#include <ctype.h>
#define STOP -1
int GetLine(char *SzBuff) {
    int i=0;
    char c;
    for( i=0;(c=getchar())!='\n';i++) {
        SzBuff[i]=c;
        if(c==EOF)  return -1;
    }
    SzBuff[i]='\0';
    return i;
}
int IsDel(char c) {
    if(c==' ' || c=='\t')   return 1;
    return 0;
}
void RemoveBlanks(char * SzString) {
    int j=0;
    while(*SzString) {
        if(!IsDel(*SzString)) { //wtf why the hell does it keep incrementing !!!
            SzString[j++]=*SzString;
            //putchar(SzString[j]);//used for debug
            //getchar();
            //j++;
        }
        SzString++;//this should fucken end the loop why the hell it keep going on incrementing
    }
    SzString[j]='\0';
    return;
}
int main(void)
{
    char SzName[100];
    while(GetLine(SzName)!=STOP) {
        RemoveBlanks(SzName);
        if(SzName[0]=='\0')//skip blank lines
            continue;
        puts(SzName);
    }
    return 0;
}

here in RemoveBlanks function
i made this puesdocode for it
while(string not = '\0')
if(not equal to characters specefied)
copyString to String[j] character by character

i don't know whats wrong in it ?

check the following part of your program in the GetLine() method.

for( i=0;(c=getchar())!='\n';i++) 
{
        SzBuff[i]=c;
        if(c==EOF)  return -1;
}

when u reach EOF u have not terminated your string with '\0'
try

for( i=0;(c=getchar())!='\n';i++) 
{
        SzBuff[i]=c;
        if(c==EOF)
        {
               SzBuff[i] = 0;// newly added
               return -1;
        }
}

the above may be a reason...............try

yah i noticed now but still get bad results same thing

#include <stdio.h>
#include <ctype.h>
#define STOP -1
int GetLine(char *SzBuff) {
    int i=0;
    char c;
    for( i=0;(c=getchar())!='\n';i++) {
        SzBuff[i]=c;
        if(c==EOF)  {
            SzBuff[i]='\0';
            return -1;
        }
    }
    SzBuff[i]='\0';
    return i;
}
int IsDel(char c) {
    if(c==' ' || c=='\t')   return 1;
    return 0;
}
void RemoveBlanks(char * SzString) {
    int j=0;
    while(*SzString) {
        if(!IsDel(*SzString)) { //wtf why the hell does it keep incrementing !!!
            SzString[j++]=*SzString;
            //putchar(SzString[j]);//used for debug
            //getchar();
            //j++;
        }
        SzString++;//this should fucken end the loop why the hell it keep going on incrementing
    }
    SzString[j]='\0';
    return;
}
int main(void)
{
    char SzName[100];
    while(GetLine(SzName)!=STOP) {
        RemoveBlanks(SzName);
        if(SzName[0]=='\0')//skip blank lines
            continue;
        puts(SzName);
    }
    return 0;
}

u are modifying the input string before checking the whole of it in the method RemoveBlanks() as follows:

while(*SzString) 
{
        if(!IsDel(*SzString))
        { //wtf why the hell does it keep incrementing !!!        
             SzString[j++]=*SzString;
            //putchar(SzString[j]);//used for debug
            //getchar();
            //j++;
        }
        SzString++;
      //this should ****en end the loop why the hell it keep going on incrementing
}

better take a temp string and put the characters in there and finlly modify the inputted string

yes but that should be copy the index which isn't space or tab but see now i changed to use index array members rather than addresses it worked fine i want to know why ?

#include <stdio.h>
#include <ctype.h>
#define STOP -1
int GetLine(char *SzBuff) {
    int i=0;
    char c;
    for( i=0;(c=getchar())!='\n';i++) {
        SzBuff[i]=c;
        if(c==EOF)  {
            SzBuff[i]='\0';
            return -1;
        }
    }
    SzBuff[i]='\0';
    return i;
}
int IsDel(char c) {
    if(c==' ' || c=='\t')   return 1;
    return 0;
}
void RemoveBlanks(char * SzString) {
    int j=0;
    for(int i=0;SzString[i]!=0;i++)
        if(!IsDel(SzString[i]))
            SzString[j++]=SzString[i];
    SzString[j]='\0';
    return;
}
int main(void)
{
    char SzName[100];
    while(GetLine(SzName)!=STOP) {
        RemoveBlanks(SzName);
        if(SzName[0]=='\0')//skip blank lines
            continue;
        puts(SzName);
    }
    return 0;
}

yes but that should be copy the index which isn't space or tab but see now i changed to use index array members rather than addresses it worked fine i want to know why ?

#include <stdio.h>
#include <ctype.h>
#define STOP -1
int GetLine(char *SzBuff) {
    int i=0;
    char c;
    for( i=0;(c=getchar())!='\n';i++) {
        SzBuff[i]=c;
        if(c==EOF)  {
            SzBuff[i]='\0';
            return -1;
        }
    }
    SzBuff[i]='\0';
    return i;
}
int IsDel(char c) {
    if(c==' ' || c=='\t')   return 1;
    return 0;
}
void RemoveBlanks(char * SzString) {
    int j=0;
    for(int i=0;SzString[i]!=0;i++)
        if(!IsDel(SzString[i]))
            SzString[j++]=SzString[i];
    SzString[j]='\0';
    return;
}
int main(void)
{
    char SzName[100];
    while(GetLine(SzName)!=STOP) {
        RemoveBlanks(SzName);
        if(SzName[0]=='\0')//skip blank lines
            continue;
        puts(SzName);
    }
    return 0;
}

hi i got the problem. u used

SzString++;
previously which moves the char * by one character. If previously u had
p = "this";
p++;
// p will now point to the string "his" only............

that was the only problem........

Edited 7 Years Ago by dkalita: n/a

oh thanks man i thought addresses is same as indexes but i guess i was wrong

oh thanks man i thought addresses is same as indexes but i guess i was wrong

u r welcome.......

This question has already been answered. Start a new discussion instead.