I'm trying to implement a C program for CRC for my laboratory exam. I've written the following code. But, it is same as the main source code but it got stuck at strcpy(). I can't figure it out. Here's the source code:
#include <stdio.h>
#include <string.h>
int ctoi(char a) {
return (a-48);
}
char itoc(int a) {
return (a+48);
}
void main() {
int ld, lm, i, j;
char div[20], ip[100], ipm[100], crc[10], snd[100]={0}, rcv[100];
printf("\t\t\tCRC Encoding\n");
printf("Enter the codeword: ");
scanf("%s",ip);
printf("Enter the divisor: ");
scanf("%s",div);
strcpy(ipm, ip);
lm = strlen(ipm);
ld = strlen(div);
if(lm>=ld) {
//padding
for(i=lm, j=0; j<ld-1; j++)
ipm[i++] = '0';
ipm[i] = '\0';
printf("Data word after appending zeroes: %s\n", ipm);
for(i=0;i<ld;i++)
crc[i] = ipm[i];
crc[i] = '\0';
for(;i<strlen(ipm);i++) {
if(crc[0] == '1') {
for(j=0; j<ld; j++)
crc[j] = itoc((ctoi(crc[j])) ^ (ctoi(div[j])));
}
crc[ld] = ipm[i];
crc[ld+1] = '\0';
for(j=0;crc[j]!='\0';j++)
crc[j] = crc[j+1];
crc[j] = '\0';
}
for(j=0;crc[j]!='\0';j++)
crc[j] = crc[j+1];
crc[j] = '\0';
printf("CRC remainder is: %s\n",crc);
strcat(snd, ip);
strcat(snd, crc);
printf("Sent codeword: %s\n",snd);
printf("CRC Decoding\n");
strcpy(rcv, snd);
printf("after strcpy");
printf("Received codeword: %s", rcv);
for(i=0;i<ld;i++) {
crc[i] = rcv[i];
}
crc[i] = '\0';
for( ;i<strlen(rcv);i++) {
if(crc[0]=='1') {
for(j=0;j<ld;j++) {
crc[j] = itoc((ctoi(crc[j])) ^ (ctoi(div[j])));
}
}
crc[ld] = rcv[i];
crc[ld+1] = '\0';
for (j = 0; crc[j]!='\0'; i++) {
crc[j] = crc[j+1];
}
crc[j] = '\0';
for(j=0; crc[j]!='\0'; j++) {
if(crc[j]!='0')
break;
}
printf("CRC remainder is: %s\n",crc);
if(j==strlen(crc)) {
printf("Received message is error free\n");
} else {
printf("Received message contains errors\n");
}
}
} else {
printf("Enter a proper divisor");
}
}
And the output screen is:
CRC Encoding
Enter the codeword: 10110
Enter the divisor: 110
Data word after appending zeroes: 1011000
CRC remainder is: 00
Sent codeword: 1011000
CRC Decoding
It is stuck at the above line. But the correct program is here:
#include<stdio.h>
#include<string.h>
int ctoi(char a)
{
return(a-48);
}
char itoc(int a)
{
return(a+48);
}
void main()
{
char ip[100],ipm[100],div[20],crc[10],sent[100]={0},rec[100];
int i,lm,ld,j;
printf("\nCRC encoding");
printf("\nEnter data word(message)");
scanf("%s",ip);
printf("\nEnter the divisor");
scanf("%s",div);
strcpy(ipm,ip);
lm=strlen(ipm);
ld=strlen(div);
if(lm>=ld)
{
for(i=lm,j=0;j<ld-1;j++)
ipm[i++]='0';
ipm[i]='\0';
printf("\nData word after appending zeros:%s",ipm);
for(i=0;i<ld;i++)
crc[i]=ipm[i];
crc[i]='\0';
for(;i<strlen(ipm);i++)
{
if(crc[0]=='1')
{ for(j=0;j<ld;j++)
{
crc[j]=itoc((ctoi(crc[j]))^(ctoi(div[j])));
}
}
crc[ld]=ipm[i];
crc[ld+1]='\0';
for(j=0;crc[j]!='\0';j++)
crc[j]=crc[j+1];
crc[j]='\0';
}
for(j=0;crc[j]!='\0';j++)
crc[j]=crc[j+1];
crc[j]='\0';
printf("\nCRC remainder is:%s",crc);
strcat(sent,ip);
strcat(sent,crc);
printf("\nCode word in sender side is:%s",sent);
printf("\nCRC decoing");
strcpy(rec,sent);
//rec[2]='1';
printf("\nReceived message in receiver side is :%s",rec);
for(i=0;i<ld;i++)
crc[i]=rec[i];
crc[i]='\0';
for(;i<strlen(rec);i++)
{
if(crc[0]=='1')
{
for(j=0;j<ld;j++)
{
crc[j]=itoc((ctoi(crc[j]))^(ctoi(div[j])));
}
}crc[ld]=rec[i];
crc[ld+1]='\0';
for(j=0;crc[j]!='\0';j++)
crc[j]=crc[j+1];
crc[j]='\0'; }
for(j=0;crc[j+1]!='\0';j++)
crc[j]=crc[j+1];
crc[j]='\0';
for(j=0;crc[j]!='\0';j++)
{
if(crc[j]!='0')
break;
}
printf("\nCRC remainder is:%s",crc);
if(j==strlen(crc))
printf("\nReceived message is error free!\n\n");
else
printf("\nError in received message!!!\n\n");
}
else
printf("\nEnter proper divisor");
}
And it runs perfect. I can't able to find the difference between these two programs even though I went through line by line. Can you tell me what's wrong with the first program?
The problem arises from a small typo in your code. You wrote
for (j = 0; crc[j]!='\0'; i++) {
crc[j] = crc[j+1];
}
but it should have been
for (j = 0; crc[j]!='\0'; j++) {
crc[j] = crc[j+1];
}
Mind the j++. Of course, the call to strcpy() is no culprit!
Related
Every time I try and run only the first loop runs. It ask how many items were sold and then just stops running. I'm not really sure what I did wrong. If anyone has any tips that would be great(story if there's some formatting issues, SO wouldn't let me post the question without them).
Everything else looks fine but if any other improvements could be made please let me know.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int digit(char term[])
{
int i = 0;
int val = 0;
while (term[i] != '\0')
{
val = val * 10 + term[i] - '0';
i++;
}
return val;
}
void error()
{
printf("Error: Sales figures must be numbers.\n");
printf("Please try again.\n");
}
bool isnumber(char term[])
{
int i = 0;
while (term[i])
{
if( isdigit(term[i]) == 0)
{
return false;
i++;
}
}
return true;
}
int main()
{
int sales[3][2], costs[3] = {3, 4, 1}, weekends[2] = {0, 0};
int i, j, val;
char term[100];
while (1)
{
printf("Number of Bagel sales on Saturday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[0][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Flatbread sales on Saturday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[1][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Muffin sales on Saturday: ");
scanf("%s", term);
if (isnumber(term) == false)
{
error;
}
else
{
sales[2][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Bagel sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[0][1] = digit(term);
break;
}
}
while (1)
{
printf("Number of Flatbread sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[1][1] = digit(term);
break;
}
}
while (1)
{
printf("Number of Muffin sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[2][1] = digit(term);
break;
}
}
for (i = 0; i < 2, i++;)
{
for (j = 0; j < 3, j++;)
{
weekends[i] += costs[j] * sales[i][j];
}
}
printf("\n");
for (i = 0; i < 3, i++;)
{
printf("%d", costs[i]);
}
printf(".");
for (i = 0; i < 3, i++;)
{
for (j = 0; j < 2, j++;)
{
printf("%d", sales[i][j]);
}
if (i == 0)
{
printf(" = ");
printf("%d %d", weekends[0], weekends[1]);
}
printf("\n ");
}
printf("\nTotal sales on Saturday: $%d", weekends[0]);
printf("\nTotal sales on Sunday: $%d", weekends[1]);
printf("\nTotal sales over the weekend: $%d", weekends[0] + weekends[1]);
return 0;
}
You have an infinite loop in isnumber(). It will return false if the first character is not a digit. But if the first character is a digit, it never increments i, so it keeps testing the first character repeatedly.
i++ should not be in the if statement.
bool isnumber(char term[])
{
int i = 0;
while (term[i])
{
if(!isdigit(term[i]))
{
return false;
}
i++;
}
return true;
}
And as others have pointed out, you need to put () after error to call it.
As for your programming stopping; you use isdigit on line 30 without declaring it.
Along that, you use error everywhere without invoking it. Use error() to invoke the function and print the error-information.
Just a small formatting tip as well: instead of if (something == false) use if (!something)
Your use of error; is incorrect. You should write error(); instead.
Why am I getting this message in hackerrank "~ no response on stdout ~"? I don't know what I am missing?
I am bit frustrated right now because I have no clue about what to do.
So I was left with only choice to post this query on Stackoverflow.
Here is the link to the problem
Here is my complete code:
char* readline();
// Complete the countingValleys function below.
int countingValleys(int n, char* s)
{
int dwnhl = 0, level = 0;
bool frmsurface = true;
int k = strlen(s);
for (int i = 0; i < k; i++)
{
if (level == 0)
{
frmsurface = true;
}
if (s[i] == 'D')
{
level--;
if ((level < 0) && (frmsurface == true))
{
dwnhl++;
frmsurface = false;
//printf("went downhill %d ",i);
}
}
else if (s[i] == 'U')
{ //printf("went uphill %d ",i);
level++;
}
// printf("\nhello - %c",s[i]);
}
printf("\nNumber of downhill = %d \n", dwnhl);
return (dwnhl);
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
char* n_endptr;
char* n_str = readline();
int n = strtol(n_str, &n_endptr, 10);
if (n_endptr == n_str || *n_endptr != '\0')
{
exit(EXIT_FAILURE);
}
char* s = readline();
int result = countingValleys(n, s);
printf("%d\n", result);
return 0;
}
char* readline()
{
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true)
{
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line)
{
break;
}
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n')
{
break;
}
size_t new_length = alloc_length << 1;
data = realloc(data, new_length);
if (!data)
{
break;
}
alloc_length = new_length;
}
if (data[data_length - 1] == '\n')
{
data[data_length - 1] = '\0';
}
data = realloc(data, data_length);
return data;
}
One problem is the way you handle frmsurface
The first time you enter the loop frmsurface is set to true. If the events are UUDD, your code will still count a "valley" because you don't clear frmsurface when you go up.
Instead of
if(level==0)
{
frmsurface=true;
}
you could try:
frmsurface = (level == 0);
but I don't really understand why you want the boolean. Just test for level == 0 instead. Something like:
if(s[i]=='D')
{
if(level==0)
{
dwnhl++;
}
level--;
}
else if (s[i]=='U')
{
level++;
}
Also I wonder if this line:
printf("\nNumber of downhill = %d \n", dwnhl);
must be removed.
Notice that
int k=strlen(s);
for(int i=0;i<k;i++)
could probably just be
for(int i=0;i<n;i++)
^
as n is passed to the function
I now need to output the Morse code equivalent of alphanumeric input. My condition for check this is an if loop: I try to look at each element of input array with each element of the alpha array but a match never seems to be found. I am not sure if I am using the correct method. I try to de-reference the point to input and compare the value with each element of alpha until a match is found. If no match is found then an error occurs.
Not working:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
int main(void)
{
char *morse[] = {"/",
".-","-...","-.-.","-..",".","..-.","--","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
"..-","...-",".--","-..-","-.--","--..",
"-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};
char *alpha[]= {" ",
"A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y", "Z",
"0", "1","2","3","4","5","6","7","8","9"};
char *print_array[50];
int print_array_index = 0;
char hold[50];
int hold_index = 0;
char input[200];
int i = 0;
printf("welcome to the Morse translator.\n");
printf("Enter input: ");
fgets(input, sizeof(input), stdin);
char *p;
for (p=input; *p !='\0';++p)
{
*p = toupper(*p);
}
if (input[0]=='-' || input[0]=='.')
{
while (input[i] !='\0') {
if (input[i] ==' ' || input[i] == '\n')
{
hold[hold_index] = '\0';
bool found = false;
for (int x = 0; x < sizeof(morse) / sizeof(char *); x++)
{
if (strcmp(morse[x], hold) == 0)
{
print_array[print_array_index++] = alpha[x];
found = true;
break;
}
}
if (!found)
{
fprintf(stderr, "invalid Morse code!\n");
}
hold_index = 0;
}
else
{
hold[hold_index++] = input[i];
}
i++;
}
for (int x = 0; x < print_array_index; x++)
{
printf("%s", print_array[x]);
}
printf("\n");
}
else if (isalnum(input[0]))
{
while (input[i]!='\0')
{
if (input[i] ==' ' || input[i] == '\n')
{
bool found = false;
for (int x=0; x < sizeof(alpha)/sizeof (char*);x++)
{
if (alpha[x]==input[i])
{
print_array [print_array_index++] = alpha[x];
found = true;
break;
}
}
if (!found)
{
fprintf(stderr, "Invalid input!\n");
}
hold_index = 0;
}
i++;
}
for (int x=0; x < print_array_index; x++)
{
printf("%s",print_array[x]);
}
printf("\n");
}
return 0;
}
part of else if (isalnum(input[0])){ should be something like
else if (isalnum(input[0])){
while (input[i]!='\0' && input[i]!='\n'){
bool found = false;
for (int x=0; x < sizeof(alpha)/sizeof(char*);x++){
if (*alpha[x]==input[i]){
print_array[print_array_index++] = morse[x];
found = true;
break;
}
}
if(!found){
fprintf(stderr, "Invalid input!\n");
}
i++;
}
for (int x=0; x < print_array_index; x++){
printf("%s", print_array[x]);
}
printf("\n");
}
This question already has answers here:
Why I am getting the segmentation fault in following c program
(2 answers)
Closed 7 years ago.
I'm trying to solve the Next Palindrome problem on SPOJ. Here is the link to the problem SPOJ
This is my code for the problem. I get correct results when I run it on my machine for the following test cases :
9 11 99 101 808 818
This is my code :
#include<stdio.h>
#include<string.h>
char k[1000004];
int find_palin(char num[])
{
int len = strlen(num);
char str1[1000004] = {NULL};
char str3[500002] = {NULL};
char str2[500002] = {NULL};
char rev[500002] ={NULL};
if(len%2==0)
{
int half = (len)/2;
int i;
int j=0;
for(i=1;i<=len-1;++i)
{
if(num[i]!='0')
break;
k[i]='0';
}
if(i>len-1)
{
k[0]=num[0];
k[len-1]=num[0];
return 0;
}
for(i=0;i<half;++i)
str1[i] = num[i];
for(i=half;i<len;++i)
str3[j++] = num[i];
for(i=0 , j=half-1;i<half;++i,--j)
{
if(str3[i]>=str1[j])
break;
else
rev[i]=str1[j];
}
if(i>=half)
{
strcat(str1,rev);
for(i=0;i<len;++i)
k[i]=str1[i];
return 0;
}
else
{
for(i=0;i<half;++i)
str3[i] = '0';
if(str1[half-1]!='9')
str1[half-1]++;
else
{
for(i=half-1;i>=0;--i)
{
if(str1[i]=='9')
str1[i] = '0';
else
{
str1[i]++;
break;
}
}
if(i<0)
{
str1[half]='0';
str1[0] = '1';
}
}
strcat(str1,str3);
find_palin(str1);
}
}
else
{
int half = (len-1)/2;
int i;
int j=0;
if(len==1)
{
if(num[0]=='9')
{
k[0] = '1';
k[1] = '1';
return 0;
}
k[0] = ++num[0];
return 0;
}
for(i=1;i<=len-1;++i)
{
if(num[i]!='0')
break;
k[i]='0';
}
if(i>len-1)
{
k[0]=num[0];
k[len-1]=num[0];
return 0;
}
for(i=0;i<half;++i)
str1[i] = num[i];
for(i=half+1;i<len;++i)
str3[j++] = num[i];
str2[0] = num[half];
for(i=0 , j=half-1;i<half;++i,--j)
{
if(str3[i]>=str1[j])
break;
else
rev[i]=str1[j];
}
if(i>=half)
{
strcat(str2 , rev);
strcat(str1 , str2);
for(i=0;i<len;++i)
k[i]=str1[i];
return 0;
}
else
{
for(i=0;i<half;++i)
str3[i] = '0';
if(str2[0]!='9')
{
str2[0]++;
strcat(str2,str3);
strcat(str1,str2);
find_palin(str1);
}
else
{
str2[0] = '0';
if(str1[half-1]!='9')
str1[half-1]++;
else
{
for(i=half-1;i>=0;--i)
{
if(str1[i]=='9')
str1[i] = '0';
else
{
str1[i]++;
break;
}
}
if(i<0)
{
str1[half]='0';
str1[0] = '1';
}
}
strcat(str2,str3);
strcat(str1,str2);
find_palin(str1);
}
}
}
}
int main()
{
char input[1000004];
int t;
scanf("%d" , &t);
int i;
for(i=0;i<t;++i)
{
scanf("%s" , &input);
find_palin(input);
printf("%s\n" , k);
}
return 0;
}
When I try to submit the code it gives Segmentation Fault. Can someone please help me as to why I'm getting this error?
The way to use large arrays is to allocate memory on the heap not on the stack. I have done this in your program to show you. Instead of doing that in main I lazily moved input[] to be a global var. You can't do that in find_palin because of the recursion, and anyway global variables are frowned on.
I also changed all your return 0 statements to goto so that a common clean-up can be done. It is not elegant, but I didn't want to change the structure of your code.
I also tweaked a few other things, such as checking the input was valid, and using a single #define from which all other sizes specified are derived.
#include<stdio.h>
#include<string.h>
#define ARRSIZE 1000004 // don't hard code stuff
char k[ARRSIZE];
char input[ARRSIZE]; // move out of main
void find_palin(char num[]) // change return type to void
{
int len = strlen(num);
char *str1 = calloc(ARRSIZE, sizeof(*str1)); // allocate and zero
char *str2 = calloc(ARRSIZE/2, sizeof(*str2));
char *str3 = calloc(ARRSIZE/2, sizeof(*str3));
char *rev = calloc(ARRSIZE/2, sizeof(*rev));
if (str1 == NULL || str2 == NULL || str3 == NULL || rev == NULL)
exit (1); // check memory allocations
if(len%2==0)
{
int half = (len)/2;
int i;
int j=0;
for(i=1;i<=len-1;++i)
{
if(num[i]!='0')
break;
k[i]='0';
}
if(i>len-1)
{
k[0]=num[0];
k[len-1]=num[0];
goto endfunc; // replace all return statements
}
for(i=0;i<half;++i)
str1[i] = num[i];
for(i=half;i<len;++i)
str3[j++] = num[i];
for(i=0 , j=half-1;i<half;++i,--j)
{
if(str3[i]>=str1[j])
break;
else
rev[i]=str1[j];
}
if(i>=half)
{
strcat(str1,rev);
for(i=0;i<len;++i)
k[i]=str1[i];
goto endfunc;
}
else
{
for(i=0;i<half;++i)
str3[i] = '0';
if(str1[half-1]!='9')
str1[half-1]++;
else
{
for(i=half-1;i>=0;--i)
{
if(str1[i]=='9')
str1[i] = '0';
else
{
str1[i]++;
break;
}
}
if(i<0)
{
str1[half]='0';
str1[0] = '1';
}
}
strcat(str1,str3);
find_palin(str1);
}
}
else
{
int half = (len-1)/2;
int i;
int j=0;
if(len==1)
{
if(num[0]=='9')
{
k[0] = '1';
k[1] = '1';
goto endfunc;
}
k[0] = ++num[0];
goto endfunc;
}
for(i=1;i<=len-1;++i)
{
if(num[i]!='0')
break;
k[i]='0';
}
if(i>len-1)
{
k[0]=num[0];
k[len-1]=num[0];
goto endfunc;
}
for(i=0;i<half;++i)
str1[i] = num[i];
for(i=half+1;i<len;++i)
str3[j++] = num[i];
str2[0] = num[half];
for(i=0 , j=half-1;i<half;++i,--j)
{
if(str3[i]>=str1[j])
break;
else
rev[i]=str1[j];
}
if(i>=half)
{
strcat(str2 , rev);
strcat(str1 , str2);
for(i=0;i<len;++i)
k[i]=str1[i];
goto endfunc;
}
else
{
for(i=0;i<half;++i)
str3[i] = '0';
if(str2[0]!='9')
{
str2[0]++;
strcat(str2,str3);
strcat(str1,str2);
find_palin(str1);
}
else
{
str2[0] = '0';
if(str1[half-1]!='9')
str1[half-1]++;
else
{
for(i=half-1;i>=0;--i)
{
if(str1[i]=='9')
str1[i] = '0';
else
{
str1[i]++;
break;
}
}
if(i<0)
{
str1[half]='0';
str1[0] = '1';
}
}
strcat(str2,str3);
strcat(str1,str2);
find_palin(str1);
}
}
}
endfunc: // free the memory
free(rev);
free(str3);
free(str2);
free(str1);
}
int main()
{
int t;
int i;
if (1 != scanf("%d" , &t)) // check garbage input
exit (1);
for(i=0;i<t;++i)
{
if (1 != scanf("%s" , input)) // remove &
exit (1); // check garbage input
find_palin(input);
printf("%s\n" , k);
}
return 0;
}
I have written following code to find str1 is present in str2 or not. But it doesn't work all scenarios.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int i,j,flag=1;
char str1[]="goa",str2[]="gogoa";
if (strlen(str1)>strlen(str2))
{
printf("not found");
return;
}
for ( i = 0; str2[i]; i++)
{
if (str1[0]==str2[i])
{
for ( j = 0; str1[j]; j++)
{
if (str1[j]!=str2[i+j])
{
printf("not found");
flag=0;
}
}
break;
}
}
if (flag==1)
{
printf("found at index %d ",i);
}
getchar();
}
its not working when str1 starting character is present multiple times in str2,otherwise it works fine.
How can I optimize this to make it work in all scenarios?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int i, j, flag=0, len1, len2;
char str1[]="goa",str2[]="gogoa";
len1 = strlen(str1);
len2 = strlen(str2);
if (len1 > len2){
printf("not found");
return 0;
}
for (i = 0; i <= len2 - len1; ++i){
if (str1[0]==str2[i]){
flag = 1;
for (j = 1; str1[j]; ++j){
if (str1[j]!=str2[i+j]){
flag=0;
break;
}
}
if(flag==1)
break;
}
}
if (flag==1){
printf("found at index %d ",i);
} else {
printf("not found");
}
return 0;
}
try this:
int i,j,flag=0, match=0;
char str1[]="goa",str2[]="gogoa";
if (strlen(str1)>strlen(str2))
{
printf("not found");
return;
}
for ( i = 0; str2[i]; i++)
{
if (str1[0]==str2[i])
{
match=1;
for ( j = 0; str1[j]; j++)
{
if (str1[j]!=str2[i+j])
match=0;
}
if(match == 1)
{
flag = 1;
break;
}
}
}
if (flag==1)
printf("found at index %d ",i);
else
printf("not found");
I agree to comments that you should show what you have done. Please do that next time.
For now, I would like to suggest two approaches to debug out of sticky problems: use printfs and understand the flow OR use gdb. I have commented your program w.r.t. the printf approach. Have not tested all cases...but this should hopefully give you a direction to your debugging. Hope it helps. Remember to do this step before you post a question next time :-)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int i,j,flag=1;
char str1[]="goa",str2[]="gogoa";
if (strlen(str1)>strlen(str2)) {
printf("not found");
return;
}
for ( i = 0; str2[i]; i++) {
/* If starting letter does not match, forget it */
if (str1[0]==str2[i]) {
flag = 1;
// Iterate thro' str1
for ( j = 0; str1[j]; j++) {
if (str1[j] != str2[i+j]) {
printf("%s not found at index %d of %s\n", str1, i, str2);
flag=0;
break; // out of inner for loop
}
}
if (j == strlen(str1)) {
// found the entire string.
break; // out of outer for loop
}
}
}
if (flag==1) {
printf("%s found at index %d of %s\n", str1, i, str2);
}
getchar();
}
I have rewritten the code with slight modifications in it and it's working for me,
check all your necessary scenarios, and let me know.
public static bool findString(string searchText, string fullText)
{
bool result = false;
if (searchText.Length > fullText.Length)
{
return false;
}
for (int i = 0; i <= (fullText.Length - searchText.Length); i++)
{
if (searchText[0] == fullText[i])
{
for (int j=0; j<searchText.Length ;j++)
{
if ((i+j)< fullText.Length && searchText[j] == fullText[i+j])
result = true;
else
{
result = false;
break;
}
}
}
if (result)
{
Console.WriteLine("found at index {0}", i);
break;
}
}
return result;
}
Sorry about the syntax, I only had c# available. you'll have to use strlen(string) for lengths and '%d' for '{0}', printf for Console.Writeline(" ");
#include <string.h>
if(strstr(str2, str1) != NULL) {
/* ... */
}
Please checkout the doc of strstr function.
//Declaration: char *strstr(const char *str1, const char *str2);
//Return: It returns a null pointer if no match is found.
#include <string.h>
#include <stdio.h>
int main(void)
{
char *p;
p = strstr("goa", "gogoa");
printf(p);
return 0;
}