关于一个删除字符串中注释的小程序的问题,求指教!
就是删除字符串中注释部分,但是搞了半天不知道问题出在哪啊,求指教
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 1000
int findcomment(char a[],int x);
void main(){
int i, j;
int c;
char a[MAXLINE];
char b[MAXLINE];
printf("Enter a string\n");
/* 将字符串存入a[] */
for(i = 0; (c = getchar()) != '\n'; i++) {
a[i] = c;
}
a[i] = '\0';
/* 将跳过注释的a[]复制给b[] */
for(i,j = 0; a[i] != '\0'; i++,j++) {
if(a[i] == '/' && a[i+1] == '*'){
i = findcomment(a, i);
}
b[j] = a[i];
}
b[j] = '\0';
/* 输出已删除注释的b[] */
printf("The result string is %s\n",b);
system("pause");
}
/* 跳过注释并返回注释结尾处的下标 */
int findcomment(char a[], int x) {
for( ; ; x++){
if ((a[x] = '*') && (a[x+1] = '/'))
return (x + 1);
}
}
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 1000
int findcomment(char a[],int x);
void main(){
int i, j;
int c;
char a[MAXLINE];
char b[MAXLINE];
printf("Enter a string\n");
/* 将字符串存入a[] */
for(i = 0; (c = getchar()) != '\n'; i++) {
a[i] = c;
}
a[i] = '\0';
/* 将跳过注释的a[]复制给b[] */
for(i = j = 0; a[i] != '\0'; i++,j++) { //改 for(i,j = 0; a[i] != '\0'; i++,j++) { //
if(a[i] == '/' && a[i+1] == '*'){
i = findcomment(a, i);
}
b[j] = a[i];
}
b[j] = '\0';
/* 输出已删除注释的b[] */
printf("The result string is %s\n",b);
system("pause");
}
/* 跳过注释并返回注释结尾处的下标 */
int findcomment(char a[], int x) {
for( ; a[x]; x++){ //改 for( ; ; x++){
if ((a[x] == '*') && (a[x+1] == '/')) //改 if ((a[x] = '*') && (a[x+1] = '/'))
return (x + 2); //改 return (x + 1);
}
}