Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

Trouble with storing values in arrays in C

Disclaimer: My problem may not be related to storing values in arrays, it is just what I believe the problem to be.

I am trying writing a program to replace a certain amount of spaces (8) with a tab in C. My expected output is just to return the same line as the input, but with the replacement being made. The output that I'm getting prints the input up until a space is given, at that point all remaining characters are not being stored in the array

Problem part:

void spaceConverter(char s[], int l, char ss[])
{
  int i; //loop
  int spceCnt = 0; //spaces in a row

  for(i = l; i >= 0; i--){

    if(s[i] == ' '){ // if character is a space
      spceCnt++;
    } else { //if character is not a space
      while(spceCnt > 0){  //unloads all stored up spaces
        ss[i - spceCnt] = ' ';
        spceCnt--;
      }
      ss[i] = s[i]; //adds character in s (pre-touched) array to (post-touched) ss array
      spceCnt = 0;
    }

    if(spceCnt == TABTOSPACE){ //if spceCnt is 8 character
      ss[i - spceCnt] = ''; //puts tab in next open line
      spceCnt = 0; //resets count
    }
  }
  printf("%d
", i); //returns negative 10, when only given a newline (no blanks or characters)
}

whole code:


#define MAXCHAR 10000 //maximum input
#define TABTOSPACE 8

int lineMaker(char s[], char ss[]); //prototype for lineMaker function
void spaceConverter(char s[], int l, char ss[]); //prototype for spaceConverter function


main()
{
  char il[MAXCHAR]; //define an array of characters il (input line) with MAXCHAR objects
  char ol[MAXCHAR]; //define an array of characters ol (output line) with MAXCHAR objects

  int l = lineMaker(il, ol);

  printf("%d: %s
", l, ol);
}


//stores the entire input in array "s"
int lineMaker(char s[], char ss[])
{
  int i = 0; //loop
  int c; //current character

  //while the current character is not a newline add the current character to the array
  while((c = getchar()) != '
' && c != EOF){
    s[i] = c;
    i++;
  }


  spaceConverter(s, i, ss);

  return i;

}


void spaceConverter(char s[], int l, char ss[])
{
  int i; //loop
  int spceCnt = 0; //spaces in a row

  for(i = l; i >= 0; i--){

    if(s[i] == ' '){ // if character is a space
      spceCnt++;
    } else { //if character is not a space
      while(spceCnt > 0){  //unloads all stored up spaces
        ss[i - spceCnt] = ' ';
        spceCnt--;
      }
      ss[i] = s[i]; //adds character in s (pre-touched) array to (post-touched) ss array
      spceCnt = 0;
    }

    if(spceCnt == TABTOSPACE){ //if spceCnt is 8 character
      ss[i - spceCnt] = ''; //puts tab in next open line
      spceCnt = 0; //resets count
    }
  }
  printf("%d
", i); //returns negative 10, when only given a newline (no blanks or characters)
}

I have done some test and all the if/else statements are running as intended


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...