#include <stdio.h>
char converter(char input[]);
char getInformation (char input[]);
int main(){
char c[99];
int i;
printf("Enter a Line:");
scanf("%c", &c);
while(i < 99){
printf("%c\n", &c[i]);
i++;
}
}
Results for when I tried to run this
z:~/homework1: gcc -o hw1_1 hw1_1.c
z:~/homework1: hw1_1
Enter a Line:Test
Ø
Ù
Ú
Û
Ü
Ý
Þ
ß
à
á
â
ã
ä
å
æ
ç
è
é
ê
ë
ì
í
î
ï
ð
ñ
ò
ó
ô
õ
ö
÷
ø
ù
ú
û
ü
ý
þ
ÿ
1
2
3
4
5
6
7
8
9
:
z:~/homework1:
I’m trying to get it to read a text and print it out. Is there a way I can set the array to undefined number and how would I implement that into the while loop? Also can I have some guidance of how to check every character and change it’s case (from lower case to upper case and the other way around). I think I can do this by checking the ASCII value of the character and flipping the numbers. For example if the character is a decimal value from 65-90, which are A-Z, I would add 32 to it to make it small. For character from 97-122, a-z, I would subtract 32. I don’t need the answer but some hits would be nice. Thank you.
So far I’ve made these changes.
#include <stdio.h>
char converter(char input[]);
char getInformation (char input[]);
int main(){
char c[30];
int i;
printf("Enter a Line: ");
fgets(c, sizeof(c), stdin);
i=0;
while( i < sizeof(c)){
printf("%s", c[i]);
i++;
}
}
This is what I get after trying to run this.
z:~/homework1: gcc -o hw1_1 hw1_1.c
z:~/homework1: hw1_1
Enter a Line: Test
Segmentation fault (core dumped)
z:~/homework1: z:~/homework1:
Yes I got it! thanks a lot everyone!! Here is the code
#include <stdio.h>
int main(){
char c[BUFSIZ];
int i;
printf("Enter a Line: ");
fgets(c, BUFSIZ, stdin);
i=0;
for (i=0; i < sizeof(c); i++){
if ((c[i] >= 65) && (c[i] <= 90) ){
c[i] = c[i]+32;
}else {
if ((c[i] >=97) && (c[i] <= 122) ){
c[i] = c[i]-32;
}
}
}
and the results when I run it:
z:~/homework1: gcc -o hw1_1 hw1_1.c
z:~/homework1: hw1_1
Enter a Line: TeoijsTY;' ';lasd
tEOIJSty;' ';LASD
z:~/homework1:
This is the best feeling you can have, writing a working code! I wonder if I post this and my professors does a Google check of my code and think that I plagiarized? Anyways thanks a lot for the help.
When you read in a line via
scanf, you use the format specifier for achar, you should use%sor, preferably,%98sto avoid overflow. On the other hand, when printing the characters one by one, you should pass the character itself as an argument toprintfand not its address, so there it should beprintf("%c\n",c[i]);.To avoid printing out more characters than have been read in, you can first zero-fill the
char-array, then scan with a width less than the array size to guarantee that you have a 0-terminated string in the array, and in the printing loop, check thatc[i] != 0.For the case conversions, the header
<ctype.h>contains the necessary functions, that’s better than to code them yourself. If you’re not allowed to use them, your plan with the comparison with'A' = 65etc. works well for ASCII characters, but you may need to also consider characters in the range 128-255 (depends on your assignment).