-->

WAP to replace all white spaces with a character.

in turboc++
---------------------------------------------------------------------------------
//program to replace all white space with a character.
#include <stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
    char string[100],sec;
    int i;
    printf("enter first string\n");
    gets(string);//accepts string
    printf("enter a character\n");
    sec=getchar();//accpts a character
    printf("before replacing,string is %s\n",string);
    for(i=0;string[i]!='\0';i++)//loop execution till null character
    {
        if(string[i]==' ')//condition testing for white space
        {
            string[i]=sec;//assigning character
        }
    }
    printf("after replacing, the string=%s",string); //prints the string.
  getch(); 
  return 0;
}
----------------------------------------------------------------------------------
in codeblocks:
-----------------------------------------------------------------------------
//program to replace all white space with a character.
#include <stdio.h>
#include<string.h>
int main()
{
    char string[100],sec;
    int i;
    printf("enter first string\n");
    gets(string);//accepts string
    printf("enter a character\n");
    sec=getchar();//accpts a character
    printf("before replacing,string is %s\n",string);
    for(i=0;string[i]!='\0';i++)//loop execution till null character
    {
        if(string[i]==' ')//condition testing for white space
        {
            string[i]=sec;//assigning character
        }
    }
    printf("after replacing, the string=%s",string); prints the string.
    return 0;
}




No comments:

Post a Comment