Neurotrace wrote:So I've got my descrambler written in C++ but I'm having issues with time. I could cure this if I could find a way to check a char array for the first character that isn't a space or # then basically slide the entire char from that point on to the first position and then reassign the size of the char array so I don't have a bunch of trailing empty slots. Any suggestions?
#include <stdio.h>
#include <string.h>
int h_val(char * str)
{
int i=0;
int h_total=0;
while(*str != '\0')
{
if(*str == ' ' || *str == '#')
;
else
h_total+=*str;
++str;
}
return h_total;
}
int main()
{
char test[]="# abc";
int res= h_val(test);
printf("%i\n", res);
}
Neurotrace wrote:Oh ok. I get that. The only problem I see with that is that after a char[] has been initialized, you can't really change the size, right? Because I'm using that same array for all 6 or 8 or however many scrambled words there are and I don't think they're all the same length.
I do appreciate the "skip the characters" code though. Very helpful
int startCheck = 0;
cin.getline(stringy, 20);
l = strlen(stringy);
for (int i = 0; i < l; i++)
if(stringy.charat(i) != 32 && stringy.charat(i) != 34 && startCheck ==0) {
startCheck = i;
l = l - i;
}
char word[l];
int startCheck = 0; // This variable will be what determines where in the array is the letter
cin.getline(stringy, 20); // Just take in a given string, in this case a scrambled one
l = strlen(stringy); // Copies the length of the string. I use this to compare scrambled word length against wordlist
for (int i = 0; i < l; i++) // Standard for loop :P
if(stringy.charat(i) != 32 && stringy.charat(i) != 35 && startCheck == 0) { // if character at position i in the string is not a space or # and if the startCheck is equal to 0 then
startCheck = i; // Assign startCheck to first real character
l = l - i; // Take the extra length off
}
char word[l]; // Create array with that length
import fileinput
scrambled = 'scrambled.txt'
for line in fileinput.input(scrambled):
for x in line:
wrd = ""
if x != " ":
wrd = wrd+x
print wrd
raw_input("Press ENTER to continue...")
raw_input("Press ENTER to exit...")
import fileinput
scrambled = 'scrambled.txt'
for line in fileinput.input(scrambled):
show = line[0:-1]
print show
print len(show)
print
raw_input("Press ENTER to continue...")
print
raw_input("Press ENTER to exit...")
Users browsing this forum: No registered users and 0 guests