For this program, credit goes to my friend Suroj Karmacharya (SKAMMA)
/*Write a program that removes all the extra spaces, tables or any other wild or special characters from a given sentence and make a simple English sentence ended with (.).
* Example: Input: I like $^ C++ very much
* Output: I like C++ very much.*/
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
char word[100],temp[100];
int i,j=0;
printf("Enter your sentence: ");
gets(word);
for(i=0;word[i]!='\0';i++)
{
if (word[i]==' ')
{
if (word[i-1]==' ' || i==strlen(word))
continue;
else
{
temp[j]=word[i];
j++;
continue;
}
}
if(isalnum(word[i])||word[i]=='+')
{
temp[j]=word[i];
j++;
continue;
}
}
temp[j]='.';
temp[j+1]='\0';
printf("The output is: \n%s",temp);
return 0;
}
Another Way:
-------------
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
char name[50]={'\0'},temp[50]={'\0'},final[50]={'\0'};
int i,j=0;
printf("Enter a sentence: ");
gets(name);
for(i=0;i<strlen(name);i++)
{
if(name[i]=='+' || name[i]==' ' || name[i]=='.' || (toupper(name[i])>='A' && toupper(name[i])<='Z'))
{
temp[j]=name[i];
j++;
}
}
j=0;
for(i=0;i<strlen(name);i++)
{
if(temp[i]==' ' && temp[i+1]==' ')
i-=1;
final[j]=temp[i];
j+=1;
}
puts(final);
return 0;
}
No comments:
Post a Comment