Pages

Get input from user and replace tab, blank space, new line and back slash by respectivily \t, \b, \n, \\

Friday, 19 April 2013


#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
      int tab=0, new_line=0, blank=0, i, j;
      char string[500];
      char ch;
      printf("Enter data. Press \'.\' when complete.\n");
 
      i=0;
      while(ch!='.')
      {
                    ch=getche();
                    string[i]=ch;
                    if(ch==13)
                    {
                              printf("\n");
                              string[i]=92;
                              i++;
                              string[i]='n';  
                    }
                    if(ch==32)
                    {
                              string[i]=92;
                              i++;
                              string[i]='b';        
                    }
                    if(ch==9)
                    {
                              string[i]=92;
                              i++;
                              string[i]='t';
                                 
                    }
                    if(ch==92)
                    {
                               string[i]=92;
                               i++;
                               string[i]=92;
                    }
                    i++;        
      }
      printf("\n");
      for(j=0; j<=i; j++)
      {
               printf("%c", string[j]);      
      }
      //printf("%d", strlen(string));
      getch();  
}

Word Counter Programe



#include <stdio.h>
#include <conio.h>
main()
{
      int i;
      char string[500];
      char ch;
      printf("Enter data. Press Esc when you complete typing.\n");
      ch=getche();
      i=1;
      while(ch!=27)
      {
                    ch=getche();
                    string[i]=ch;
                    if(ch==13)
                    {
                                printf("\n");
                                i++;      
                    }
                    if(ch==32 || ch==9)
                    {
                              i++;      
                    }      
      }
      printf("\nYou have enter %d word(s).", i);
      getch();  
}

Q:. what kind of inputs are most likely to uncover bugs if there are any?
Ans:. Semantic definition of word, some special cases:
  • link word: "cat-walk"
  • small word: a, b,c
  • biiiiiig words: "a fooooooooo<40MILLIONLETTERS>ooooooo a" has 3 words
boundary conditions:
  • Texts with multiple spaces between words.
  • Texts bigger than 2GB
  • Words which contain a dash but no whitespace.
  • Non-ascii words. (e, g: Ellipsis, registerd sign, gegrees)
  • Files in some different encoding (if your program supports that)
  • Characters which are surrounded by whitespace but do not contain any word characters (e.g. "hello - world")
  • Texts without any words
  • Texts with all words on a single line

Get Input From User and Print one word per Line.



#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
      int tab=0, new_line=0, blank=0, i, j;
      char string[500];
      char ch;
      printf("Enter data. Press \'.\' when complete.\n");
 
      i=0;
      while(ch!='.')
      {
                    ch=getche();
                    string[i]=ch;
                    if(ch==13)
                    {
                              printf("\n");
                              string[i]='\n';
                             
                    }
                    if(ch==32 || ch==9)
                    {
                              string[i]='\n';        
                    }
                    i++;        
      }
      printf("\n");
      for(j=0; j<=i; j++)
      {
               printf("%c", string[j]);      
      }
      getch();  
}

Program to print a histrogram of the frequencies of different characters in its input.



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

int main()
{
    int word_no[500], j, count_char=0,;
    char ch;
          for(j=0; j<=10; j++)
      {
               word_no[j]=0;    
      }
    printf("Enter data. Press \'Esc\' when you complete typing.\n word containing more than 10 character is not included.\n");
      j=0;
      while(ch!=27)
      {
                    ch=getche();
                    if(ch==13)
                    {
                              printf("\n");
                              ++word_no[count_char];
                              count_char=0;
                              j++;
                         
                    }
                    if(ch==32 || ch==9 || ch==27)
                    {
                              ++word_no[count_char];
                              count_char=0;
                              j++;
                    }
                    if(ch!=32 && ch!=9 && ch!=27 && ch!=13)
                    {
                    count_char++;
                    }    
      }
      printf("\n");
      printf("index \t time\n");
      for(j=0; j<=10; j++)
      {
               printf("%d \t %d\n", j, word_no[j]);    
      }
    getch();
}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *