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 5 years ago.
Improve this question
I am a beginner C-programmer. Recently I've been trying to practise using string functions in C.
As such, I wrote the following program:
MessageDetector.c
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[100] = "Alex:HeyGoodMorning!:1911hrs:0:1012:2017:::";
char *p = strtok(a,":");
char n[20];
int i = 1;
while(p != NULL) {
strcpy(n,p);
p = strtok(NULL,":"); //limit to characters before semi-colon
i++;
if (i = 2) { //after 2 occurrences of the semi-colon. print a string
printf("%s\n",n);
break;
}
}
return 0;
}
The output of my program is as follows:
Alex
However, I would like the program to output
HeyGoodMorning!
What are the changes I should make to the above program? Your help is greatfully appreciated
Initialize the variable i with 0 and use comparison instead of assignment in this condition
int i = 0;
//...
if(i == 2){//
Take into account that the first call of strcpy is redundant.
In fact you could do the same without a loop. For example
char a[100] = "Alex:HeyGoodMorning!:1911hrs:0:1012:2017:::";
char *p;
if ((p = strtok(a, ":")) && (p = strtok(NULL, ":")))
{
puts(p);
}
Related
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 2 years ago.
Improve this question
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void func(char* str) {
int i = 0;
for (i = 0; i < 8191; i += 1) {
str[i] = ('a');
}
}
int main(void) {
char buff[8192] = { 0, };
int len = 0;
func(buff);// printf("%s\n", buff);
len = strlen(buff);
printf("len:%d\n");
//printf("%s\n",buff);
return 0;
}
I try to expect the len : 8191 ,
but returns wrong number..
why is this happen??
could you explain why this happens??
printf("len:%d\n"); is incorrect. For each conversion specification such as %d, there must be an argument in the function call that gives the value to be printed. It should be printf("len:%d\n", len);.
Your compiler likely warned you of this. If it did not, enable warnings in your compiler and pay attention to them.
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 5 years ago.
Improve this question
I'm doing the CS50 course, pset2 initials. The only error I get is a problem with get_string: it would have an 'incompatible pointer types initializing 'string' with an expression of type 'string (void)'. I really don't understand what I am doing wrong, because my code for get_string worked for the last problem set.. Here's my code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
// ask user for input
printf("What are your names?");
string s = get_string;
// print first letter from string & capitalize
printf("%c", toupper(s[0]));
//iterate over characters in current string + start loop
for (int i = 0; i < strlen(s); i++)
{
//find space character
if (s[i] == ' ')
{
// print character next to space & capitalize
printf("%c", toupper(s[i++]));
}
// new rule
printf("\n");
}
}
Although you have not shown us cs50.h, we can guess that get_string is a function, so this:
string s = get_string;
Needs to be:
string s = get_string();
get_string is a function that takes no parameters:
string get_string(void);
so
string s = get_string;
must be
string s = get_string();
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
I've created a simple program that takes two command line arguments(a char and a number), and prints the char however many times specify with the number to the screen.
Example:
./fstring a 4
aaaa
It mostly works, but for some reason, with specific numbers there is weird input at the end.
./fstring a 8
aaaaaaaa¼#
./fstring a 9
aaaaaaaaa#
./fstring a 10
aaaaaaaaaa#
The same pattern of weirdness happens with 40, 41 and 42, as well as 88, 89 and 90.. and so forth. It seems to happen in increments of forty starting at 8. Here's the code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void create_string(char chr, int times);
int main(int argc, char *argv[])
{
create_string(*argv[1], atoi(argv[2]));
return 0;
}
void create_string(char c, int t)
{
char buf[t+1];
int i;
for(i = 0; i < t; ++i)
buf[i] = c;
printf("%s\n", buf);
}
I imagine it has something to do with buf, but I can't figure it out.
You forgot about the terminating zero of strings. Write
for(i = 0; i < t; ++i)
buf[i] = c;
buf[i] = '\0';
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 7 years ago.
Improve this question
Hi I've just started programming in C and I am making a array counter but I get segmentation error when I try to run this:
//function to calculate the length of an array
int arr_length(char arr[]) {
int i = 0;
while(arr[i] != "\0") {
i++;
}
return i;
}
while(arr[i] != "\0") {
i++;
}
is equivalent to:
char const* str = "\0";
while(arr[i] != p) {
i++;
}
You are comparing a char with a char*. It evaluates to false most of the time. As a result, you end up accessing arr beyond the valid limits, which causes undefined behavior. In your case, that manifests as segmentation error.
You need to use:
while(arr[i] != '\0') { // Compare with the null character
i++;
}
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'm new to C programming and I know there have been other explanations on how to split a string into words but none of them seem similar to my program. I'm having difficulty finding the errors in my program:
#include <stdio.h>
#include <stdlib.h>
int tokenise(char str[], int start, char result[]) {
if (str[start] == "/o") {
return -1;
} else {
result = str[start];
}
}
int main() {
const int MAX_STRING = 256;
char buffer[MAX_STRING];
fgets(buffer, MAX_STRING, stdin);
char result[256];
int start;
start = tokenise(buffer, 0, result);
while ( start != -1 ) {
printf("%s\n", result);
start = tokenise(buffer, start, result);
}
}
In your function tokenise -
if(str[start] == "/o"){
What is "/o" you compare with? It should be '\0'.
if(str[start] == '\0'){
And in else your function does not return anything , therefore , in that case UB.
You function doesn't have any loop or use recursion to iterate over array ,therefore , your logic doesn't seem to achieve anything close .
You have many problems with your code:
else {
result = str[start];
}
No return value. That is undefined behaviour.
str[start] == '\o'
Thats incorrect as you want to compare to the EOS null termination character
Do this instead:
str[start] == '\0'
Lastly, if you want your tokenise function to write into result, you need to pass a pointer to result, not the value of result.
ps: semantic errors aside, your function does nothing resembling what you want. Look into loops and their implementation.