0

im trying to separate words from a sting, whem the code replaces the space for the ' \0' i need to increment the "pos" so i can continue read the array the code so far is this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
void separa_pal(char text[]);

int main(int argc, char** argv)
{
    char text[MAX] = "hello my darling";
    printf("text: %s \n", text);
    separa_pal(text);
    printf("%s\n", text);
}

void separa_pal(char text[])
{
    int i = 0;
    int pos = 0;
    pos = text[i]; //the problem is here
    while(text[i] != '\0')
    {
        if(text[i] == ' ')
        {
                text[i] = '\0';
                pos = text[i + 1]; // and here
            }else{
                i++;
            }
    }
}

btw i know of strtok but i need to do it without pointers so if you guys can help me i would apreciate it :)

Edited by claire89: n/a

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by csurfer
0

Whenever you define a string as

char str[]="Hello\0MIster\0HowAre You?";
printf("%s",str);

Every time the compiler stops outputing the characters of the string to the output console once it encounters a '\0' character.So even if you put '\0' character in place of ' ' character this won't do any good.
Instead you can use an array of character arrays and store every generated string in order.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.