[b][red]This message was edited by the beginnerCpls at 2002-3-29 14:18:7[/red][/b][hr]
Can anyone tell me how to write a program using C++ array and pointer to delete extra spaces from an input string? For instance, an user inputs a string like this "This_______is______an___example." after the program runs, the output should be like this "This is an example." Thank you!
Best Regards,
Jackey
Comments
: Can anyone tell me how to write a program using C++ array and pointer to delete extra spaces from an input string? For instance, an user inputs a string like this "This_______is______an___example." after the program runs, the output should be like this "This is an example." Thank you!
:
: Best Regards,
: Jackey
:
:
:
I worked on this for a little bit, after I read your question.
I got some coding done, but there is a bug in it. If you care to see it, and try to fix the bug, then I'll post it.
: Can anyone tell me how to write a program using C++ array and pointer to delete extra spaces from an input string? For instance, an user inputs a string like this "This_______is______an___example." after the program runs, the output should be like this "This is an example." Thank you!
[/italic]
[code]
[b]main.cpp[/b]
[blue]#include[/blue]
[blue]#include[/blue]
[blue]#include[/blue]
[blue]int[/blue] main ()
{
[blue]char[/blue] s[] = "This is an example";
size_t slen = strlen(s);
[blue]for[/blue] ([blue]unsigned[/blue] [blue]int[/blue] i = 0, j = 0; s[i] != ''; i++, j++)
{
[blue]if[/blue] (s[i] == ' ')
{
[blue]for[/blue] (++i, ++j; i < slen && s[i] == ' '; i++);
s[j] = s[i];
}
s[j] = s[i];
}
s[j] = '';
printf (s);
getch ();
}
[/code]
If there are spaces at the end of the string, they won't be removed. Maybe that's what you can add yourself
---
[b]edx[/b] - Member of [blue][b]CodeForce[/b][/blue] (http://codeforce.d2g.com)
He asked for a C++ source code........
I'm not too familiar with C, so I was wandering if you could explain how the for loop in your code gets implemented.
: [code]
: [b]main.cpp[/b]
:
: [blue]#include[/blue]
: [blue]#include[/blue]
: [blue]#include[/blue]
:
: [blue]int[/blue] main ()
: {
: [blue]char[/blue] s[] = "This is an example";
: size_t slen = strlen(s);
:
: [blue]for[/blue] ([blue]unsigned[/blue] [blue]int[/blue] i = 0, j = 0; s[i] != ''; i++, j++)
: {
: [blue]if[/blue] (s[i] == ' ')
: {
: [blue]for[/blue] (++i, ++j; i < slen && s[i] == ' '; i++);
: s[j] = s[i];
: }
: s[j] = s[i];
: }
: s[j] = '';
:
: printf (s);
: getch ();
: }
: [/code]
:
: If there are spaces at the end of the string, they won't be removed. Maybe that's what you can add yourself
:
: ---
: [b]edx[/b] - Member of [blue][b]CodeForce[/b][/blue] (http://codeforce.d2g.com)
:
:
: He asked for a C++ source code........
[/italic]
C source is not necessarily not C++ source... anyways
[italic]
: I'm not too familiar with C, so I was wandering if you could explain how the for loop in your code gets implemented.
[/italic]
What do oyu mean by "gets implemented"?
It scans the string till it arrives at a space (' ') and then it replaces all the additional spaces with characters that follow (replace position = j and real position = i).
Does that answer your question?
---
[b]edx[/b] - Member of [blue][b]CodeForce[/b][/blue] (http://codeforce.d2g.com)
: He asked for a C++ source code........
: I'm not too familiar with C, so I was wandering if you could explain how the for loop in your code gets implemented.
[red]
for loops are implemented the exact same way in C as in C++
A C++ compiler (all of them I know of) will compile C code. As a matter of fact you can use C functions in your C++ code. They should work just fine.
[/red]
:
: : He asked for a C++ source code........
: : I'm not too familiar with C, so I was wandering if you could explain how the for loop in your code gets implemented.
: [red]
: for loops are implemented the exact same way in C as in C++
: A C++ compiler (all of them I know of) will compile C code. As a matter of fact you can use C functions in your C++ code. They should work just fine.
: [/red]
:
:
I was talking about this for loop:
for (unsigned int i = 0, j = 0; s[i] != ' '; i++, j++)
It's the first time I saw this syntax, and I wanted to know how the loop gets processed.
Whether it processes i first then j, or i and j at the same time.
Anyways, you did answer my question FELIX2002.
thanks
[snip - code]
: If there are spaces at the end of the string, they won't be removed. Maybe that's what you can add yourself
This algorithm will take care of duplicate spaces anywhere in the string:
[code=ffffff]
[color=000080]void[/color] collapseDuplicateSpaces ([color=000080]char[/color]* s) {
[color=000000][b]for[/b][/color] ([color=000080]char[/color]* r=s; ; ++r)
[color=000000][b]if[/b][/color] (!(*r == [color=bb0000]' '[/color] && r[[color=bb0000]1[/color]] == [color=bb0000]' '[/color]) && (*s++ = *r) == [color=907050]' '[/color])
[color=000000][b]break[/b][/color];
}
[/code]
Cheers,
Eric
:
: [snip - code]
:
: : If there are spaces at the end of the string, they won't be removed. Maybe that's what you can add yourself
:
: This algorithm will take care of duplicate spaces anywhere in the string:
: [code=ffffff]
: [color=000080]void[/color] collapseDuplicateSpaces ([color=000080]char[/color]* s) {
: [color=000000][b]for[/b][/color] ([color=000080]char[/color]* r=s; ; ++r)
: [color=000000][b]if[/b][/color] (!(*r == [color=bb0000]' '[/color] && r[[color=bb0000]1[/color]] == [color=bb0000]' '[/color]) && (*s++ = *r) == [color=907050]' '[/color])
: [color=000000][b]break[/b][/color];
: }
: [/code]
: Cheers,
: Eric
:
:
Your function worked perfectly, but I like to return the pointer, for certain reasons.
[code]
char *collapseDuplicateSpaces (char* s) {
char *x = s;
char *r;
for (r=s; ; ++r)
if (!(*r == ' ' && r[1] == ' ') && (*s++ = *r) == ' ')
break;
return x;
}
[/code]
that way I can use it like this
[code]
printf("This is the message: %s
", collapseDuplicateSpaces(messageString));
[/code]