0

I got a program that split up words. Like if I write "My name is ali" The program will write

Word one: My
Word two: name
word three: is
word four: ali

Now I want to add something like

int number;
printf("which word do you want to write? number: \n");
scanf("%d",&number)

Then if user writes "four" it is suppose to write ali or if they write "two" it is supposed to write name.

int delaUppText(char input[], char* pekare[])
{
    int i=0, j=0;
    char prevLetter=' ';

    for(i=0; input[i]!=NULL; i++)
    {
        if(prevLetter==' '&&input[i]!=' ')
        {
            pekare[j]=&input[i];
            j++;
        }
        prevLetter=input[i];
    }

    for(i=0; input[i]!=NULL; i++)
    {
        if (input[i] == ' ')
        {
            input[i]=NULL;
        }
    }

    return j;
}

int main()
{
    char input[200];
    char* pekare[100];
    int i, antalPekare;
    char answer = 32;
    int nummer;

    do
    {
        system("cls");
        printf("Enter a string: ");

        gets(input);

        printf("\n");

        antalPekare = delaUppText(input, pekare);

        for(i=0;i<antalPekare;i++)
        {
            printf("Ord %d: %s\n", i+1, pekare[i]);
        }

        printf("\nWould you like to try again? [Space]/[q]");
        answer = getch();

        if (answer == 32)
        {
         system("cls");
        }
        else if (answer == 'q')
        {
            break;
        }
    }while (answer == 32);

    return 0;
}
2
  • Lets say you downvote, please leave a comment on why. I really do not see why this question should be downvoted. Commented Mar 18, 2016 at 14:05
  • 2
    What's the question? What has the OP tried? Commented Mar 18, 2016 at 14:45

2 Answers 2

0

Unforunately compilers don't speak English.

So if your user has to enter "four", then don't expect that scanf("%d",&number) will recognize the word "four". it will recognize only digits, so your user must enter "4", or you must parse the input to learn what number-word he entered. How about input in German?

Sign up to request clarification or add additional context in comments.

Comments

0

1

Looking over your code I find that there are much better ways of doing this. One such way is the use of strtok you can find more about it here.

#include <string.h>
char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr);

http://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm

Or you can also use strsep

#include <string.h>
char *strsep(char **stringp, const char *delim);

2

Now if you want to make it work with what you have. You need to use heap memory. aka [malloc, calloc]. The reason is once you return to your main the stack of the program will be reduced and the memory on the stack that was once in you program will be no more. Now if you use the heap then you should also make sure you free the memory once and only once.

For the rest of the problem I am going to leave you to read this.

In C/C++, is char* arrayName[][] a pointer to a pointer to a pointer OR a pointer to a pointer?

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.