Wednesday 15 September 2010

c++ - Reformatting an entered string with spaces in it -



c++ - Reformatting an entered string with spaces in it -

currently working on problem reformat inputted string odd char char no newline. ex. input: test. ouput: go etodts. reason when run programme outputs "g".

#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib> using namespace std; int main (int argc, char ** argv) { char swordodd[100] = {0}; scanf("%s", swordodd); int inum = strlen(swordodd); (int i=0; i<=inum && i%2==0; i++) { printf("%c",swordodd[i]); } (int a=0; a<=inum && a%2!=0; a++) { printf("%c",swordodd[a]); } printf("\n"); homecoming 0; }

your i<=inum && i%2==0 break status terminates loop early. accomplish effect want, set if statement within loop, so:

for (int = 0; <= inum; i++){ if(i % 2 == 0) printf("%c",swordodd[i]); }

as sec loop, think meant a++ instead of a--, because otherwise you'll seek access array using negative index. think meant loop this:

for (int = 0; <= inum; a++){ if(a % 2 != 0) printf("%c",swordodd[a]); }

side note: notice spacing between variables , operators in code. using spaces makes code easier read.

c++ arrays char

No comments:

Post a Comment