Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Thursday, 9 May 2013

Pattern of strings-2



DESCRIPTION: This program prints the string pattern as below-
                                                                 hello
                                                                 hell
                                                                 hel
                                                                 he
                                                                 h


PROGRAM:


#include<stdio.h>
main()
{
    char str[50];
    int i,j,n;
    printf("enter the string\n");
    scanf("%s",str);
    for(i=0;str[i]!='\0';i++);
    n=i;
    for(i=0;i<n;i++)
    {
        for(j=0;j<i;j++)
        printf(" ");
        for(j=i;j<n;j++)
        printf("%c",str[j]);
        printf("\n");
    }
}



OUTPUT WINDOW:





Pattern of strings-1



DESCRIPTION: This program prints the string pattern as below-
                                                                hello
                                                                  ello
                                                                    llo
                                                                     lo
                                                                      o


PROGRAM:



#include<stdio.h>
main()
{
    char str[50];
    int i,j,n;
    printf("enter the string\n");
    scanf("%s",str);
    for(i=0;str[i]!='\0';i++);
    n=i;
    for(i=0;i<n;i++)
    {
        for(j=0;j<i;j++)
        printf(" ");
        for(j=i;j<n;j++)
        printf("%c",str[j]);
        printf("\n");
    }
}



OUTPUT WINDOW:



Count number of words in a text


DESCRIPTION: This program checks for the number of words present in the text using the spaces in the text to distinguish words.


PROGRAM:


#include<stdio.h>
main()
{
  char a[50],i,count=1;
  printf("Enter the string\n");
  gets(a);
  for(i=0;a[i]!='\0';i++)
  {
      if(a[i]==' '&&a[i+1]!=' ')
      count++;
  }
  printf("Number of words =%d",count);
}



OUTPUT WINDOW:



Deleting character from a text


DESCRIPTION:This program makes use of the keyword continue to delete the appropriate character present in the string or the text.
The continue statement passes control to the next iteration of the nearest enclosing dofor, or while statement in which it appears, bypassing any remaining statements in the dofor, or while statement body.


PROGRAM:

#include<stdio.h>
main()
{
  char a[50],b[50],i,j,ch;
  printf("Enter the string\n");
  gets(a);
  printf("Enter the character to be deleted\n");
  scanf("%c",&ch);
  for(i=0,j=0;a[i]!='\0';i++)
  {
      if(a[i]==ch)
      continue;
      b[j]=a[i];
      j++;
  }
  b[j]='\0';
  puts(b);
}


OUTPUT WINDOW:








Delete unnecessary spaces in a text


DESCRIPTION: This program makes use of the keyword continue to delete the unnecessary spaces present in the string.The continue statement passes control to the next iteration of the nearest enclosing dofor, or while statement in which it appears, bypassing any remaining statements in the dofor, or while statement body.


PROGRAM:


#include<stdio.h>
main()
{
  char a[50],b[50],i,j;
  printf("Enter the string\n");
  gets(a);
  for(i=0,j=0;a[i]!='\0';i++)
  {
      if(a[i-1]==' '&&a[i]==' ')
      continue;
      b[j]=a[i];
      j++;
  }
  b[j]='\0';
  puts(b);
}


OUTPUT WINDOW:


Delete spaces from a string


DESCRIPTION: This program makes use of the keyword continue to delete the spaces present in the string.
The continue statement passes control to the next iteration of the nearest enclosing dofor, or while statement in which it appears, bypassing any remaining statements in the dofor, or while statement body.


PROGRAM:

#include<stdio.h>
main()
{
  char a[50],b[50],i,j;
  printf("Enter the string\n");
  gets(a);
  for(i=0,j=0;a[i]!='\0';i++)
  {
      if(a[i]==' '&&a[i+1]!=' ')
      continue;
      b[j]=a[i];
      j++;
  }
  b[j]='\0';
  puts(b);
}


OUTPUT WINDOW:


Delete vowels from a string



DESCRIPTION: This program makes use of the keyword continue to delete the vowels present in the string.
The continue statement passes control to the next iteration of the nearest enclosing dofor, or while statement in which it appears, bypassing any remaining statements in the dofor, or while statement body.


PROGRAM:

#include<stdio.h>
main()
{
  char a[50],b[50],i,j;
  printf("enter string\n");
  gets(a);
  for(i=0,j=0;a[i]!='\0';i++)
  {
      if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U')
      continue;
      b[j]=a[i];
      j++;
  }
  b[j]='\0';
  puts(b);
}


OUTPUT WINDOW: