c++ - Extract letters from a string and convert to upper case? -


here program in c++ string accepts characters outputs letters; if letters lower case should make them upper case.

#include<iostream> #include<cstdlib> #include<cctype> #include <iomanip> #include <cstring>  using std :: cin; using std :: cout; using std :: endl; using std::setw ;  const int max_str_len=100;  int main() {     char str1 [max_str_len];     int i;      cin >> setw(max_str_len) >> str1;      (int i=0; <max_str_len; i++)      {         if (isalpha(str1[i])) {             if (str1[i]>='a'&& str1[i]<= 'z')                 cout << str1[i];              if (str1[i]>='a'&& str1[i]<= 'z')             {                 str1[i]= toupper(str1 [i]);                 cout << str1[i];              }         }     }     return exit_success; }  

this tends work fine gives letters, seems i'm overlooking something. also, when input numbers gives letters such phuyxpu, didn't input.

for (int i=0; <max_str_len; i++) 

this mean iterate 100 cells of array irrespective of length of string. should either initialise array before cin statement like:

for(i=0;i<max_str_len;i++) str1[i] = '\0'; 

or replace condition in loop iterate through length of array like:

for(int i=0;i<strlen(str1);i++) {  //blah blah blah 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -