Monday, 31 August 2015

Program in c for removing spaces from the string
#include<stdio.h>
#include<string.h>
void main(){
char str[20];
int i,j,n,t,nl;
int k=0;
int l=0;
printf("\nEnter the string");
fgets(str,20,stdin);

n= strlen(str);
while(str[l]!='\0'){
k=0;
nl=0;
t=0;
for(i=l;i<n;i++){

if(str[i]=='\n'){
nl++;
}
else if(str[i]=='\t'){
t++;
}
else if(str[i] ==' '){
k++;
}

else
break;
}
if(nl>0){
for(j=l;j<n;j++){
str[j]=str[j+nl];
}
n=n-nl;
}
else if(t>0){
for(j=l;j<n;j++){
str[j]=str[j+t];
}
n=n-t;
}
else if(k>0){
for(j=l;j<n;j++){
str[j]=str[j+k];
}
n=n-k;
}
l++;
}

for(i=0;i<n;i++)
printf("%c",str[i]);
}

No comments:

Post a Comment