-
whisper("strings")
I am trying to rebuild a string that has already had its initial spaces removed. The problem is I'm not sure the end of the last word gets a '\0' stapled to it or not....ahh The string is user defined and I'm now trying to eliminate internal spaces..and especially any after the last character in the string..here's the function;
too much time has been spent....please throw me a line!!!!
im tryin'
Code:
void leading_spaces(char string[])
{
int i=0,c=0;
while(string[i]==' ') /*gets rid of leading zeros*/
{
i++;
}
while(string[i]!='\0') /*rebuilds string w/o leading spaces*/
{
string[c]=string[i];
c++;
i++;
}
string[c]='\0'; /*places terminator after last character*/
}
void all_spaces(char string[])
{
int i=0,c=0;
while(string[i]!='\0'&&string[i]!=' ')
{
string[c]=string[i];
i++;
c++;
while(string[i]==' '&&string[i]!='\0')
{
i++;
}
while(string[i]!='\0')
{
i--;
string[c]=string[i];
c++;
i++;
puts(string);
}
}
string[c]='\0';
}
void display_line(char string[])
{
int i=0;
while(string[i]!='\0')
{
while(string[i]!=' '&&string[i]!='\0')
{
printf("%c",string[i]);
i++;
}
if(string[i]!='\0')
{
printf("\n");
i++;
if(string[i]==' '||string[i]=='\0')
string[i]='\0';
}
}
}
[code][/code]tagged by Salem
-
>The string is user defined and I'm now trying to eliminate internal spaces
Well...it seems you're doing it the hard way:
Code:
#include <stdio.h>
#include <ctype.h>
char *remspace ( char *s )
{
char *p, *q;
for ( p = q = s; ( *p = *q ) != '\0'; q++ ) {
if ( !isspace ( *q ) )
p++;
}
return s;
}
int main ( void )
{
char a[] = " This is a test ";
printf ( "%s\n", remspace ( a ) );
return 0;
}
-Prelude
-
I'm confused, what's your question again? Which part are you having problems with, and what exactly are you trying to do with it?
string is a reserved name, so you shouldn't use it.
You can use isspace() to test a character to see if it's a space (meaning white space, \n \t space etc etc)
Here's a function that will remove all spaces from the input string. Maybe this will help you understand one way of doing things.
Code:
#include <stdio.h>
#include <ctype.h>
void remove_all_spaces(char *s)
{
char *p = s;
if (p != NULL)
{
while (*p != '\0')
{
if (isspace(*p) == 0)
*s++ = *p;
p++;
}
*s = '\0';
}
}
int main(void)
{
char data[] = " This is a test ";
printf("Before: >%s<\n", data);
remove_all_spaces(data);
printf("After : >%s<\n", data);
return 0;
}
/*
Output
Before: > This is a test <
After : >Thisisatest<
*/
[edit]Darn it beat just before submitting!
-
Im trying to learn c from the book and I was given a set of problems from a friend(without solutions) and the one I am presently working on is the following; allow a user to enter a string of characters and eliminate any leading spaces then eliminate any unwanted spaces within. I knew how to eliminate the leading spaces(see pathetic function) however once I started to eliminate the internal spaces I wasn't quite sure how to retain the single space between words, once I started it seemed I just got deeper and deeper in the &*^%(so I figured if somebody had some better solutions, I could figure it out a bit easier......anyways I'm sure there are tons of different ways to tackle this one....thanks for your valuable time (I know I wasted enough of mine trying to figure it out properly)
cheers
-
Yet another... and without pointers :D
Code:
#include <stdio.h>
#define MAX 80
void spaces(char aString[]);
int main(void)
{
spaces(" a string with spaces .");
printf("\n\nWell....alrighty then\n");
return 0;
}
void spaces(char aString[])
{
char buff[MAX];
int store[MAX];
int i, j, k;
j=0;
k=0;
for(i=0; aString[i] != '\0'; i++)
{
if(aString[i] != ' ')
{
store[j] = 1;
buff[k++] = aString[i];
}
else
store[j] = 0;
j++;
}
buff[k] = '\0';
printf("%s\n", aString);
printf("%s\n", buff);
j=0;
for(i=0; buff[i] != '\0'; i++)
{
while(store[j++] != 1)
putchar(' ');
putchar(buff[i]);
}
}
-
thanks again for the input however i want to keep a space between the words in the string.....the initial user defined string is garbage, thats why i used the user string in the function...I want to edit it for future use....:(
-
This problem was discussed just 3 or 4 posts ago here.