2
for (i = 0; i < size; i++) {
if((string[i] >= 65 && string[i] <= 90) || (string[i] >= 97 && string[i] <= 122) || string[i] == 10) {
    sec_str[j] = tolower(string[i]);
    putchar(sec_str[j]);
    j++;
}
}
printf("%s\n", sec_str);

This is my code, was trying to copy one string to another one, stripping all non letter cases, and that works fine for me, I use putchar(sec_str[j]) to check and they are all good, but when I check with the printf("%s", sec_str), the output was a mess. something like this:

asantaspotstopsatnasa
asantaspotstopsatnasa

twasbrilligandtheslithytoves
twasbrilligandtheslithytoves
����r�$
yobananaboy
yobananaboy
ndth
neveroddoreven
neveroddoreven
h
thetimehascomethewalrussaid
thetimehascomethewalrussaid
����t�r�$

and the printf should print

asantaspotstopsatnasa
twasbrilligandtheslithytoves
yobananaboy
neveroddoreven
thetimehascomethewalrussaid

to be correctly

1 Answer 1

4

You forgot to add a null terminator to the end of the string :)

#include <stdio.h>
#include <ctype.h>
...
  for (i = 0; i < size; i++) {
    if(ischar(string[i]) ||  (string[i] == 10) ) {
      sec_str[j] = tolower(string[i]);
      putchar(sec_str[j]);
      j++;
    }
  }
  sec_string[j] = '\0';
  printf("%s\n", sec_str);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.