Click here to Skip to main content
15,888,007 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
‘a tailor has become a sailor after he has experienced a voyage with his captain friend’. In the above given sentence how to store the words in a two dimensional array like
array = {"a", "tailor", "has", "become", "a", "sailor", "after", "he", "has", "experienced", "a", "voyage", "with", "his", "captain", "friend"}

My code is:
#include <stdio.h>
#include <string.h>

int main()
{
    char line[1000];
    scanf("%[^\n]", line);
    int l=strlen(line),i,count=0;
    for(i=0;i<l;i++){
        if(line[i]==' '){
            count+=1;
        }
    }
    
    char arr_word[count+1][26];
    int j,ind=0;
    for(i=0;i<count+1;i++){
        for(j=0;j<26;j++){
            if(line[ind]==' '){
                ind++;
                break;
            }
            arr_word[i][j]=line[ind];
            ind++;
        }
    }
    return 0;
}


What I have tried:

I have tried the above given code but it stores some garbage value at the end of each word. Please help me.
Posted
Updated 17-Jul-23 7:29am
v4
Comments
Rick York 27-Feb-21 2:59am    
Your logic was cut off.

The reason you see garbage is because you are not storing the terminating null character of the strings. You always need to store one more character than string's length to account for that null character.

Why are you using the literal value 26? That should a macro definition, or a constant value in C++. Literal values should be avoided, especially when they are used in multiple places.
 
Share this answer
 
You need to initialize the arr_word array with a null terminator first because an uninitialized array has the potential to store garbage values.

And also, you need to added an additional condition to break the loop when it encounters a null terminator in the sentence ("line" array). This prevents the loop from storing a non-existing character, which could result in the output of a garbage value.
C
#include <stdio.h>
#include <string.h>

int main()
{
    char line[1000];
    scanf("%[^\n]%*c", line);
    int l=strlen(line),i,count=0;
    for(i=0;i<l;i++){
        if(line[i]==' '){
            count+=1;
        }
    }
    
   
    
    char arr_word[count+1][26];
    int j,ind=0;
    
/* intialize arr_word array */
    
     for(i=0; i <count+1; i++){
        for(j = 0; j <26; j++){
        arr_word[i][j] = '\0';
        }
    }
    for(i=0;i<count+1;i++){
        for(j=0;j<26;j++){
            if(line[ind]==' ' || line[ind]=='\0'){
                ind++;
                break;
            }
            arr_word[i][j]=line[ind];
            ind++;
        }
    }
    
    for(i=0; i <count+1; i++){
        for(j = 0; j <26; j++){
            printf("%c", arr_word[i][j]);
        }
        
        printf("\n");
    }
    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900