C++ arrays and strstrok
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm working with the arrays in C++. So my input in the array is O3B4F2 and I want to have in output OOOBBBBFF?.. I'm reading about function strtrok but I don't understand that really good because it's dividing sentence on the tokens.
#include <iostream>
#include <string.h>
#include<stdlib.h>
using namespace std;
int main ()
{
char a[100+1];
cin>>a;
char * pch;
char dioba="0 1 2 3 4 5 6 7 8 9 ";
pch = strtok (a,dioba);
int c;
for(int i=0;i<strlen(a);i++)
{
if(isdigit(a[i])==1)
{
}
}
while (pch != NULL)
{
cout<<pch<<endl;
pch = strtok (NULL,dioba);
}
return 0;
}
Also, I try to resolve a similar task where I need to divide the array into sets of the letter. I have output need togo and I want to my output look like ne e d to go. So after letter e an o, I want to use white space or new line.
#include <iostream>
#include <iostream>
#include <string.h>
#include<stdlib.h>
using namespace std;
int main ()
{
char a[100+1];
cin>>a;
char b[100+1];
int i=0,j=0;
for(i;i<strlen(a);i++)
{
if(a[i]=='a'||a[i]=='e'|| a[i]=='i')
for(j;j<strlen(a);j++)
{
b[j]=' ';
}
b[j]= a[i];
cout<<b<<endl;
}
return 0;
}
c++ arrays strtok typed-arrays
add a comment |
I'm working with the arrays in C++. So my input in the array is O3B4F2 and I want to have in output OOOBBBBFF?.. I'm reading about function strtrok but I don't understand that really good because it's dividing sentence on the tokens.
#include <iostream>
#include <string.h>
#include<stdlib.h>
using namespace std;
int main ()
{
char a[100+1];
cin>>a;
char * pch;
char dioba="0 1 2 3 4 5 6 7 8 9 ";
pch = strtok (a,dioba);
int c;
for(int i=0;i<strlen(a);i++)
{
if(isdigit(a[i])==1)
{
}
}
while (pch != NULL)
{
cout<<pch<<endl;
pch = strtok (NULL,dioba);
}
return 0;
}
Also, I try to resolve a similar task where I need to divide the array into sets of the letter. I have output need togo and I want to my output look like ne e d to go. So after letter e an o, I want to use white space or new line.
#include <iostream>
#include <iostream>
#include <string.h>
#include<stdlib.h>
using namespace std;
int main ()
{
char a[100+1];
cin>>a;
char b[100+1];
int i=0,j=0;
for(i;i<strlen(a);i++)
{
if(a[i]=='a'||a[i]=='e'|| a[i]=='i')
for(j;j<strlen(a);j++)
{
b[j]=' ';
}
b[j]= a[i];
cout<<b<<endl;
}
return 0;
}
c++ arrays strtok typed-arrays
You can use walk the input char by index or by pointer, no need to use strtok. As for mapping digit chars to numbers, a common trick is to usec - '0'
to get the digit value.
– Botje
Nov 22 '18 at 8:09
Re:if(isdigit(a[i])==1)
-- this won't work. Read the documentation forstd::is digit
.
– Pete Becker
Nov 22 '18 at 14:52
add a comment |
I'm working with the arrays in C++. So my input in the array is O3B4F2 and I want to have in output OOOBBBBFF?.. I'm reading about function strtrok but I don't understand that really good because it's dividing sentence on the tokens.
#include <iostream>
#include <string.h>
#include<stdlib.h>
using namespace std;
int main ()
{
char a[100+1];
cin>>a;
char * pch;
char dioba="0 1 2 3 4 5 6 7 8 9 ";
pch = strtok (a,dioba);
int c;
for(int i=0;i<strlen(a);i++)
{
if(isdigit(a[i])==1)
{
}
}
while (pch != NULL)
{
cout<<pch<<endl;
pch = strtok (NULL,dioba);
}
return 0;
}
Also, I try to resolve a similar task where I need to divide the array into sets of the letter. I have output need togo and I want to my output look like ne e d to go. So after letter e an o, I want to use white space or new line.
#include <iostream>
#include <iostream>
#include <string.h>
#include<stdlib.h>
using namespace std;
int main ()
{
char a[100+1];
cin>>a;
char b[100+1];
int i=0,j=0;
for(i;i<strlen(a);i++)
{
if(a[i]=='a'||a[i]=='e'|| a[i]=='i')
for(j;j<strlen(a);j++)
{
b[j]=' ';
}
b[j]= a[i];
cout<<b<<endl;
}
return 0;
}
c++ arrays strtok typed-arrays
I'm working with the arrays in C++. So my input in the array is O3B4F2 and I want to have in output OOOBBBBFF?.. I'm reading about function strtrok but I don't understand that really good because it's dividing sentence on the tokens.
#include <iostream>
#include <string.h>
#include<stdlib.h>
using namespace std;
int main ()
{
char a[100+1];
cin>>a;
char * pch;
char dioba="0 1 2 3 4 5 6 7 8 9 ";
pch = strtok (a,dioba);
int c;
for(int i=0;i<strlen(a);i++)
{
if(isdigit(a[i])==1)
{
}
}
while (pch != NULL)
{
cout<<pch<<endl;
pch = strtok (NULL,dioba);
}
return 0;
}
Also, I try to resolve a similar task where I need to divide the array into sets of the letter. I have output need togo and I want to my output look like ne e d to go. So after letter e an o, I want to use white space or new line.
#include <iostream>
#include <iostream>
#include <string.h>
#include<stdlib.h>
using namespace std;
int main ()
{
char a[100+1];
cin>>a;
char b[100+1];
int i=0,j=0;
for(i;i<strlen(a);i++)
{
if(a[i]=='a'||a[i]=='e'|| a[i]=='i')
for(j;j<strlen(a);j++)
{
b[j]=' ';
}
b[j]= a[i];
cout<<b<<endl;
}
return 0;
}
c++ arrays strtok typed-arrays
c++ arrays strtok typed-arrays
asked Nov 22 '18 at 7:52
Be BehindBe Behind
197
197
You can use walk the input char by index or by pointer, no need to use strtok. As for mapping digit chars to numbers, a common trick is to usec - '0'
to get the digit value.
– Botje
Nov 22 '18 at 8:09
Re:if(isdigit(a[i])==1)
-- this won't work. Read the documentation forstd::is digit
.
– Pete Becker
Nov 22 '18 at 14:52
add a comment |
You can use walk the input char by index or by pointer, no need to use strtok. As for mapping digit chars to numbers, a common trick is to usec - '0'
to get the digit value.
– Botje
Nov 22 '18 at 8:09
Re:if(isdigit(a[i])==1)
-- this won't work. Read the documentation forstd::is digit
.
– Pete Becker
Nov 22 '18 at 14:52
You can use walk the input char by index or by pointer, no need to use strtok. As for mapping digit chars to numbers, a common trick is to use
c - '0'
to get the digit value.– Botje
Nov 22 '18 at 8:09
You can use walk the input char by index or by pointer, no need to use strtok. As for mapping digit chars to numbers, a common trick is to use
c - '0'
to get the digit value.– Botje
Nov 22 '18 at 8:09
Re:
if(isdigit(a[i])==1)
-- this won't work. Read the documentation for std::is digit
.– Pete Becker
Nov 22 '18 at 14:52
Re:
if(isdigit(a[i])==1)
-- this won't work. Read the documentation for std::is digit
.– Pete Becker
Nov 22 '18 at 14:52
add a comment |
2 Answers
2
active
oldest
votes
For your first case, as mentionned in comments, no need to use strtok. Here is an example of code (although there are many ways to perform the requested task):
#include <iostream>
#include <string>
#include <sstream>
int main ()
{
std::string s;
std::cin >> s;
std::istringstream stream(s);
char pch;
// fetch stream for the character to repeat until the end of the string
while( stream >> pch )
{
char nbChars;
// fetch length for repetition
stream >> nbChars;
// convert character to its integer value
nbChars -= '0';
// repeat character as many times as needed
for(int i=0; i < nbChars;i++)
{
std::cout << pch;
}
}
return 0;
}
For your second task, I propose this sample code, you can modify letters as needed.
#include <iostream>
#include <string>
int main ()
{
std::string s;
std::string needSpaceChars{"aeo"};
// read content on standard input
std::getline(std::cin, s);
// for each char, check its value and add space after the letters defined in needSpaceChars variable
for(char pch: s)
{
// display character first
std::cout << pch;
// add space if character is in the list of characters to handle
if( needSpaceChars.find(pch) != std::string::npos )
{
std::cout << ' ';
}
}
return 0;
}
add a comment |
The way you have described the problem, the input will always in form of char
followed by int
. Hence you have to pick in pair (char
, int
)and print it as per rule.
for(size_t i = 0; i < arr.size() - 1;) {
int val = (int)arr[i+1] - 48;
for(auto j = 0; i < val; j++) {
cout << arr[i];
}
i += 2;
}
- First
for()
is for traversing through wholearr
. - Second
for()
is for printing the particularchar
for it's number of occurrences.
Note : Since the given arr
is of type char
you have to convert the second element of pair from char
to int
.
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%2f53426168%2fc-arrays-and-strstrok%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
For your first case, as mentionned in comments, no need to use strtok. Here is an example of code (although there are many ways to perform the requested task):
#include <iostream>
#include <string>
#include <sstream>
int main ()
{
std::string s;
std::cin >> s;
std::istringstream stream(s);
char pch;
// fetch stream for the character to repeat until the end of the string
while( stream >> pch )
{
char nbChars;
// fetch length for repetition
stream >> nbChars;
// convert character to its integer value
nbChars -= '0';
// repeat character as many times as needed
for(int i=0; i < nbChars;i++)
{
std::cout << pch;
}
}
return 0;
}
For your second task, I propose this sample code, you can modify letters as needed.
#include <iostream>
#include <string>
int main ()
{
std::string s;
std::string needSpaceChars{"aeo"};
// read content on standard input
std::getline(std::cin, s);
// for each char, check its value and add space after the letters defined in needSpaceChars variable
for(char pch: s)
{
// display character first
std::cout << pch;
// add space if character is in the list of characters to handle
if( needSpaceChars.find(pch) != std::string::npos )
{
std::cout << ' ';
}
}
return 0;
}
add a comment |
For your first case, as mentionned in comments, no need to use strtok. Here is an example of code (although there are many ways to perform the requested task):
#include <iostream>
#include <string>
#include <sstream>
int main ()
{
std::string s;
std::cin >> s;
std::istringstream stream(s);
char pch;
// fetch stream for the character to repeat until the end of the string
while( stream >> pch )
{
char nbChars;
// fetch length for repetition
stream >> nbChars;
// convert character to its integer value
nbChars -= '0';
// repeat character as many times as needed
for(int i=0; i < nbChars;i++)
{
std::cout << pch;
}
}
return 0;
}
For your second task, I propose this sample code, you can modify letters as needed.
#include <iostream>
#include <string>
int main ()
{
std::string s;
std::string needSpaceChars{"aeo"};
// read content on standard input
std::getline(std::cin, s);
// for each char, check its value and add space after the letters defined in needSpaceChars variable
for(char pch: s)
{
// display character first
std::cout << pch;
// add space if character is in the list of characters to handle
if( needSpaceChars.find(pch) != std::string::npos )
{
std::cout << ' ';
}
}
return 0;
}
add a comment |
For your first case, as mentionned in comments, no need to use strtok. Here is an example of code (although there are many ways to perform the requested task):
#include <iostream>
#include <string>
#include <sstream>
int main ()
{
std::string s;
std::cin >> s;
std::istringstream stream(s);
char pch;
// fetch stream for the character to repeat until the end of the string
while( stream >> pch )
{
char nbChars;
// fetch length for repetition
stream >> nbChars;
// convert character to its integer value
nbChars -= '0';
// repeat character as many times as needed
for(int i=0; i < nbChars;i++)
{
std::cout << pch;
}
}
return 0;
}
For your second task, I propose this sample code, you can modify letters as needed.
#include <iostream>
#include <string>
int main ()
{
std::string s;
std::string needSpaceChars{"aeo"};
// read content on standard input
std::getline(std::cin, s);
// for each char, check its value and add space after the letters defined in needSpaceChars variable
for(char pch: s)
{
// display character first
std::cout << pch;
// add space if character is in the list of characters to handle
if( needSpaceChars.find(pch) != std::string::npos )
{
std::cout << ' ';
}
}
return 0;
}
For your first case, as mentionned in comments, no need to use strtok. Here is an example of code (although there are many ways to perform the requested task):
#include <iostream>
#include <string>
#include <sstream>
int main ()
{
std::string s;
std::cin >> s;
std::istringstream stream(s);
char pch;
// fetch stream for the character to repeat until the end of the string
while( stream >> pch )
{
char nbChars;
// fetch length for repetition
stream >> nbChars;
// convert character to its integer value
nbChars -= '0';
// repeat character as many times as needed
for(int i=0; i < nbChars;i++)
{
std::cout << pch;
}
}
return 0;
}
For your second task, I propose this sample code, you can modify letters as needed.
#include <iostream>
#include <string>
int main ()
{
std::string s;
std::string needSpaceChars{"aeo"};
// read content on standard input
std::getline(std::cin, s);
// for each char, check its value and add space after the letters defined in needSpaceChars variable
for(char pch: s)
{
// display character first
std::cout << pch;
// add space if character is in the list of characters to handle
if( needSpaceChars.find(pch) != std::string::npos )
{
std::cout << ' ';
}
}
return 0;
}
answered Nov 22 '18 at 9:01
Aurelien NormandAurelien Normand
491
491
add a comment |
add a comment |
The way you have described the problem, the input will always in form of char
followed by int
. Hence you have to pick in pair (char
, int
)and print it as per rule.
for(size_t i = 0; i < arr.size() - 1;) {
int val = (int)arr[i+1] - 48;
for(auto j = 0; i < val; j++) {
cout << arr[i];
}
i += 2;
}
- First
for()
is for traversing through wholearr
. - Second
for()
is for printing the particularchar
for it's number of occurrences.
Note : Since the given arr
is of type char
you have to convert the second element of pair from char
to int
.
add a comment |
The way you have described the problem, the input will always in form of char
followed by int
. Hence you have to pick in pair (char
, int
)and print it as per rule.
for(size_t i = 0; i < arr.size() - 1;) {
int val = (int)arr[i+1] - 48;
for(auto j = 0; i < val; j++) {
cout << arr[i];
}
i += 2;
}
- First
for()
is for traversing through wholearr
. - Second
for()
is for printing the particularchar
for it's number of occurrences.
Note : Since the given arr
is of type char
you have to convert the second element of pair from char
to int
.
add a comment |
The way you have described the problem, the input will always in form of char
followed by int
. Hence you have to pick in pair (char
, int
)and print it as per rule.
for(size_t i = 0; i < arr.size() - 1;) {
int val = (int)arr[i+1] - 48;
for(auto j = 0; i < val; j++) {
cout << arr[i];
}
i += 2;
}
- First
for()
is for traversing through wholearr
. - Second
for()
is for printing the particularchar
for it's number of occurrences.
Note : Since the given arr
is of type char
you have to convert the second element of pair from char
to int
.
The way you have described the problem, the input will always in form of char
followed by int
. Hence you have to pick in pair (char
, int
)and print it as per rule.
for(size_t i = 0; i < arr.size() - 1;) {
int val = (int)arr[i+1] - 48;
for(auto j = 0; i < val; j++) {
cout << arr[i];
}
i += 2;
}
- First
for()
is for traversing through wholearr
. - Second
for()
is for printing the particularchar
for it's number of occurrences.
Note : Since the given arr
is of type char
you have to convert the second element of pair from char
to int
.
edited Nov 22 '18 at 9:21
answered Nov 22 '18 at 9:10
Shravan40Shravan40
4,44531633
4,44531633
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%2f53426168%2fc-arrays-and-strstrok%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
You can use walk the input char by index or by pointer, no need to use strtok. As for mapping digit chars to numbers, a common trick is to use
c - '0'
to get the digit value.– Botje
Nov 22 '18 at 8:09
Re:
if(isdigit(a[i])==1)
-- this won't work. Read the documentation forstd::is digit
.– Pete Becker
Nov 22 '18 at 14:52