简体   繁体   中英

Why aren't the characters being appended to the end of the string?

My program takes a string input from the user, looks through the string, finds a dash and prints any letters in between the two letters beside the dash (exercise 3-3 in "The C Programing Language"). My program always either adds too many letters or not enough. Here is the code that I think is responsible for the bug:

for (i=0; i<(strlen(stringin));i++, w++){
        c = stringin[i];
    
        /*finds a - and logs the letters before*/
        if(c == '-'){

            printf("found - ");
            logpre = stringin[i-1];
            printf("%d ", logpre);
            logpost = stringin[i+1];
            printf("%d\n", logpost);
            
            /*logs newly generlated letters to string*/
            for(o=logpre+1; logpre <= logpost; w++, o++){stringout[w]=o;} 
        }
        else{
            /*logs unused letters to string*/
            stringout[w]=c;
            printf("%d\t%d\n", c, w);
        }
    }

print statements are there because who knows how to debug without them?

  • didn't know what to try, but this is the current input/output:
a-z 0-9
String Length- 7
97  0
found - 97 122
211 7005
212 7006
213 7007
214 7008
55255   7009
14155992    7010
-654311207  7011
218 7012
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
-

Input =

�����������������������������������������������

Output =

abcdefghijklmnopqrstuvwxyz{|}\~��������������������������������������������������������������������������������������������������������������������������������

this is what I want it to look like:

a-z 0-9
String Length- 3
97  0
found - 97 122

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 31 48 49 50 51 52 53 54 55 56 57
-

Input = az 0-9 Output = abcdefghijklmnopqrstuvwxyz 0123456789

for (i=0; i<(strlen(stringin));i++, w++)

w++ here is wrong. It should be only incremented when you actually add to the output string. You also do not terminate you output string.

Do it much more simple way:

{
    size_t outPos = 0;
    if(str && *str && buff)
    {
        for(size_t pos = str[1] == '-'; str[pos]; pos++)
        {
            if(str[pos] == '-' && str[pos + 1])
            {
                for(char ch = str[pos - 1]; ch <= str[pos + 1]; ch++)
                    buff[outPos++] = ch;
                pos++;
            }
            else buff[outPos++] = str[pos];
        }
        buff[outPos++] = 0;
    }
    return buff;
}

int main(void)
{
    char buf[500];
    printf("\"%s\"\n", print("asa a-z hello 0-3 wrewertyrt 5-W", buf));
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM