Inputting and reversing multiple strings in C?
First time posting on this forum, however not the first time surfing through it! Quite helpful threads, now I feel it's my time to ask for some help so thanks in advance for those who can help me out.
I have the following lines of code: which upon input reverses the order of the words.
i.e.
INPUT: Hello there I am Batman
OUTPUT: Batman am I there Hello
Now I would like to allow the user to be able to input multiple string i.e. ''Hello there I am Batman'' ; ''Hello World'' ; etc. These ideally while being inputted are seperated by a semi-colen and are parsed with it.
CODE:
#include <stdio.h>
/* function prototype for utility function to
reverse a string from begin to end */
/*Function to reverse words*/
void reverseWords(char* s)
{
char* word_begin = NULL;
char* temp = s; /* temp is for word boundry */
/*STEP 1 of the above algorithm */
while (*temp) {
/*This condition is to make sure that the string start with
valid character (not space) only*/
if ((word_begin == NULL) && (*temp != ' ')) {
word_begin = temp;
}
if (word_begin && ((*(temp + 1) == ' ') || (*(temp + 1) == ''))) {
reverse(word_begin, temp);
word_begin = NULL;
}
temp++;
} /* End of while */
/*STEP 2 of the above algorithm */
reverse(s, temp - 1);
}
/* UTILITY FUNCTIONS */
/*Function to reverse any sequence starting with pointer
begin and ending with pointer end */
void reverse(char* begin, char* end)
{
char temp;
while (begin < end) {
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
/*
int main( void )
{
int i, n;
printf("Enter no of strings:");
scanf("%i", &n);
char **str = (char **) malloc( n* sizeof(char*));
for (i = 0; i < n; i++) {
str[i] = (char*) malloc(100);
fgets(str[i],100,stdin);
}
for (i = 0; i < n; i++) {
printf("%s", str[i]);
}
for (i = 0; i < n; i++) {
free(str[i]);
}
free(str);
return 0;
}
*/
/* Driver function to test above functions */
int main()
{
char str[50];
char* temp = str;
printf("Enter a string : ");
gets(str);
reverseWords(str);
printf("%s", str);
return(0);
}
c arrays input
add a comment |
First time posting on this forum, however not the first time surfing through it! Quite helpful threads, now I feel it's my time to ask for some help so thanks in advance for those who can help me out.
I have the following lines of code: which upon input reverses the order of the words.
i.e.
INPUT: Hello there I am Batman
OUTPUT: Batman am I there Hello
Now I would like to allow the user to be able to input multiple string i.e. ''Hello there I am Batman'' ; ''Hello World'' ; etc. These ideally while being inputted are seperated by a semi-colen and are parsed with it.
CODE:
#include <stdio.h>
/* function prototype for utility function to
reverse a string from begin to end */
/*Function to reverse words*/
void reverseWords(char* s)
{
char* word_begin = NULL;
char* temp = s; /* temp is for word boundry */
/*STEP 1 of the above algorithm */
while (*temp) {
/*This condition is to make sure that the string start with
valid character (not space) only*/
if ((word_begin == NULL) && (*temp != ' ')) {
word_begin = temp;
}
if (word_begin && ((*(temp + 1) == ' ') || (*(temp + 1) == ''))) {
reverse(word_begin, temp);
word_begin = NULL;
}
temp++;
} /* End of while */
/*STEP 2 of the above algorithm */
reverse(s, temp - 1);
}
/* UTILITY FUNCTIONS */
/*Function to reverse any sequence starting with pointer
begin and ending with pointer end */
void reverse(char* begin, char* end)
{
char temp;
while (begin < end) {
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
/*
int main( void )
{
int i, n;
printf("Enter no of strings:");
scanf("%i", &n);
char **str = (char **) malloc( n* sizeof(char*));
for (i = 0; i < n; i++) {
str[i] = (char*) malloc(100);
fgets(str[i],100,stdin);
}
for (i = 0; i < n; i++) {
printf("%s", str[i]);
}
for (i = 0; i < n; i++) {
free(str[i]);
}
free(str);
return 0;
}
*/
/* Driver function to test above functions */
int main()
{
char str[50];
char* temp = str;
printf("Enter a string : ");
gets(str);
reverseWords(str);
printf("%s", str);
return(0);
}
c arrays input
Welcome to SO, Keith! What exactly is your question? If you want review of your code, I suggest that you ask that in codereview.SE instead, where it is more appropriate. Otherwise please make it clearer what you're asking for!
– joH1
Nov 22 '18 at 8:55
Take a look at the "strtok" function (and string.h in general).
– katzenversteher
Nov 22 '18 at 9:23
add a comment |
First time posting on this forum, however not the first time surfing through it! Quite helpful threads, now I feel it's my time to ask for some help so thanks in advance for those who can help me out.
I have the following lines of code: which upon input reverses the order of the words.
i.e.
INPUT: Hello there I am Batman
OUTPUT: Batman am I there Hello
Now I would like to allow the user to be able to input multiple string i.e. ''Hello there I am Batman'' ; ''Hello World'' ; etc. These ideally while being inputted are seperated by a semi-colen and are parsed with it.
CODE:
#include <stdio.h>
/* function prototype for utility function to
reverse a string from begin to end */
/*Function to reverse words*/
void reverseWords(char* s)
{
char* word_begin = NULL;
char* temp = s; /* temp is for word boundry */
/*STEP 1 of the above algorithm */
while (*temp) {
/*This condition is to make sure that the string start with
valid character (not space) only*/
if ((word_begin == NULL) && (*temp != ' ')) {
word_begin = temp;
}
if (word_begin && ((*(temp + 1) == ' ') || (*(temp + 1) == ''))) {
reverse(word_begin, temp);
word_begin = NULL;
}
temp++;
} /* End of while */
/*STEP 2 of the above algorithm */
reverse(s, temp - 1);
}
/* UTILITY FUNCTIONS */
/*Function to reverse any sequence starting with pointer
begin and ending with pointer end */
void reverse(char* begin, char* end)
{
char temp;
while (begin < end) {
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
/*
int main( void )
{
int i, n;
printf("Enter no of strings:");
scanf("%i", &n);
char **str = (char **) malloc( n* sizeof(char*));
for (i = 0; i < n; i++) {
str[i] = (char*) malloc(100);
fgets(str[i],100,stdin);
}
for (i = 0; i < n; i++) {
printf("%s", str[i]);
}
for (i = 0; i < n; i++) {
free(str[i]);
}
free(str);
return 0;
}
*/
/* Driver function to test above functions */
int main()
{
char str[50];
char* temp = str;
printf("Enter a string : ");
gets(str);
reverseWords(str);
printf("%s", str);
return(0);
}
c arrays input
First time posting on this forum, however not the first time surfing through it! Quite helpful threads, now I feel it's my time to ask for some help so thanks in advance for those who can help me out.
I have the following lines of code: which upon input reverses the order of the words.
i.e.
INPUT: Hello there I am Batman
OUTPUT: Batman am I there Hello
Now I would like to allow the user to be able to input multiple string i.e. ''Hello there I am Batman'' ; ''Hello World'' ; etc. These ideally while being inputted are seperated by a semi-colen and are parsed with it.
CODE:
#include <stdio.h>
/* function prototype for utility function to
reverse a string from begin to end */
/*Function to reverse words*/
void reverseWords(char* s)
{
char* word_begin = NULL;
char* temp = s; /* temp is for word boundry */
/*STEP 1 of the above algorithm */
while (*temp) {
/*This condition is to make sure that the string start with
valid character (not space) only*/
if ((word_begin == NULL) && (*temp != ' ')) {
word_begin = temp;
}
if (word_begin && ((*(temp + 1) == ' ') || (*(temp + 1) == ''))) {
reverse(word_begin, temp);
word_begin = NULL;
}
temp++;
} /* End of while */
/*STEP 2 of the above algorithm */
reverse(s, temp - 1);
}
/* UTILITY FUNCTIONS */
/*Function to reverse any sequence starting with pointer
begin and ending with pointer end */
void reverse(char* begin, char* end)
{
char temp;
while (begin < end) {
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
/*
int main( void )
{
int i, n;
printf("Enter no of strings:");
scanf("%i", &n);
char **str = (char **) malloc( n* sizeof(char*));
for (i = 0; i < n; i++) {
str[i] = (char*) malloc(100);
fgets(str[i],100,stdin);
}
for (i = 0; i < n; i++) {
printf("%s", str[i]);
}
for (i = 0; i < n; i++) {
free(str[i]);
}
free(str);
return 0;
}
*/
/* Driver function to test above functions */
int main()
{
char str[50];
char* temp = str;
printf("Enter a string : ");
gets(str);
reverseWords(str);
printf("%s", str);
return(0);
}
c arrays input
c arrays input
asked Nov 22 '18 at 8:48
dude_98dude_98
11
11
Welcome to SO, Keith! What exactly is your question? If you want review of your code, I suggest that you ask that in codereview.SE instead, where it is more appropriate. Otherwise please make it clearer what you're asking for!
– joH1
Nov 22 '18 at 8:55
Take a look at the "strtok" function (and string.h in general).
– katzenversteher
Nov 22 '18 at 9:23
add a comment |
Welcome to SO, Keith! What exactly is your question? If you want review of your code, I suggest that you ask that in codereview.SE instead, where it is more appropriate. Otherwise please make it clearer what you're asking for!
– joH1
Nov 22 '18 at 8:55
Take a look at the "strtok" function (and string.h in general).
– katzenversteher
Nov 22 '18 at 9:23
Welcome to SO, Keith! What exactly is your question? If you want review of your code, I suggest that you ask that in codereview.SE instead, where it is more appropriate. Otherwise please make it clearer what you're asking for!
– joH1
Nov 22 '18 at 8:55
Welcome to SO, Keith! What exactly is your question? If you want review of your code, I suggest that you ask that in codereview.SE instead, where it is more appropriate. Otherwise please make it clearer what you're asking for!
– joH1
Nov 22 '18 at 8:55
Take a look at the "strtok" function (and string.h in general).
– katzenversteher
Nov 22 '18 at 9:23
Take a look at the "strtok" function (and string.h in general).
– katzenversteher
Nov 22 '18 at 9:23
add a comment |
1 Answer
1
active
oldest
votes
Using strtok
as mentioned by one of the comments, you could do it that way:
void reverseSentence(char* sent){
char *argv[25]; // This assumes you have a max of 25 words
int argc = 0;
char* token = strtok(sent, " ");
while (token != NULL) {
argv[argc] = malloc(100); // This assumes each word is 99 characters at most
strcpy(argv[argc++], token);
token = strtok(NULL, " ");
}
for(int z = argc-1; z>=0; z--)
fprintf(stdout, "%s ", argv[z]);
fprintf(stdout, "n");
}
Warning! I did not test the code, so you may have few bugs there... like missing semi colons, variable names not matching, etc...
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426980%2finputting-and-reversing-multiple-strings-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using strtok
as mentioned by one of the comments, you could do it that way:
void reverseSentence(char* sent){
char *argv[25]; // This assumes you have a max of 25 words
int argc = 0;
char* token = strtok(sent, " ");
while (token != NULL) {
argv[argc] = malloc(100); // This assumes each word is 99 characters at most
strcpy(argv[argc++], token);
token = strtok(NULL, " ");
}
for(int z = argc-1; z>=0; z--)
fprintf(stdout, "%s ", argv[z]);
fprintf(stdout, "n");
}
Warning! I did not test the code, so you may have few bugs there... like missing semi colons, variable names not matching, etc...
add a comment |
Using strtok
as mentioned by one of the comments, you could do it that way:
void reverseSentence(char* sent){
char *argv[25]; // This assumes you have a max of 25 words
int argc = 0;
char* token = strtok(sent, " ");
while (token != NULL) {
argv[argc] = malloc(100); // This assumes each word is 99 characters at most
strcpy(argv[argc++], token);
token = strtok(NULL, " ");
}
for(int z = argc-1; z>=0; z--)
fprintf(stdout, "%s ", argv[z]);
fprintf(stdout, "n");
}
Warning! I did not test the code, so you may have few bugs there... like missing semi colons, variable names not matching, etc...
add a comment |
Using strtok
as mentioned by one of the comments, you could do it that way:
void reverseSentence(char* sent){
char *argv[25]; // This assumes you have a max of 25 words
int argc = 0;
char* token = strtok(sent, " ");
while (token != NULL) {
argv[argc] = malloc(100); // This assumes each word is 99 characters at most
strcpy(argv[argc++], token);
token = strtok(NULL, " ");
}
for(int z = argc-1; z>=0; z--)
fprintf(stdout, "%s ", argv[z]);
fprintf(stdout, "n");
}
Warning! I did not test the code, so you may have few bugs there... like missing semi colons, variable names not matching, etc...
Using strtok
as mentioned by one of the comments, you could do it that way:
void reverseSentence(char* sent){
char *argv[25]; // This assumes you have a max of 25 words
int argc = 0;
char* token = strtok(sent, " ");
while (token != NULL) {
argv[argc] = malloc(100); // This assumes each word is 99 characters at most
strcpy(argv[argc++], token);
token = strtok(NULL, " ");
}
for(int z = argc-1; z>=0; z--)
fprintf(stdout, "%s ", argv[z]);
fprintf(stdout, "n");
}
Warning! I did not test the code, so you may have few bugs there... like missing semi colons, variable names not matching, etc...
answered Nov 22 '18 at 10:05
PhoenixBluePhoenixBlue
460514
460514
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426980%2finputting-and-reversing-multiple-strings-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to SO, Keith! What exactly is your question? If you want review of your code, I suggest that you ask that in codereview.SE instead, where it is more appropriate. Otherwise please make it clearer what you're asking for!
– joH1
Nov 22 '18 at 8:55
Take a look at the "strtok" function (and string.h in general).
– katzenversteher
Nov 22 '18 at 9:23