shaadi.com

Searching...
Wednesday, 12 June 2013

Assignment4

/* Assignment
Program no. EX4.c */
void main()
{

int i, j, temp, cnt=0, len, m, n, ch;
char str[51],s[51],s2[51];
clrscr();

printf("\n\n Enter a string: ");
gets(str);
flushall();

// Delete Trim (Delete left-side spaces)
for(i=0;str[i]==' ';i++);
for(j=0; str[i]!='\0';i++,j++)
{

s[j]=str[i];
}
s[j]='\0';

//Replace each string of one or more blanks by a single blank
for(i=0;s[i]!='\0';i++)
{
if(s[i]==' ')
{
if(s[i+1]==' ')
{
x:
j=i;
while(s[j]!='\0')
{
s[j]=s[j+1];
j++;
}
if(s[i+1]==' ')
goto x;
s[j]='\0';
}
}
}

// Copied string s in s2
for(i=0;s[i]!='\0';i++)
s2[i]=s[i];

for(i=0; s[i]!='\0';i++)
{
temp=i;
if(s[i]==' ') //Count spaces
{
cnt++;
}
if(cnt%2!=0)   //Odd blank spaces follows even words
{
j=0;
for(j=i+1,len=0; s[j]!=' ' && s[j]!='\0'; j++,++len); //Calculate word length

for(m=temp+1, n=temp+len; s[m]!=' ' && s[m]!='\0'; m++,n--) // Reverse the word
{
s[m]=s2[n];
}
i=m-1;
}
}

printf("\n The manipulated string: %s",s);
printf("\n\n\n Press any key to exit....");
getch();

}

0 comments:

Post a Comment