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.
Related
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 1 year ago.
Improve this question
This code doesn't do as intended - it should print out all of plaintext. I'm guessing there's something at work under the hood, but that logic escapes me. If you remove the else condition with underlying statement, it suddenly works.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
int main() {
string plaintext = get_string("plaintext: ");
int i;
for(i = 0; plaintext[i] != '\0'; i++)
if(isalpha(plaintext[i] != 0))
printf("%c", plaintext[i]);
//I intend to do stuff with alphabetic characters, but that code isn't relevant, so it's not included
else
printf("%c", 'a');
}
I'll be honest, this looks like magic to me. Why would adding an else condition affect whether the condition for the original if statement was met or not (if that's the case)?
Is it somehow because of using string in cs50.h?
for(i = 0; plaintext[i] != '\0'; i++)
if(isalpha(plaintext[i] != 0))
printf("%c", plaintext[i]);
else
printf("%c", plaintext[i]);
is the same as :
for(i = 0; plaintext[i] != '\0'; i++)
printf("%c", plaintext[i]);
because any logical operation may have two results: true and false. If in both cases you do exactly the same, why do you check the condition?
another problem :
if(isalpha(plaintext[i] != 0))
it should be
if(isalpha(plaintext[i]) != 0)
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 2 years ago.
Improve this question
I'm new in C.
I've been asked to write a function that checks if the string has increasing/decreasing sequence of letters based on ASCII values + not includes white spaces(tabs,new lines etc.).
For example: demo is a very increasing string
beef is an increasing string
aa or zzz is an increasing string
tonic is a very decreasing string
spoon is a decreasing string
suddenly has no clear sequence.
So I wrote this code,it works but not so well..can you help me to improve it a little bit?
#include <stdio.h>
void f_sequence (char str[]);
int main()
{
char strl[101];
printf("your string is:\n");
scanf("%s\n", strl);
f_sequence(strl);
return 0;
}
void f_sequence(char str[])
{
int increase=0;
int decrease=0;
int match=0;
int i = 1;
if(!str[0])
printf("empty");
else if(!str[1])
printf("need more chars");
for(i=1; str[i]; i++)
{
if (str[i] == str[i-1])
match = 1;
if (str[i] > str[i-1])
increase = 1;
if (str[i] < str[i-1])
decrease = 1;
}
if((decrease==1) && (increase==0) && (match==0))
printf("we have a very descreasing sequence in here");
if((decrease==0) && (increase==1) && (match==0))
printf("we have a very increasing sequence in here");
if((decrease==1) && (increase==0) && (match==1))
printf("we have a descreasing sequence in here");
if((decrease==0) && (increase==1) && (match==1))
printf("we have an increasing sequence in here");
if((decrease==0) && (increase==0) && (match==1))
printf("we have an increasing sequence in here");
if((decrease==1) && (increase==1))
printf("not increasing and not decreasing");
puts("");
}
To ignore whitespaces, you can use isspace from <ctype.h> and skip in your calculations:
for(i = 1; str[i]; i++)
{
if (isspace((unsigned char)str[i])) continue;
...
}
But as is, you can't read input string with whitespaces as scanf with %s would stop reading at the first whitespace. You need fgets to read a line (so that you could read a string with whitespaces):
int main()
{
char strl[101];
printf("your string is:\n");
if (fgets(strl, sizeof strl, stdin)) {
char *p = strchr(strl, '\n');
if (p) *p = '\0'; // Remove newline if present
f_sequence(strl);
}
}
fgets would also read the newline character if there's space in the buffer which you may want to remove as shown (include <string.h> for strchr).
Use a function like below to remove whitespaces:
void removeSpaces(char *str)
{
// To keep track of non-space character count
int count = 0;
// Traverse the given string. If current character
// is not space, then place it at index 'count++'
for (int i = 0; str[i]; i++)
if (str[i] != ' ')
str[count++] = str[i]; // here count is
// incremented
str[count] = '\0';
}
And then add this function in the first line of f_sequence function. like this:
removeSpaces(str);
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;
}
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! :)
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