Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is it possible in c programming language to check string with if else, and if it is, can someone please explain how, with some basic exampel. I would like to check if string matches a certain word. Thanks very much. For example if(n==0) instead of 0 to be a word.
To check for the contents of a string [whether is matches another], you cannot use == operator. You need to use strcmp() function with if condition.
check the man page here
If the strings match, it will return 0, otherwise, it will return non-zero. However, this is case-sensitive. ["Hello" and "hello" are not same]
Suppose, you want to check whether your string strcontains Hello or not. A pseudo-code will look like
if (! strcmp(str, "Hello"))
printf("String contains Hello\n");
else
printf("No Hello!!\n");
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I rarely log on and am very new to the C language, therefore I apologize if this is a duplicate question or if this is a silly query.
I'm currently learning C and am hitting a wall with strings. I understand that char arrays are used in place of strings in the language. My question is, is there a better way than to assign an arbitrary value when declaring a char[] for user input(i.e setting the size of the array to one value when the user might enter more or less than that amount of characters)?
If you're on a POSIX system (basically anything that's not Windows), you can use getline(3) to do this. It will automatically allocate a buffer of the right size for you. Otherwise, you'll have to guess a length, allocate that, then read the input up to that length, and if you guessed wrong, use realloc to increase your guess and try again.
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 7 years ago.
Improve this question
I have a couple of questions.
I need to write a program(winapi) that will create a buffer of a fixed size, then append strings to it and returns it.
1. Is it even possible for "main" to return a buffer?
2. How should I create, append string and return it?
I am not new to C, but I have very little experience with buffers and strings handling.
Thank you!
Is it even possible for "main" to return a buffer?
No and you shouldn't. What should main() return in C and C++?
How should I create, append string and return it?
char aString[FIXED_SIZE];
memset(aString, 0, sizeof aString);
strcpy(aString, "This is a string");
main in c only returns an integer, which is used to indicate the success or failure of the program.
See What should main() return in C and C++?
What you might want to consider is writing to stdout (What does it mean to write to stdout in C?) or direct to a file.
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 8 years ago.
Improve this question
Is there any function in C, that will find string exactly same as string , if there is no exact form, return first string that contains substring?
or i have to use first binary search for exact form, and then linear for substing
Under string.h,
strcmp(str1,str2); - returns 0 if strings are same, returns the difference between the strings if they are not same.
strstr(str1,str2); - finds whether str2 is a substring of str1.
You may want to use these two library functions and write the required logic.
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 8 years ago.
Improve this question
I'm trying to learn C, and am having difficulty understanding what end-of-file means, in terms of a statement like "if fgets() attempts to read past the end of file".
I understand that one can mark an EOF by pressing certain key combos, and that char '\0' represents an EOF, but there must be something basic that I'm not understanding regarding my question, and I hope someone can help explain it to me>
A file is a finite sequence of bytes, just like a book is a finite sequence of words. Eventually you reach the end and there's nothing more to read.
A null character does not represent the end of a file, by the way — you're probably confusing that with the null character used to mark the end of a string in memory in C/C++. That's unrelated to files.
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
Basically I am a Java programmer, and not very well know about pointers in C.
so,
#include<stdio.h>
int main( ){
char*______Time______ = "world";
printf("%s",_____Time_____);
return 0;
}
I guess the output here should be: world ?
Is something spooky here which I should know?
Thanks for any help.
This should print "world", yes.
It looks a bit like it's trying to play with the GCC built-in preprocessor symbol __TIME__, but of course it's spelled wrong to do that.
I expect to see world, but your shell might then see that the last command ended without a newline and it might add something to signify that before starting its prompt on a fresh line.
and not very well know about pointers in C
A pointer is a variable which points to a specific address in the memory.
In this case, it points to the first letter of "world", which is then printed by printf() all until NUL (automatically inserted at the end of strings).
So, answering your question: yes, the output will be "world".