Segmentationfault error - c

I'm trying to compile this program in Linux with a makefile but the output shows
make: ***[run] segmentation fault
My code is:
int removeblanks(char string[], char temp[]){
int i, j=0; printf("no");
while(string[i] != '\0'){
if (string[i] != ' ') {
temp[j] = string[i];
printf("%c\n",temp[j]);
j++;
}
i++;
}
temp[j] = '\0';
return i;
}
int palindromos(int size, char temp[]) {
int i,palindromo=1; int middle= size/2;
for(i=0; i<middle; i++) {
if(temp[i]!=temp[size]){
palindromo= 0;
}
size--;
}
return palindromo;
}
int main() {
char string1[30] ="Anotaram a data da maratona";
char string2[30]="A torre da derrota";
char temp1[30];
char temp2[30];
printf("frase 1: %s\n", string1);
printf("frase 2: %s\n", string2);
int size1=0;
size1=removeblanks(string1,temp1);
printf("size1: %d",size1);
int size2=removeblanks(string1,temp1);
int palindromo1=palindromos(size1, string1);
int palindromo2=palindromos(size2, string2);
printf("As frases sao palindromos.\n");
printf("As frases nao sao palindromos.\n");
return 1;
}
I don't know how to debug and find the error. Can someone help me?

In function int removeblanks(char string[], char temp[])-
int i, j=0; printf("no"); // i declared not initialized
while(string[i] != '\0'){ // uninitialized local variable used here
...
}
You should initialize i=0 before using it.

Related

Error, Return value 3221225477 on running c code runtime error

I was trying to write a program that would have an array of employees and by reading from the input do some things such as adding another new employee, deleting one, showing the list of all employees or exit the program but as I dealt with all the errors in my code and was able to finally compile it, I had this return value and I don't know what to do with it.
#include<stdio.h>
#include<string.h>
using namespace std;
struct employee{
int id;
char* name;
};
int main(){
char* input;
employee person[30];
int employee_count=0;
bool command=false;
while(input !="exit"){
fgets(input, 100, stdin);
char* temp;
strncpy(input, temp, 3);
if(temp=="add"){
char* name;
for(int x=0; x<100; x+=1){
temp[x]='\0';
}
int i=4;
for(; input[i]!=' '; i+=1){
const char* tmp = &input[i];
temp=strcat((char*)temp,tmp);
}
person[employee_count+1].name=temp;
name=temp;
for(int x=0; x<100; x+=1){
temp[x]='\0';
}
for(; i<strlen(input); i+=1){
const char* tmp = &input[i];
temp=strcat((char*)temp,tmp);
}
printf("%s added.", name);
command=true;
employee_count+=1;
}
strncpy(input, temp, 3);
if(temp=="del"){
for(int x=0; x<100; x+=1){
temp[x]='\0';
}
for(int i=4; input[i]!=' '; i+=1){
const char* tmp = &input[i];
temp=strcat((char*)temp,tmp);
}
int i=0, found=0;
for(; i<employee_count; i+=1){
if(person[i].id==*((int*)(&temp))){
found=1;
break;
}
temp=person[i].name;
if(found==1){
for(int j=i; j<employee_count; j+=1){
person[j]=person[j+1];
}
printf("%s deleted.", temp);
}
else{
printf("not found.");
}
}
command=true;
employee_count-=1;
}
strncpy(input, temp, 4);
if(temp=="list"){
for(int i=0; i<employee_count; i+=1){
printf("%d %s", person[i].id, person[i].name);
}
command=true;
}
else if(command==false){
printf("invalid command.\n");
}
}
return 0;
}

I want to change the output value of the string

#include <stdio.h>
int main() {
char str[101];
int i;
int j=1;
scanf("%s", str);
for(i=0; str[i]!='\0'; i++) {
if(i!=0 && i%j==0) {
printf("\n");
j++;
}
printf("%c", str[i]);
}
}
If I input "abcdefg" in this code, I want it printed in turn like stairs.
a(\n)
bc(\n)
def(\n)
g
How fix code?
Try:
#include <stdio.h>
int main() {
char str[101];
int i;
int j=1,k=0;
scanf("%s", str);
for(i=0;str[i]!='\0';i=k+i)
{
for(j=i;j<=i+k && str[j]!='\0';j++)
printf("%c", str[j]);
k++;
if(str[j]!='\0')
printf("\n");
}
}
This could fix your problem
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j=1,k;
char str[100];
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
for(k=0;k<j;k++)
{
if(str[k+i]!='\0')
printf("%c",str[k+i]);
else
exit(1);
}
printf("\n");
j++;
i=k+i-1;
}
return 0;
}

Reading and reversing string using getchar in C

New C coder here. I'm not certain why, but my program gets stuck in the first while loop and will not move on to the other code. Does anyone have any idea what's wrong?
#include <stdio.h>
int main(void){
char str[20];
char reverse[20];
int c;
int i;
int j;
int k;
printf("Enter a string: ");
i=0;
j=0;
while((i<20)&&((c=getchar())!='\n')){
str[i] = c;
i++;
}
k=i;
while((j<k)&&(i>=0)){
reverse[j]=str[i];
j++;
i--;
}
printf("\n");
if(i==0){
while(i<k){
putchar(reverse[i]);
}
}else{
printf("logic error");
}
return 0;
}
Thank you!
#include<stdio.h>
void reverse(void)
{
char c;
if((c = getchar()) != '\n'){ reverse(); }
putchar(c);
return;
}
int main(void)
{
printf("Enter a line of text below:n");
reverse();
putchar('\n');
}
Your code don't get stuck in the first while. It gets stuck here:
while(i<k){
putchar(reverse[i]);
}
because you never change i or k, i.e. an endless loop.
Try this instead:
while(i<k){
putchar(reverse[i]);
++i;
}
Another problem is that you reverse one character past the input as i has been incremented to index the "next free char" . Don't do that.
Instead of:
while((j<k)&&(i>=0)){
reverse[j]=str[i];
j++;
i--;
}
try:
while(i>=0){
reverse[j]=str[i-1]; // Notice the -1
j++;
i--;
}
++i;
Putting it all together, it will be:
int main(void){
char str[20];
char reverse[20];
int c;
int i;
int j = 0;
int k;
printf("Enter a string: ");
i=0;
j=0;
while((i<20)&&((c=getchar())!='\n')){
str[i] = c;
i++;
}
k=i;
while(i >= 0){
reverse[j]=str[i-1]; // Notice
j++;
i--;
}
++i; // Notice
printf("\n");
if(i==0){ // This if-statement is nor really needed - just remove it
while(i<k){
putchar(reverse[i]);
i++; // Notice
}
}else{
printf("logic error");
}
printf("\n");
return 0;
}

Exit program in string functions - upper to lower characters and vice versa

I wanted the two functions to stop the program as the word "exit" is entered, but the loop for that specification is not working. can you please fix this?
Here is the code. please check it.
#include<stdio.h>
#include<string.h>
void uppercase(char *str) {
int i;
for(i=0; i<=strlen(str); i++) {
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nThe string in lower case is->%s\n",str);
}
void lowercase(char *str) {
int i;
for(i=0; i<=strlen(str); i++) {
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("The uppercase equivalent is: %s\n", str);
getchar();
}
int main()
{
char str[100];
char lower[100];
int i;
while(1)
{
printf("Enter any string->");
scanf("%s",&str);
if (str == 'exit')
{
break;
}
else
{
printf("The string is->%s\n",str);
uppercase(str);
lowercase(str);
}
}
return 0;
}
replace:
if (str == 'exit')
by this
if (strcmp(str , "exit")==0)
void uppercase(char *str){
int i;
for(i=0;i<strlen(str);i++){
//if(islower(str[i]))
str[i]=toupper(str[i]);
}
printf("\nThe string in uppercase is->%s\n",str);
}

Error: too few arguments to function 'strcmp'

I am having a few issues with my code. First: when I try to compile, I get error: too few arguments to function 'strcmp'. I have looked all over and made multiple changes and am still unable to get it to work. Second: when my code does compile (if I remove the strcmp part), it will not complete the count functions correctly. Can anyone please assist? Thank you!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int count(char array[], int size);
int stringLen(char array[]);
int convert(char ch);
int value, n;
int main()
{
//char * str;
//char s;
char a[100];
char b[100];
char c[100];
int charCount = stringLen(a);
int lCount = count(a, charCount);
printf("Enter your string: \n");
scanf("%s \n", a);
printf("Enter your string: \n");
scanf("%s \n", b);
printf("Enter your string: \n");
scanf("%s \n", c);
printf("The count is %d, length is %d\n", lCount, charCount);
int i;
for(i = 0; i < charCount; i++)
{
char c = a[i];
printf("Char %s = %d \n", &c, value);
}
n = strcmp(char string1[], char string2[], char string3[]);
printf("The first string in the alphabet is: %d \n", n);
return 0;
}
int stringLen(char array[])
{
char count;
int index;
while(array[index] !=0)
{
count++;
index++;
}
return count;
}
int count(char array[], int size)
{
int count;
int i;
for(i = 0; i < size; i++)
{
if(array[i] == 'a')
{
count ++;
}
else if(array[i] == 'A')
{
count ++;
}
}
return count;
}
This is not right way to use strcmp.
n = strcmp(char string1[], char string2[], char string3[]);
strcmp is used for compararison of string. See doc
int result = strcmp (string1,string2)
If strings are same, function will return 0.

Resources