Monday, 5 February 2018

Program in C to recognize spaces, words, tabs & chars and no. of lines in a input string and print input string without these words

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

char* copyString(char s1[100],int l1,int l2)
{
char r[100]="";
int k=0;
for(int i=l1;i<l2;i++)
{
//printf("E loop %d %d\n",l1,l2);
r[k]=s1[i];
//printf("%c",r[k]);
k+=1;
}
//puts(r);
return r;

}

void main()
{
int i,cs=0,k=0,ct=0,cn=0,cw=1;
char a[100]="",b[100],c[100];
char oS[100],*nS;
clrscr();
printf("Enter your string\n");
label1:
gets(c);
if(strcmp(c,"#")!=0)
{

strcat(a,c);
cn+=1;
cw+=1;
goto label1;
 }
//puts(a);
//printf("\n%d",strlen(a));
int o=0;
for(i=0;i<strlen(a);i++)
{
if(a[i]==' ')
cs+=1;
else if(a[i]=='\t')
ct+=1;
else
{
b[k]=a[i];
k+=1;
}
if(a[i]==' '||a[i]=='\t')
{
nS= copyString(a,o,i);
  /*
printf("* * ");
printf("%s",nS);
printf("* * ");
*/if(strlen(nS)>0 &&strcmp(nS," ")!=0)
{cw+=1;

//printf("true \n");
}
o=i;
}
}
printf("\nTotal space=%d",cs);
printf("\nTotal tabs=%d",ct);
printf("\nTotal lines=%d",cn);
printf("\nTotal chars=%d",(k));
printf("\nTotal words=%d",cw);
printf("\n\n");
for(int h=0;h<k;h++)
printf("%c",b[h]);
printf("\n\n");

getch();
}



Thanks

No comments:

Post a Comment