3
#include<stdio.h>
#include<ctype.h>
int main() {
    int i=0;
    char in[100],mychar;
    fgets(in,sizeof(in),stdin);
    while (in[i]) {
        mychar=in[i];
        putchar (toupper(mychar));
        i++;
    }
    return 0;
}


This is a simple program used to change a string of characters to uppercase letters. My question is that I declare the array in[100], how can I make the array number follow my input? I mean if I type 1201 characters , then the program will make the have the array that follows my input which is in[1201].

user3507600
  • 1,075
  • 9
  • 15
Outrageous
  • 133
  • 1
  • 6
  • 2
    Which compiler? If you are using `gcc` or `clang` then you can likely use [VLA](http://stackoverflow.com/a/18521417/1708801), the alternative would be `malloc`. – Shafik Yaghmour Apr 14 '14 at 13:33
  • 2
    possible duplicate of [Dynamically Size Array in C](http://stackoverflow.com/questions/15983255/dynamically-size-array-in-c) – user1937198 Apr 14 '14 at 13:35
  • 1
    Possible duplicate of [Reading in a string of unknown length from the console](http://stackoverflow.com/questions/5375604/reading-in-a-string-of-unknown-length-from-the-console). – user3507600 Apr 14 '14 at 13:51
  • @user1937198 I believe that a duplicate answer would involve realloc(). – Lundin Apr 14 '14 at 14:05
  • Use `malloc` and `realloc` as the answer in the post user1937198 suggests does, or use c++ and use `std::vector`. – enhzflep Apr 14 '14 at 14:06

2 Answers2

0

You can see what C++ Vector does: you can malloc a length of L at fist, every time you getch(), you should decide if you expand L to 2*L (or to L + 100 for saving spaces)

kaitian521
  • 548
  • 2
  • 10
  • 25
0

Perhaps something like this?

#include<stdio.h>
#include<ctype.h>

int main() 
  {
  int mychar;

  mychar=fgetc(stdin);
  while(EOF != (mychar=fgetc(stdin)))
     putchar(toupper(mychar));

  return 0;
  }
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28