I am currently trying to solve a challenge and to solve a part of it I first need to turn words a user inputs from the gets() function into a two dimensional array.
Like for example if the user inputs: Hey there everyone
I have to make a two dimensional array where every row contains a word from that input.
like row 1 will contain the word "Hey", row 2 "there" and so on.
So heres what I came up with:
(I tried using the "code" bbcode but for some reason it was not printing it all)
It works some times but other times it prints out random symbols along with the word..Meaning that it is also printing the unfilled parts of the array.
But it shouldn't since in the "conversion loop"(line 48 on codeblocks) I gave the condition that if the character is ' ' or ' ' the loop will break but the array will be assigned a special char '\/' at that value.
So during the loop I used to print out the contents of the array I gave a "if condition"(line 76 on cb) that if the character '\/' is found the loop will break and not continue.
I'm a complete novice at this e.e.. I know I made a mistake somewhere but I can't find out where. So could someone please help me?
Also I'm sorry if everything sounded a bit confusing, English is not my first language soo >.<... But if you run the code you'll get what I'm talking about.
Turning words in a string into a two dimensional array
Post a Reply
|Oldest Newest Rating|
· November 23, 2015
Can I suggest that you rewrite this so that you can make it easier for yourself to read.
I haven't actually compiled this to see if it works, so you might have to fix it slightly.
The point here is, you can set inputString to whatever you like in the program and test if that function works.
inputString = "This is my test string";
printf("inputString has %d spaces\n", getNumberOfSpaces(inputString));
Ask yourself if this function does what you expect. If it doesn't then you know that the problem is with your code in the function.
Remember, if you change the function to add 1 before it returns, then you should change the name of your function to getNumberOfWords, or
whatever makes sense for what you are doing.
Hope this helps.
One of the most popular languages of all time.
|Bucky Roberts Administrator|