Question

In Programming language C - How would I convert my words char array into a string...

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 <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

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("\n\n");

for(i = 0; i < wordNum; i++)

{

for(j = 0; words[i][j] != '\0'; j++)

{

printf("%c",words[i][j]);

}

printf("\n\n");

}

printf("\n%d",count);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

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 =(char *)malloc(length*sizeof(char*));//modified//you have to use (char *) inoder to conver void* to char *

//i don't understand what you are trying to do here,
//but you can strcmp to compare char* words
//if you want any further code, pls comment
//if will provide

//i'm giving you the sample for strcmp
/*
char str[6]="hel";
char *ar = (char *)malloc(5*sizeof(char));
ar[0]='c';ar[1]='e';ar[2]='l';

if(strcmp(str,ar)==0)
printf("Equal\n");
else printf("Not Equal\n");

*/
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("\n\n");

for(i = 0; i < wordNum; i++)

{

for(j = 0; words[i][j] != '\0'; j++)

{

printf("%c",words[i][j]);

}

printf("\n\n");

}

printf("\n%d",count);

}

Add a comment
Know the answer?
Add Answer to:
In Programming language C - How would I convert my words char array into a string...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

  • If you already answer this question, please skip, thanks C Programming. Fill in ... This program...

    If you already answer this question, please skip, thanks C Programming. Fill in ... This program will be called with one command line argument that contains a string followed by an asterisk and an integer. Print out the string as many time as indicated by the integer. For example, when called as prog Hi*3, you print HiHiHi. Hint: Look for the '*' starting from the back of the string. len = strlen(arg) gives you the string length. When you have...

  • Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...

    Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with a new string. * Append an s to the end of your input string if it's not a keyword. * Insert a period at the end, if you have an empty input string do not insert a period Problems: //Why does my code stop at only 2 input strings //If I insert a character my empty string case works but it's still bugged where...

  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • #include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* * Expected usage:...

    #include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* * Expected usage: * ./wc <words | lines> <file> * * If argv[1] is "words", then you should count the number of words. If it * is "lines", then you should count the number of lines. * * For example: * $ cat a.txt * a b c d * $ ./wc words a.txt * 4 * $ ./wc lines a.txt * 1 * * YOUR PROGRAM...

  • C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #include <stdio.h> #include...

    C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name    loadMusicFile // @Brief   Load the music database //          'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size; song++){       // Allocate memory for each individual character string       database[song] =...

  • Need help, i can't get a infinite loop to work, that terminates when a blank line...

    Need help, i can't get a infinite loop to work, that terminates when a blank line is inputted for the firstName. note: C programing langauge and using microsoft visual studios . #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <math.h> #include <stdbool.h> int main() {    //int arraySalaries[50];    int calculate_avg = 0;    //int calculate_max;    //int calculate_min;    int salary[50];    //int length;    //int average_salary;    char firstName[50][50];    char lastName[50][50];    int i...

  • T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer...

    T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer is true or false in order to receive credit. #include <stdio.h> #include <string.h> int run_through(int num, char **a) { int i; int check=0; for(i=0;i<num;i++) { printf("%s\n", *(a+i)); if(strcmp(*(a+i), "filename")==0) { check=1; } } return check; } char** find_filename(int n, char **b) { int i; int check=0; for(i=0;i<n;i++) { if(strcmp(*b, "filename")==0) { b++; break; } b++; } return b; } int main(int argc, char **argv)...

  • System Programming in C

    Explain what the problem is within the program. Fix the problem when you create a child process per column. The code is given below. So basically, after the child processes have successfully excuted their code, the final print statement does not give the correct values. It prints the original matrix rather than the multiplied matrix.#include  #include  #include  #include  int main(int argc, char *argv[]){ int row = 0; int column = 0; row = atoi(argv[1]); column = atoi(argv[2]); int *A = ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT