【发布时间】:2015-09-22 17:15:25
【问题描述】:
我目前在 reddit 上做 this challenge。
现在我的问题在程序的底部(未完成),但我发布了整个内容,以防它可能与它的顶部有关。
现在我试图通过将所有非空格字符放入另一个字符数组中来从用户应该输入的字符串中删除空格。但是,每次我打印尝试打印带有假定已删除空格的字符串时,它只打印第一个单词。但是,我希望它将所有单词打印成一个单独的组合单词。
例如,当我尝试转换Hello World! 时,它应该给我HelloWorld!,但它只给我Hello。我用指针和诸如此类的东西尝试了不同的方法,每次我遇到同样的问题。这是怎么回事?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(void)
{
char StringtobePacked[100]; //This is the string the user puts in
char StringtobePackedWOspaces[100]; //This is gonna be the string without spaces
int i , o , p; //incrementers/counters
double AmtfChrsnStrng;
//ask user for sentence input:
printf("Please type in sentence so I can pack it into the array:\n");
scanf("%s" , StringtobePacked);
//check for the amounts of letters in the sentence minus the spaces
for (i = 0; StringtobePacked[o] > i ; i++ , o++)
{
AmtfChrsnStrng++;
if (StringtobePacked[o] == ' ')
{
AmtfChrsnStrng--;
};
};
//now we know the amounts of letters in the sentence minus the spaces
//find the root and make that into the array size
double root = sqrt(AmtfChrsnStrng); //This is to find the square root of the (number) of letters in the string
int rootup = ceil(root); //this is to round up the double
char PackingArray[rootup][rootup]; //have a two dimensional array to pack sentence into
//make sure to check wether a sign is a space or not so as to not pack these
for (i = 0 , o = 0; StringtobePacked[i] != 0; i++)
{
if (StringtobePacked[i] != ' ')
{
StringtobePackedWOspaces[o] = StringtobePacked[i];
o++;
}
}
StringtobePackedWOspaces[o] = 0;
puts(StringtobePackedWOspaces);
//loop through the sentence in order to pack it into the array
// after end of column has been reached increment row
//now don't keep incrementing the column but increment it backwards so letters can be packed upwards
//print array by looping through , first columns then rows
//the starting position of the packing should be randomised
};
【问题讨论】:
-
0)
scanf("%s" , StringtobePacked);-->scanf("%99[^\n]" , StringtobePacked);1)StringtobePacked[o] > i-->StringtobePacked[o] != '\0'