I am trying to remove the spaces from my array "secuencia", the users give me this entry:
"1 2 3 4 5 6 7 8 9"
I want to remove the spaces, and save it in another array for later. Then, convert to integer with "ATOI" like I do with the arrays "palancas" and "palancaroja". Those two arrays only contained one number, so I had no problem with them.
please help me... I am programming in ANSI C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char * argv[])
{
char palancas [20000];
int palancai;
char palancaroja[10];
int palancarojai;
char secuencia[20000];
char temp[20000];
int j = 0;
printf("Dame El Numero De Palancas:");
fgets(palancas, 20000, stdin);
printf("Dame La Posision De La Palanca Roja:");
fgets(palancaroja, 10, stdin);
palancai = atoi(palancas);
palancarojai = atoi(palancaroja);
printf("Dame La cadena");
fgets(secuencia, 20000, stdin);
for (int i = 0; i < palancai; i++) {
if (secuencia [i] != ' ') {
temp [i] = secuencia [i];
printf("%s", temp);
}
}
}
This is the simplest way to remove spaces from a string.
char *SourcePtr = secuencia;
char *TargetPtr = SourcePtr;
while (*SourcePtr != 0)
{
if (*SourcePtr != ' ')
{
*TargetPtr = *SourcePtr;
TargetPtr += 1;
}
SourcePtr += 1;
}
*TargetPtr = 0;
Translated version of critical section
for (int i = 0; i < length; i++) {
if (source[i] != ' ') {
temp[i] = source[i];
printf("%s", temp);
}
}
This code copies every character from the array source
to the array temp
, but simply skips spaces. So if temp
is initialized with XXXXX
and source
is ABC
, then temp
is AXBXC
after the execution of the loop.
You have use two indexes (see other answer)
#include <stdio.h>
//copy to d from s removed space
void remove_space(char *d, const char *s){
for(;*s;++s){
if(*s != ' ')
*d++ = *s;
}
*d = *s;
}
int main(){//DEMO
char secuencia[] = "1 2 3 4 5 6 7 8 9";
char temp[sizeof(secuencia)];
remove_space(temp, secuencia);
puts(temp);//123456789
return 0;
}
You could use strtok and tokenize the string that you get with the delimiter string being " ".
In other words:
char * tok;
int i = 0;
tok = strtok(secuencia, " ");
while(tok != NULL){
temp[i] = tok[0];
i++;
tok = strtok(NULL, " ");
}
This would only work if it's guaranteed that it's a single digit between each space though. Another way to copy it would be to use another loop, cycling through strtok until '\\0' is reached, or using strcpy.
first I think that your for loop is looking at the wrong variable. you are trying to loop on palancai where really you want to loop on secuencia.
Below you can find a function that will parse your int.
int MyIntParse(char* str)
{
int iReturn = 0;
for(int i=0;i<20000;++i)
{
iReturn *=10;
switch(str[i])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
iReturn = iReturn + (str[i] - '0');
break;
}
}
return iReturn;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.