String is very useful chapter in C programming language. In previous several posts I have discussed on this chapter. Here are another two C programs on array – char type.
program to count line,word,character from a string.
#include < stdio.h >
#include< conio.h >
void main()
{
char ch;
int blank=1,character=0,line=1;
clrscr();
printf("Enter the string with blanks,tabs and new line\n");
printf("At the end put a full stop.\n");
while(ch!='.')
{
/* read a character from the standard input stream */
ch = getc(stdin);
if(ch==' ')
blank++;
else if(ch=='\n')
line++;
else if(ch!='.')
character++;
}
printf("\nWord %d\nCharacters %d\nLine %d",blank,character,line);
getch();
}
Write a program that converts characters like newline, blank and tab into visible sequence like \n, \b and \t.
#include < stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
char str1[100],str2[100];
int i=0,j=0;
clrscr();
printf("Enter the string with blanks,tabs and new line\n");
printf("At the end put full stop.\n");
gets(str1);
while(str1[i]!='.')
{
switch(str1[i])
{
case ' ':
{
str2[j]=';';
j++;
str2[j]='b';
j++;
i++;
break;
}
case '\t':
{
str2[j]=';';
j++;
str2[j]='t';
j++;
i++;
break;
}
case '\n':
{
str2[j]=';';
j++;
str2[j]='n';
j++;
i++;
break;
}
default :
{
str2[j]=str1[i];
j++;
i++;
break;
}
}
}
str2[j]='\0';
for(i=0;i<strlen(str2);i++)
{
if(str2[i]==';')
printf("\\");
else
printf("%c",str2[i]);
}
getch();
}
No comments:
Post a Comment