Originally Posted by
Adak
123456 - The input
My thinking goes like this:
#1.
for every digit in the set of digits, print it, then print a space
#2.
for(every digit in the set of digits)
....print the digit and a space
#3.
for(i equals first digit, i is less than or equal to 6, increment i)
....printf(the digit and 1 space)
Ready to code up the loop.
It's odd to take it through these steps, because with practice, you just say "got it", and code it. But the process REALLY comes into play with any problem where you don't have an answer.
Failing anything better, start with how YOU would do it, and take that logic step by step, to your program.
i'm going to do that practice and practice
i need more problems that let me learn
Working
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char arr[100];
int i=0;
fgets(arr,100,stdin);
int x=strlen(arr);
for(i=0;i<x;i++){
printf(" %c",arr[i]);
}
return 0;
}
Working
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char arr[100];
int i=0;
fgets(arr,100,stdin);
while(i<strlen(arr)){
printf(" %c",arr[i]);
i++;
}
return 0;
}
Not Working i used debugger step by step it go ahead to return 0 Why?
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char arr[100];
int i;
fgets(arr,100,stdin);
for(i=0;i!='\0';i++){
printf(" %c",arr[i]);
}
return 0;
}
Not Working i used debugger step by step it go ahead to return 0
Why?
loop till find the Null terminator ?
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char arr[100];
int i=0;
fgets(arr,100,stdin);
while(i!='\0'){
printf(" %c",arr[i]);
i++;
}
return 0;
}
Not Working i used debugger step by step it go ahead to return 0
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char arr[100];
int i=0;
fgets(arr,100,stdin);
while(arr){
if(i!='\0'){
printf(" %c",arr[i]);
i++;}
else
return 0;
}
return 0;
}
in the first,second Code i used strlen() that will count the Null terminator
i mean it is the same idea with the other 3 code
so what is the problem