Printing a question mark after a loop [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Trying to figure out how to print the question mark as so: user inputs: "apple" and then the string gets stored in an array and gets printed like this : "apple?" here is my code:
#include<stdio.h>
int main()
{
char a[23];
int i=0;
printf("enter a single english word: ");
while( (a[i++]=getchar()) != '\n' && i < 23){
a[i] = '\0';
i = 0;
}
while(a[i] != '\0'){
printf("%c",a[i++]);
}
printf("?");
return 0;
}

You are resetting i after each character read. That is effectively making you erase whatever was stored. you need to move i = 0; from inside the first while loop to just after it.

The code you have inside the first while loop should be done after the loop is done. You do all the work of the loop in the while() header. The loop body was resetting i back to 0 each time, so you were repeatedly overwriting the first character of a.
while( (a[i++]=getchar()) != '\n' && i < 23){
}
a[i] = '\0';
i = 0;

First of all, your input loop is incorrect. It should be like so:
while( (a[i++]=getchar()) != '\n' && i < 23) {}
a[i] = '\0';
As the comments to your question say above, you are resetting the pointer index after each character is read, therefore reading character 0 of the array every time. You should do this after the loop.
Also, because a is just a string, you should be able to just print it like this (without the loop):
printf("%s?", a);
Final Program:
#include<stdio.h>
int main()
{
char a[23];
int i=0;
printf("enter a single english word: ");
while( (a[i++]=getchar()) != '\n' && i < 23) {}
a[i] = '\0';
printf("%s?",a);
return 0;
}

The follwoing logic works:
char a[23];
int i=0,j=0;
printf("enter a single english word: ");
To scan the input into an array :
for(a[i]=getchar(); (a[i]!='\n') && (i<23) ; i++)
;
And finally printing :
for(j=0;j<i;j++)
putchar(a[j]);
Hence the program :
#include<stdio.h>
int main()
{
char a[23];
int i=0,j=0;
printf("enter a single english word: ");
for(a[i]=getchar(); (a[i]!='\n') && (i<23) ; i++)
;
for(j=0;j<i;j++)
putchar(a[j]);
printf("?");
return 0;
}

Related

Is there a way to print only the first word of a string with gets() in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am very new to programming and i wonder if there is a way to print out the first word of a string with gets() in C?
void printFirstWord(char string[])
{
int i;
for(i = 0; i < (string[i] != '\0'); i++)
{
if(isalpha(string[i]))
printf("%c", string[i]);
}
}
int main()
{
char string[MAX];
printf("Type in a scentence");
gets(string);
printFirstWord(string);
return 0;
}
This is the function that i have written and called in main right now. Is it because i have isalpha in the function?
In your implementation, you might add the following line in the loop:
if (string[i] == ' ')
break;
also, fix your loop parameters e.g. like this:
for (i = 0; i < strlen(string); i++)
Overall implementation in you way will be as below.
Consider choosing another design according to comments you got, e.g. not using gets.
void printFirstWord(char string[])
{
int i;
for (i = 0; i < strlen(string); i++)
{
if (isalpha(string[i]))
printf("%c", string[i]);
if (string[i] == ' ')
break;
}
}
int main()
{
#define MAX 100
char string[MAX];
printf("Type in a scentence\n");
gets_s(string, MAX);
printFirstWord(string);
getchar();
return 0;
}
I just found a way with isblank(); function, hope it helps to anybody :)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main (){
int length, number, counter, position;
char name[50];
printf("Please type your complete name:\n");
gets(name);
//strlen();
//Returns the length of the given null-terminated byte string, that is, the number of characters in a character array
length=strlen(name);
//Counts each position until it finds a space
for(counter=0;counter<length;counter++)
{
if(isblank(name[counter]))
position=counter;
}
//Prints each character until the counter reaches the position number given by the counter variable
printf("\nThe first word you typed is: ");
for(number=0; number<=position; number++){
printf("%c", name[number]);
}
}

Why isn't anything being saved to my character array? [duplicate]

This question already has answers here:
If statements not working?
(2 answers)
Closed 7 years ago.
I'm trying to ask a user to enter a string of characters. I want my program to continue scanning in the characters one at a time until it sees the \n character (i.e., when the user presses the ENTER key).
It appears that the code I've written doesn't store the characters to the array for some reason. I know this because the for loop containing the printf() statement doesn't reproduce the characters that are entered into the terminal. My last ditch effort was to print out a[0] in case something was going wrong with my loop, but it still showed that nothing was stored to my character array.
Any explanations? (Please don't suggest that I use the string.h library--I do not wish to use it for my purposes.)
#include <stdio.h>
int main(void)
{
char a[21];
int i;
printf("enter a bunch of characters: ");
for (i = 0; ; i++)
{
scanf("%c", &a[i]);
if (a[i] = '\n')
{
break;
}
}
printf("the size of char array is %d\n", sizeof(a)/sizeof(a[0]));
for (i = 0; a[i] != '\0'; i++)
{
printf("%c", a[i]);
}
printf("%c", a[0]);
return 0;
}
if (a[i] == '\n') { break; }
Modify if statement as above and check.

C, Dividing a string into string by whitespaces with pointers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to divide a string into strings by whitespaces with pointers. I wrote the code below, but it does not work. Do you have any idea?
char sentence [] = "Thank you very much";
char * words [4]; //number of words
int i=0, charCounter=0, wordCounter=0;
while(sentence[i]){
char temp [5]; //maximum character count
while(sentence[i] != ' ' && sentence[i] != '\0'){
temp[charCounter] = sentence[i];
charCounter++;
i++;
}
temp[charCounter] = '\0';
words[wordCounter] = &temp[0];
wordCounter++;
charCounter = 0;
i++;
}
//there I want to write first word to check
int k;
for(k=0; k<12; k++)
printf("%c ", *(words[0]+k));
I think there isn't too much wrong aside from your output loops:
// write word by word
int k;
for(k=0; k < wordCounter; k++)
printf("%s ", words[k]);
// write first word byte by byte
k;
for(k=0; ((words[0])[k])!=0; k++)
printf("%c", (words[0])[k]);
Check this this code pad example based on your code above.
Only thing I changed was to allocate memory for words and copy the string using strcpy:
words[wordCounter] = malloc(sizeof(char) *(charCounter +1));
strcpy(words[wordCounter], &temp[0]);
Otherwise you assign always the same address (compare wrong example).
char temp[5] not need to save the start address of the word.
like as follows
char sentence [] = "Thank you very much";
char *words[sizeof(sentence) / 2] = {0};
int i=0, wordCounter=0;
while(sentence[i]){
while(sentence[i] == ' '){
++i;//skip space
}
if(sentence[i] == '\0')
break;
words[wordCounter++] = &sentence[i];//set top of word
while(sentence[i] != ' ' && sentence[i] != '\0'){
i++;//skip word
}
}
int k;
for(k=0; k<wordCounter; k++){
while(*words[k] != ' ' && *words[k] != '\0')
printf("%c", *words[k]++);
printf("\n");
}
You are adding a 6th char to an array of size 5!
Thats the error!
Also! You never reset the counter to 0, so as you keep going through words, you keep increasing the index added to temp as well
EDIT: I saw the temp[5]; above the while loop xD my Mistake!\
I believe your error lies in never declaring the temp[] array. In C, you must always provide an array with its size, as well.
Add this the line after you define sentence:
char temp[strlen(sentence)];
Also, a space and whitespace are two very different things.
Not all spaces are literally just a space!
However, to get a more accurate reading of any kind of space, using the function isspace, like so, is a much better practice:
while ((isspace(&sentence[i]) != 0) && (sentence[i] != '\0'))
hope this helps! :)

rubbish instead of string display [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The task is to be sure that phrases are can be readed vise versa or not (without spaces ' ').
So I got the point and made some code. But when I made
printf("%s", str_1)
I found a lot of same russian letters.
For example my inputs are: '123' '321', but displayed rubbish
My str_1[i] is going to be like 123321 and then checked first and last and so on members.
After that I added in if condition if(str[i] != 'М') and now everything nice.
Please tell me what I did wrong and what is going on with "M".
I hope my question is clear.
#include "stdio.h"
#include "locale.h"
int main(){
char str[100];
char str_1[100];
int i, j, k, l;
j = 0;
l = 0;
fgets(str, 100, stdin); //we got array
//how many chars before '\0'
for(i = 0; i < 100; i++)
{
if(str[i] != ' ' && str[i] != '\n' && str[i] != 'М')
{
str_1[j] = str[i]; //made new array without spaces between words or whatever
j++;
}
}
j--;
for(k = 0; k < j; k++)
{
if(str_1[k] == str_1[j - 1 - k])
{
l++;
}
}
if( l == k)
{
printf(";) YES! good job!\n");
}
else
{
printf(";) NOT! I'm sorry!\n");
}
return 0;
}
As it seems, you skip over the end of the string, as you never check where it ends.
At least, this is my suspicion; I am not sure if it is the right reason.
You should check for str[i] == '\0' as well and then break the whole loop.
As I see, you even made this consideration in
//how many chars before '\0'
but didn't include it in code.
A
for(i = 0; i < 100 && str[i] != '\0'; i++)
should do the trick if you omit the j-- afterwards.

Weird results in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'd like to ask you about my prog. The main purpose of this one is to fill the "result" array with data collect from "tab1" and "tab2" arrays. Could anybody check, why does results are so weird? Thank you.
#include <stdio.h>
#include <stdlib.h>
void laczenie(char tab1[],char tab2[])
{
int i;
char* result =(char*) malloc(100*sizeof(char));
for(i=0;i<30;i++)
{
if(tab1[i] != '\0') tab1[i]==result[i];
else if (tab2[i] !='\0')tab2[i]==result[i];
else printf(" ");
}
for(i=0;i<30;i++)printf("%c",result[i]);
free(result);
}
int main()
{
char x[10]={'n','a','p','i','s','1'};
char y[10]={'n','a','p','i','s','2'};
//char x[10] = {"napis1"};
//char y[10] = {"napis2"};
laczenie(x,y);
return 0;}
In addition to LihOs answer above this block looks wrong:
if(tab1[i] != '\0')
tab1[i]==result[i];
else if (tab2[i] !='\0')
tab2[i]==result[i];
else printf(" ");
Don't you mean to assign the value in tab1[i]or tab2[i] to result[i]like this?
if(tab1[i] != '\0')
result[i] = tab1[i];
else if (tab2[i] !='\0')
result[i] = tab2[i];
else printf(" ");
Also using magic numbers like in the loops: for(i=0;i<30;i++) is pretty bad practice, you should probably be using a constant for the size value (which you could then use in both the loops and in the array declarations. And why loop to 30 when the arrays is 10 elements only?
In your function you check for null-terminating character:
if(tab1[i] != '\0')
but where is null-terminating character here?
char x[10]={'n','a','p','i','s','1'};
Try:
char x[7]={'n','a','p','i','s','1','\0'};
Also note that tab1[i]==result[i]; compares tab[1] with result[i], if you want to assign the result[i] to tab1[i], use assignment operator =:
tab1[i]=result[i];
if(tab1[i] != '\0')
result[i] = tab1[i];
else if (tab2[i] !='\0')
result[i] = tab2[i];
else printf(" ");`
This is still wrong as by the time you assign tab2 to result i will be already at 6 so you will have to use two for loops i suppose for assign tab1 and tab 2

Resources