Here is my code:
#include<stdio.h>
#include<string.h>
int string_length(char s[]);
main()
{
char s[50];
int l;
printf("Enter the string\n");
scanf("%s\n",s);
l=strlen(s);
printf("Length of string = %d\n",l);
}
int string_length(char s[])
{
int i;
i=0;
while(s[i] != '\0')
++i;
return i;
}
After compile it scan for two input values.
What's wrong with my code?
Get rid of the newline in the scanf.
scanf("%s",s);
That should get this code to work.
But I am unable to understand why you wrote a function to compute string length if you had to use strlen().
HTH,
Sriram.
You're calling strlen instead of your own string_length in main.
const size_t sillyStrlen(const char* text) {
if (*text) {
return sillyStrlen(text + 1) + 1;
}
return 0;
}
Buy a book, see The Definitive C Book Guide and List
Read about the language syntax
Learn how to use pointers
You've just learned how to write a int strlen(char[]) function.
Try This one.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char *str;
printf("Enter a string\n");
gets(str);
printf("The size of the string is %d",strlen(str));
getch();
return 0;
}
Related
Can you help me in my code? I'm trying to concatenate characters using (+) sign but it doesn't work what shall I do? and my homework tells me to do it in that way with only using stdio.h library. that's what I wrote till now and thanks. This is the question-> enter image description here
#include <stdio.h>
char reverse(char string[], int stringLength){
static int i;
i=stringLength;
if(stringLength!=0){
if(i>=0){
return string[i]+reverse(string[i-1],i--);
}
}
return string;
}
int main(void) {
int stringLength=0;
char string[100];
printf("Enter a string that will be reversed: ");
scanf("%s",string);
for (stringLength=0; string[stringLength++]!='\0';){};
printf("%d", stringLength);
reverse(string, stringLength);
printf("the reverse is :%s:\n", string);
return 0;
}
I'm trying to run this program by taking input from the user and I'm getting a Segmentation fault (core dumped) error. Can anyone help me to find out why this error is occurring and how can I fix this error? My code is given below:
// C program to remove the occurrences of a specific char from the given string.
#include <stdio.h>
char *squeeze(char *a[], int b){
int i,j;
for(i=j=0; *(*a+i)='\0';i++){
if(*(*a+i)!=b)
*(*a+j++)=*(*a+i);
}
*(*a+j)='\0';
return *a;
}
void main(){
char *c[1];
char d[2];
int e;
printf("Enter the string: ");
scanf("%s",*c);
printf("Enter the character to delete: ");
scanf("%c",d);
e=d[0];
printf("Resulting string is:%s.\n",squeeze(c,e));
}
You have a few problems in your code. First, as pointed out in the comments, your declaration of c as an array of char * length one is wrong. Declare it as an array of char or some max length. The will make the squeeze code simpler, with less dereferences. Next your for loop inside of squeeze is wrong, you have an assignment statement there in the middle, when you really want to check for inequality. Your second scanf needs a space in it to get rid of the white space character leftover in the input buffer. So, making these changes, your code should look something like this:
#include <stdio.h>
char *squeeze(char *a, int b){
int i,j;
for(i=j=0; *(a+i) != '\0';i++){
if(*(a+i)!=b)
*(a+j++)=*(a+i);
}
*(a+j)='\0';
return a;
}
int main() {
char c[100] = {0};
char d[2];
int e;
printf("Enter the string: ");
scanf("%99s",c);
printf("Enter the character to delete: ");
scanf(" %c",d);
e=d[0];
printf("Resulting string is: %s.\n",squeeze(c,e));
}
EDIT: Specify string length with scanf when reading in c.
// C program to remove the occurrences of a specific char from the given string.
#include<stdio.h>
char * squeeze(char a[], char b){
for(i=0; a[i]!='\0';i++){
if(a[i]==b){
a[i]=a[i+1];
while(a[i++]!='\0'){
a[i]=a[i+1];
}
}
}
return a;
}
int main(){
char c[50];
char d;
printf("Enter the string:");
scanf("%s",c);
printf("Enter the character to delete:");//add \n
scanf(" %c",&d);
printf("Resulting string is:%s\n",squeeze(c,d));
return 0;
}
I want the string to be printed till character ('e') comes.
Code which I tried:-
#include <stdio.h>
int main() {
int a,i,x;
char b[10];
char ch;
//enter input string
for(i=0;i<10;i++)
scanf("%c",&b[i]);
for(i=0;i<10;i++)
if(b[i]!='e')
printf("%c",b[i]);
return 0;
}
Input:abcdefghij
Actual output:abcdfghij
Desired output:abcd
Question : Where am I wrong ? Will putting a break inside if block work here?
This is much cleaner if you want to use scanf.
#include <stdio.h>
int main()
{
char b[101];
scanf("%100s", b);
printf("%s\n", b);
return(0);
}
Or even better.
#include <stdio.h>
#define MAX_LENGTH 100
int main()
{
char b[MAX_LENGTH+1]; // add 1 for the terminating zero
scanf("%100s", b);
printf("%s\n", b);
return(0);
}
This one uses fgets to read the entire line.
#include <stdio.h>
#define MAX_LENGTH 100
int main()
{
char b[MAX_LENGTH];
fgets(b, MAX_LENGTH, stdin);
printf("%s", b);
return(0);
}
How to print a string till limit?
What code should do is use fgets().
Avoid using scanf(). Is is too easy to use wrong.
#include <stdio.h>
#include <string.h>
int main() {
char b[100];
if (fgets(b, sizeof b, stdin)) {
// If code needs to lop off the potential \n at the end
b[strcspn(b, "\n")] = '\0';
printf("%s\n", b);
}
return 0;
}
Advanced issues include how to handle excessively long input lines and error handling - not shown here.
Here is what you need to do
#include <stdio.h>
int main()
{
int a,i,x;
char b[10];
char ch;
//enter input string
for(i=0;i<10;i++)
{
scanf("%c",&b[i]);
}
for(i=0;i<10;i++)
{
if(b[i]=='e')
{
break;
}
}
return 0;
}
re
There are several mistakes!
If you are initializing your loops from 0 then you need to set the condition till i<100.
Change your format specifiers to %s.
Change your IF statement to if(b[i]!='\0').
#include <stdio.h>
int main()
{
int i;
char b[10];
for(i=0;i<10;i++)
{
scanf("%c",&b[i]);
}
for(i=0;i<10;i++)
{
if(b[i]=='e')
{
break;
}
printf("%c",b[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int compare(char word[], char mystery[])
{
int i=0;int bool=1;
while((i<=20)&&(bool==1))
{
if (word[i]==mystery[i])
i++;
else
bool=0;
}
return bool;
}
char readCharacter()
{
char character = 0;
character = getchar();
character = toupper(character);
while (getchar() != '\n') ;
return character;
}
void readString(char *word,char *mystery)
{
int i=0;
printf("Enter the word to guess : ");
scanf("%s",word);
while(*((word)+(i)) != '\0')
{
*((word)+(i))= toupper(*(word+i));
*((mystery)+(i))='*';
i++;
}
*(mystery+i)='\0';
}
void process(char *word,char *mystery,char letter,int *change)
{
int i=0;
while (*((word)+(i))!= '\0')
{
if (*((word)+(i))==letter)
{
*((mystery)+(i))=letter;
*change=1;
}
i++;
}
}
void test(char *word,char *mystery, int triesleft)
{
if (*mystery!=*word)
{
printf("The mystery word is : %s",*mystery);
printf("\n You have %d tries left.", triesleft);
}
else
{
printf("You won !");
}
}
int main()
{
int triesleft = 10; int change=0;
char word[20]; char mystery[20];char letter;
readString(&word,&mystery);
while((compare(word,mystery)==0) && (triesleft>0))
{
change=0;
printf("Enter the letter :");
letter=readCharacter();
process(&word,&mystery,letter,&change);
if ((change)==1)
triesleft--;
test(&word,&mystery,triesleft);
}
if (triesleft>0)
return 0;
printf("You lost.");
return 1;
}
I'm a beginner in C and I wanted to code a simple Hangman game in C and it compiled fine but it seems to crash after entering the first letter and I can't find a solution !
I don't know what may be the cause but I had a lot of trouble using strings in C, as they don't exist maybe it was a bad manipulation of that I don't know :/
You first call to readString is enough to crash the program.
word and mystery are arrays, so &word is a char ** not a char *. You should use
readString(word, mystery);
But compiler should have issue a warning on that. Warning are not there to distract beginners to to denote possible (probable if you do not understand the warning) mistakes.
There are probably other problems later ...
In the readString() function, you should use '\0' instead of NULL, as C strings are ended with this character.
You cannot declare a variable named bool as it is a type. In C, it is not actually defined for all compilers as bool is not part of the standard but some compilers and some platform will define it anyway
So I'm trying to print an inputted string using putch and a little bit of pointers.
Here is my current code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void printer(char *c);
char *c;
char ch;
main(){
clrscr();
printf("Enter a string: ");
scanf("%s",&ch);
c = &ch;
printer(c);
getch();
}
void printer(char *c){
int x;
for(x=0;x<strlen(c);x++){
putch(*c);
}
}
The problem is that i can only print the first character of the string, also for some reason strlen always return 3 for strings that are 3 characters and below.
Do I have to use array for this so that I can use putch since it is limited to only 1 character output.
One of the problems is that your printer() function is not printing anything other than the first character. There are two ways of approaching this. Using pointers:
void printer(char const *c){
while ( *c != '\0' ) {
putch(*c);
c++;
}
}
And using pointer arithmetic:
void printer(char const *c) {
int x;
for ( x=0; x < strlen(c); x++ ) {
putch( *(c + x) );
}
}
The biggest problem is that you are attempting to store a string in a single character in memory. That's just asking for problems.
char ch;
scanf("%s",&ch); // NO NO NO NO NO
Instead declare your buffer (to store the string in) as an array big enough for the biggest string you expect:
char ch[512];
scanf("%s", ch);
First off, you pass a pointer to "storage for one character" to scanf. Anything that happens after that is in nsal demons territory.
Second, scanf does not allocate storage for your input, so even if you'd passed c instead of &ch, you would not be any better off.
Third, you really should be declaring your variables inside main rather than using global variables.
Something like this may be closer to what you actually want:
void output (char *c)
{
char *cp;
for (cp = c; *cp; cp++) {
putch(*cp);
}
}
int main (void)
{
char input[80];
printf("Please input a string: ");
scanf("%s\n", input);
output(input);
}
try this code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void printer(char *c);
char *c;
char buffer[1000];// use as a buffer
void main(){
clrscr();
printf("Enter a string: ");
scanf("%s",buffer);//read the input to the buffer
c=(char*)malloc(strlen(buffer)+1);//alloc memory with len of input + 1 byte to "\0"(end of string)
strcpy(c,buffer);//copy the input from the buffer to the new memory
printer(c);
getch();
free(c);//free the memeory
}
void printer(char *c)
{
int x;
for(x=0;x<strlen(c);x++){//move the index string pointer to next char in the string
putch(c[x]);//print the char to the screen
}
}
1)You cant use char to save a string u need char*!!!
2)You can get input to memory that not allocated!!!! because of that u must read the input to buffer after that alloc string by size of the input inside the buffer!
Your code print only first character because c is always pointing to first character of the array. For printing total string you need to increment character pointer as well
You need to do like this
void printer(char *c){
while(*c != '\0'){
putch(*c);
c++;
}
}
First calculate the length of the string & then use above implementation like this-
void printer(char *c){
int i, length;
length=strlen(c)
for(i=0;i<lenth;i++,c++){
putch(*c);
}
}
It should work I think.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void printer(char *c);
char *c;
char ch;//the ch should be a array
main(){
clrscr();
printf("Enter a string: ");
scanf("%s",&ch);//the ch don't need '&'
c = &ch;//the ch don't need '&'
printer(c);
getch();
}
void printer(char *c){
int x;
for(x=0;x<strlen(c);x++){
putch(*c);
}
}