-2

I am trying to print each char in s array one at a time. The program I have printed all the letters after the defined index in the array.

The problem I am facing is after that the user inserts the phrase then it is allocated. How do you call letters from the string using pointers?

Can you use strtok to split this string, I tried setting the delim to "" got no output. In other word make it a list like a Python split().

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>


int main() {
        
    char *s;
    s = malloc(1024 * sizeof(char));
    scanf("%[^\n]", s);
    s = realloc(s, strlen(s) + 1);
   
    //printf("%s \n", s);
        
    for (int i = 0 ; i <strlen(s); i++){
        printf("%s \n", &s[i]);        
    }
        
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
m3sfit
  • 65
  • 6
  • Why are you using `%s` to print a single character? – klutt Dec 12 '20 at 15:22
  • 2
    `for (int i = 0; s[i] != '\0'; i++) printf("%c\n", s[i]);`? – Jonathan Leffler Dec 12 '20 at 15:23
  • what should I use, `%ch`? – m3sfit Dec 12 '20 at 15:23
  • 1
    You should use `%c` – Krishna Kanth Yenumula Dec 12 '20 at 15:24
  • 1
    And use `s[i]`, not `&s[i]` – Robert Harvey Dec 12 '20 at 15:24
  • @JonathanLeffer can you please explain you for loop in detail, please – m3sfit Dec 12 '20 at 15:25
  • @m3sfit You should read the documentation for functions you're using. – klutt Dec 12 '20 at 15:28
  • See also [How to prevent buffer overflow in `scanf()`?](https://stackoverflow.com/q/1621394/15168). Also read POSIX [`scanf()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html) and note the `m` modifier ('optional assignment-allocation character'). If it is available on your system (it isn't on macOS X, for example), then you could use that instead of the `malloc()` and `realloc()`. You should also read the POSIX specification for [`printf()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html). – Jonathan Leffler Dec 12 '20 at 17:13
  • The loop I wrote steps through each element of the string, up to but not including the null terminator byte (avoiding the overhead of calling `strlen()` on each iteration, which an optimizer might or might not avoid), and uses the conversion specification to print a single character (plus a newline) and passes a single character, `s[i]`. The characters are already available individually by using array indexing on the string. – Jonathan Leffler Dec 12 '20 at 17:16

3 Answers3

2
printf("%c\n", s[i]);

or

printf("%c\n", *(s + i));
vmp
  • 2,370
  • 1
  • 13
  • 17
1
len = strlen(s);

for (int i = 0 ; i < len; i++){
    printf("%c\n", s[i]);        
}
anotherOne
  • 1,513
  • 11
  • 20
0
int main() 
{
    char *s;
    s = malloc(1024 * sizeof(char));
    scanf("%[^\n]", s);
    s = realloc(s, strlen(s) + 1);
    for (int i = 0; s[i] != '\0'; i++)
    {
        printf("%c \n", s[i]);
    }
    return 0;
}
MED LDN
  • 684
  • 1
  • 5
  • 10