for this program, I prompt the use to enter a string. after the string is executed, the program will then execute to convert all lowercase letters into upper case.
Code:
#include<stdio.h>
#include <ctype.h>
#include <string.h>
main()
{
char x[100];
int i = 0;
printf("Enter a string: ");
gets(x);
for(i = 0; x[i] !=0; i++)
{
x[i] = toupper(x[i]);
}
printf("%s\n", x);
}
But when I use another char array to represent the new uppercased strings it would output an extra character.
Code:
#include<stdio.h>
#include <ctype.h>
#include <string.h>
main()
{
char x[100];
char l[100];
int i = 0;
printf("Enter a string: ");
gets(x);
for(i = 0; x[i] !=0; i++)
{
l[i] = toupper(x[i]);
}
printf("%s\n", l);
}
Sample run for the first program.
Code:
Enter a string: This is A teSt
THIS IS A TEST
Sample run for the 2nd program with an l char array:
Code:
Enter a string: This is A teSt
THIS IS A TEST9
As you can see, there's an excess char '9' in the executed program. Can anyone explain as to why this happens?