Question: In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and
In Programming language C
- How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words?
#include
#include
#include
#include
int main(int argc, char*argv[]){
int i =0;
int j =0;
int count =0;
int length = strlen(argv[1]);
for(i =0; i < length; i++){
if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){
count ++;
}
printf("%c",argv[1][i]);
}
char *strings;
int wordNum =0;
int charNum =0;
strings =malloc(length*sizeof(char*));
char words[count][length];
for(i = 0; i < length; i++){
strings[i] = argv[1][i];
}
for(i = 0; i < length; i++){
if(isalpha(strings[i])!=0){
words[wordNum][charNum] = strings[i];
charNum++;
}
else{
words[wordNum][charNum] = '\0';
wordNum++;
charNum =0;
}
}
printf(" ");
for(i = 0; i < wordNum; i++)
{
for(j = 0; words[i][j] != '\0'; j++)
{
printf("%c",words[i][j]);
}
printf(" ");
}
printf(" %d",count);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
