-
anther one not working
Anther one i cant get to work
This one is for removing the extra white spaces in a string of characters
Thank in advance for ure effort and co-operation...
#include<stdio.h>
#include<stdlib.h>
#define nonblank 'a'
int main(int argc, char *argv[])
{
char buffer[100];
int i,lastc;
lastc=nonblank;
printf("Enter string to be read:\n");
fgets(buffer);
for(i=0;i<sizeof(buffer);i++)
{
if(buffer[i]!='\0')
{
if(buffer[i]==' ')
printf("%c",buffer[i]);
if(buffer[i]==' ')
{
if (lastc!=' ')
printf("%c",buffer[i]);
}
lastc=buffer[i];
}
}
return EXIT_SUCCESS;
}
-
no offense, but that's a mess...
maybe you could comment it a little bit, and then use spacing to help tell which brackets go with which. As it is, I couldn't make heads or tails of it. Well, I didn't try that hard, but I don't think anyone will with it in it's current form.
dragonB
-
oopsey ... sorry abt that...here it is again with the comments...
#include<stdio.h>
#include<stdlib.h>
#define nonblank 'a'
int main(int argc, char *argv[])
{
char buffer[100];
int i;
char c,lastc;
lastc=nonblank;/*to set valuf os lastc as ny non whitespace character*/
printf("Enter string to be read:\n");
gets(buffer);//gets the string stored in buffer
for(i=0;i<sizeof(buffer);i++)/*loop to read string till end of buffer is reached */
{
if(buffer[i]!='\0')//checks whether end of buffer that is NUL string is reached
{
if(buffer[i]==' '||c=='\n'||c=='\t'||c=='v'||c=='\f'||c=='\r')
{
if (lastc!=' '||c=='\n'||c=='\t'||c=='v'||c=='\f'||c=='\r')
printf("%c",buffer[i]);
}/*loop to check if character is white space and if charcater bfore that is also white space then ignore the charcater which must a white space*/
lastc=buffer[i];
}
}
return EXIT_SUCCESS;
}
-
Member
It's still a mess.
Use the CODE tag. Please?
OK. Notice you never set the value of c; also you fail to increment i.
I'm guessing the code you want for the loop is
Code:
for ( i = 0; i < sizeof(buffer); i++ ) {
if ( buffer[i] != '\0' ) {
if( (c = buffer[i])==' ' || c=='\n' || c=='\t' || c=='v' || c=='\f' || c=='\r' )
if ( (c = lastc)!=' ' || c!='\n' || c!= '\t' || c!='v' || c!='\f' || c!='\r' )
printf("%c",buffer[i]);
lastc = buffer[i];
}
}
Why does 'v' count as whitespace? Also, there seem to be a few logic errors, and this is all rather untidy. I believe stdlib.h has an iswhitespace function or macro. So your code becomes
Code:
for (i = 0; i < sizeof(buffer) && buffer[i] != '\0'; lastc = buffer[i++])
if (!iswhitespace(buffer[i]) || !iswhitespace(lastc))
printf("%c", buffer[i]);
A bit neater hey?
-
Thanx..:)
Hi strangerstill..
Thanks so much for your help.....sorry for the mess...am still learning the tricks here 
Finally managed to get it to run..heres the complet code for nyone who might be interested
This program is for removing excess white space characters from a string of characters using a function called 'deblank'
Code:
int i;
void deblank(char string[])
{
char c;
char lastc;
lastc='a';
i=0;
while(string[i]!='\0')
//checks whether end of string i.e. NUL value is reached
{
c=string[i];
if(c==' '||c=='\n'||c=='\t'||c=='\v'||c=='\f'||c=='\r')
{
if (lastc!=' '&&lastc!='\n'&&lastc!='\t'&&lastc!='\v'&&lastc!='\f'&&lastc!='\r')
printf("%c",c);
}
/*loop to check if character is white space and if character bfore that is also white space then ignore the character */
else{printf("%c",c);}
lastc=c;
i++;
}
}
int main(int argc, char *argv[])
{
char buffer[100];
printf("Enter string to be read:\n");
gets(buffer);//stores whole line as a string in buffer
deblank(buffer);
return EXIT_SUCCESS;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke
|
Bookmarks