Learning C, segmentation fault - c

I'm just learning C after learning Java and I'm having a tough time...
This program is supposed to be a simple command prompt program that takes in a command like "sum 1 2" and adds outputs "3". The program tokenizes the input by space into a 2d array. So the first element will have the command and the following integers will be for arithmetic. As soon as I type "sum 1 2" I get a segmentation fault error and the program crashes.
I set every row in my 2d array to "NULL" so that I could know when to stop iterating through the rows. I looked everywhere and I would like to know if this was an incorrect way of doing it, and if there's a more effective way.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define tLength 20
#define tRow 7
char input[tLength];
char tokens[tRow][tLength];
char p[tLength];
char y[tLength];
int counter = 0;
void getTokens(char inp[]) {
strcpy(y, inp);
strcpy(p, strtok(y," "));
strcpy(tokens[counter], p);
counter = 1;
while (p!=NULL){
strcpy(p,strtok(NULL, " "));
if (counter < tRow) {
strcpy(tokens[counter], p);
counter++;
}
else {
printf("Cannot process more lines");
counter = 0;
break;
}
}
counter = 0;
}
void commandLine() {
int count;
for (count=0;count<tRow;count++){
strcpy(tokens[count],"NULL");
}
printf(">");
fgets(input, tLength, stdin);
getTokens(input);
if (strcmp("quit", tokens[0]) == 0 || strcmp("Quit", tokens[0]) == 0) {
printf("Bye!");
}
else if (strcmp("sum", tokens[0]) == 0 || strcmp("Sum", tokens[0]) == 0) {
int sum = 0;
counter = 1;
while (strcmp("NULL",tokens[counter])!=0) {
sum += atoi(tokens[counter]);
counter++;
}
counter = 0;
printf("%d", sum);
}
else if (strcmp("prod", tokens[0]) == 0 || strcmp("Prod", tokens[0]) == 0) {
int temp = 0;
int prod = 1;
counter = 1;
if (atoi(tokens[1]) == 0) {
printf("%d", 0);
}
else {
while (strcmp("NULL",tokens[counter])!=0) {
prod *= atoi(tokens[counter]);
counter++;
}
}
printf("%d", prod);
}
else {
printf("Error, unknown command");
}
}
void main(void) {
commandLine();
}

Several tips:
global variables when unecessary, are bad
use capital letters for macros
better initialize variables, than left uninitialized
use descriptive variable names ( not, p, y etc )
empty string can't be compared to "NULL"
POSIX offers strcasecmp which is case insensitive version of strcmp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXINPUTLEN 20
#define MAXTOKENS 7
void getTokens( char* input, char tokenized[][MAXINPUTLEN] )
{
int counter = 0;
char* token = strtok( input, " " );
while( token != NULL )
{
if( counter < MAXTOKENS )
{
strncpy( tokenized[counter], token, MAXINPUTLEN );
counter++;
}
else
{
printf( "Cannot process more lines" );
counter = 0;
break;
}
token = strtok( NULL, " " );
}
counter = 0;
}
void commandLine()
{
char input[MAXINPUTLEN] = {0};
printf( ">" );
fgets( input, MAXINPUTLEN, stdin );
char tokens[MAXTOKENS][MAXINPUTLEN] = {{0}};
getTokens( input, tokens );
if( strcasecmp( "quit", tokens[0] ) == 0 )
{
printf( "Bye!" );
}
else if( strcasecmp( "sum", tokens[0] ) == 0 )
{
int sum = 0;
for( int i = 1; tokens[i][0] != 0; ++i )
{
sum += atoi( tokens[i] );
}
printf( "%d", sum );
}
else if( strcasecmp( "prod", tokens[0] ) == 0 )
{
int prod = 1;
for( int i = 1; tokens[i][0] != 0; ++i )
{
prod *= atoi( tokens[i] );
}
printf( "%d", prod );
}
else
{
printf( "Error, unknown command" );
}
}
int main( void )
{
commandLine();
return 0;
}

Arrays in C work differently than in Java.
In C, arrays are primitive types. You cannot assign NULL to an array in C.
In Java, an array is an object reference; you need to create an array object and assign it to the array variable.
In fact, Java arrays are more like C pointers.
Your code makes excessive use of global variables and tries to call strcpy on a NULL pointer when you reach the last token.
You should use a pointer to capture the return value from strtok:
/* Returns the number of tokens captured. */
int getTokens(char inp[]) {
char *p;
int counter = 0;
strcpy(y, inp);
p = strtok(y," ");
while (p!=NULL){
if (counter < tRow) {
strcpy(tokens[counter], p);
counter++;
}
else {
printf("Cannot process more lines");
counter = 0;
break;
}
}
return counter;
}
Note: There may be more errors in the code. I never made it past getTokens.

Related

I am trying to make a function that checks whether all characters in a string are unique, and returns 0 if this is so, or 1 if not all are unique

I'm not sure what is wrong with my code. It just seems to print 0 most of the time, when I change string word. Would appreciate any help/comments - is it a problem with the logic in my code? If so, where, and how can I rectify it?
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int check_unique_letters(string words);
int main (void)
{
string word = "ABCB" ;
printf ("%i\n", check_unique_letters (word));
}
int check_unique_letters (string words)
{
int j = 0;
do
{ int x = (int) (words [j]) ;
int y = 0;
for (int i=j + 1; i<strlen(words); i++)
{
if ((int)words[i] == x)
{
y += 1;
}
else
{
y+= 0;
}
}
if (y>0)
{
return 1;
break;
}
else
{
j = j+1;
}
}
while (j < strlen (words));
}
The function returns nothing outside the do-while loop though its return type is not void.
That is after the do-while loop you need to place the statement
return 0;
Pay attention to that there is no need to use objects of the type int like
int x = (int) (words [j]) ;
and there is a redundant code that does not have an effect like this else
else
{
y+= 0;
}
or like the break statement
if (y>0)
{
return 1;
break;
}
And it would be more logically consistent if the function returned non-zero in case when all symbols are unique.
Also calling the function strlen is inefficient.
I would write the function the following way
#include <stdio.h>
#include <string.h>
int check_unique_letters( const char *s )
{
if (*s)
{
while (s[1] != '\0' && strchr( s + 1, *s ) == NULL) ++s;
}
return *s == '\0' || s[1] == '\0';
}
int main( void )
{
const char *s = "ABCB";
printf( "All characters of the string \"%s\" are %s.\n",
s, check_unique_letters( s ) ? "unique" : "not unique" );
s = "ABCD";
printf( "All characters of the string \"%s\" are %s.\n",
s, check_unique_letters( s ) ? "unique" : "not unique" );
}
The program output is
All characters of the string "ABCB" are not unique.
All characters of the string "ABCD" are unique.
Here is a quick implementation of what you need. You can quickly compare as to where you might be going wrong.
Feel free to modify the program to suit your needs.
You can see it running here.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check_unique_chars(char* str, int length)
{
// standard null checks
if (length == 0 || str == NULL)
return 1;
// The easiest way to do this is to, for each character
// compare every other character ahead of it. If there is a match, then return 1. Else return 0.
for(int idx =0; idx < length; ++idx)
{
for(int idx1 =idx+1; idx1 < length; ++idx1)
{
if (str[idx] == str[idx1])
return 1;
}
}
return 0;
}
int main(void)
{
char* s = "Hel0";
char* s1 = "banana";
printf("string has unique chars: %s\n", check_unique_chars(s, strlen(s)) == 0 ? "true" : "false");
printf("string has unique chars: %s\n", check_unique_chars(s1, strlen(s1)) == 0 ? "true" : "false");
return EXIT_SUCCESS;
}
I am reasonably sure this is not the most efficient implementation, but it is quite obvious in its operation and you should be able to modify it to suit your needs.

Check palindrome and debug

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define FALSE 0
#define TRUE 1
int alphabetic(char *string )
{
int i, valid;
valid = TRUE;
for ( i = 0; i < strlen(string); i++ )
{
if ( toupper ( string[i] ) < 'A' || toupper (string[i] ) > 'Z' )
valid = FALSE;
}
return valid;
}
int main()
{
char c, inputarray[10], temp[10];
int i = 0;
strcpy(temp, inputarray);
printf("%s Please enter string>");
while ( ( c = getchar () ) != '\n')
{
if ( i < 9 )
inputarray[i] = c;
i++;
}
if ( i < 10 )
inputarray[i] = '\0';
else
{
inputarray[9] = '\0';
printf("String too long\n");
return;
}
printf("%s\n",inputarray);
if (! alphabetic (inputarray) )
{
printf("Invalid input");
}
if (strcmp(strrev(inputarray),temp) == 0 )
printf("Palindrome\n");
else
printf("Not palindrome\n");
}
Trying this and still getting 'not palindrome' when input is a palindrome. It says 'stack around inputarray corrupted' when I run the program. Any ideas on how to fix it so reads palindrome and stop the input array being corrupted.
Here is one possible implementation. I've tried to explain in comments as much as I can but feel free toleave a comment if there is something that's not clear.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
int alphabetic(char *string)
{
int i, valid;
valid = true;
for (i = 0; i < strlen(string); i++)
{
if (toupper(string[i]) < 'A' || toupper(string[i]) > 'Z')
{
valid = false;
// break here we are done;
break;
}
}
return valid;
}
void printArray(char* str)
{
printf("Array = ");
for (int i = 0; i < strlen(str); ++i)
{
printf("%c", str[i]);
}
printf("\n");
}
bool isPalindrome(char* str1, char* str2)
{
bool isValidPalindrome = true;
int length = strlen(str1);
if (length != strlen(str2))
{
printf("Strings must be the same lenth");
isValidPalindrome = false;
}
else
{
--length;
for (int i = length; i >= 0; --i)
{
if (str1[i] != str2[length - i])
{
isValidPalindrome = false;
break;
}
}
}
return isPalindrome;
}
int main()
{
const int length = 10;
char c, inputarray[length], temp[length];
int i = 0;
// Comparing strings that have not been initialized
// produces undefined behavior. Imagine inputArray is equal to:
// inputArray: "my String ... some other unknown stuff"... where does
// the string ends? there is no '\n' in the horizon.
// The stack error you are getting is produced by the statement
// below. I've pusehd this statement below right after inputArray
// has been initialized
// strcpy(temp, inputarray);
// You don't need the format specifier %s unless you
// rewrite your printf statement as printf("%s", "Please enter string");
// for simplicity you can write it as follows
printf("Please enter string: ");
while ((c = getchar()) != '\n')
{
if (i < length - 1)
inputarray[i] = c;
i++;
}
// Pulled the code inside the if to avoid multiple returns // just preference... not needed
if (i < length)
{
inputarray[i] = '\0';
// helper function to print array
printArray(inputarray);
if (!alphabetic(inputarray))
{
printf("Invalid input");
}
// copy the strings here
strcpy(temp, inputarray);
// reverse the string here
strrev(inputarray);
// you will have to roll out your own isPalindrome
// implementation since reversing a string and comparing it
// with itself will always return false e.g.
// inputArray = "hello";
// copy inputArray into temp
// temp = "hello";
// reverse inputArray
// compare strings: "olleh" == "hello" -> false
if (isPalindrome(inputarray, temp) == true)
printf("Palindrome\n");
else
printf("Not palindrome\n");
}
else
{
inputarray[9] = '\0';
printf("String too long\n");
}
return 0;
}
Trying this and still getting 'not palindrome' when input is a palindrome. It says 'stack around inputarray corrupted' when I run the program.
The probable reason for both is that strcpy(temp, inputarray) is called before inputarray is entered. Move this immediately before the if (strcmp(strrev(inputarray),temp) == 0 ), and your program may work. Another error is the %s in printf("%s Please enter string>").

Read a char and a number in c

Im trying to read a char and number with the following:
char c;
char plus = '+';
int last;
if(scanf("%c%d",&c,&last)!=2 || last<0){
printf("fail\n");
return 1;
};
//trying to test it
if(plus==c){
// code
}
But when I start the program, and type + 100 it throws "fail", as scanf wasn't successful. But if I just type 100 it works. Why does "fail" get printed when there are one char (+) and number (100) and why it doesn't if I just type number.
Your code is fine except for a ; try this it works :
#include <stdio.h>
int main( )
{
test();
}
int test()
{
char c;
char plus = '+';
int last;
if ( scanf( "%c%d", &c, &last ) != 2 || last < 0 )
{
printf( "fail\n" );
return 1;
} ///////////// YOU HAD UNNEEDED ; HERE
else
{
printf( "\nyou entered:\n%c,%d", c, last );
getchar( );
}
//trying to test it
if ( plus == c )
{
// code
}
}

Palindrome finder in C?

I'm trying to make a palindrome finder in C and I don't know where it is going wrong, no matter what I get the output false on the 2 different ways that I have tried to code this. I have only just started C (in the past week) so if you could explain things simply that'd be great, thanks!
//way1
#include <stdio.h>
int read_char() { return getchar(); }
void read_string(char* s, int size) { fgets(s, size, stdin); }
void print_char(int c) { putchar(c); }
void print_string(char* s) { printf("%s", s); }
int is_palin(char word[]) {
int m = 0;
int arr_len = sizeof(word) / sizeof(char); //change to char_index
int n = arr_len;
int t = 1;
if(n % 2 != 0) {
for (m=0; m < ((n-1)/2); m++) {
if(word[m] != word[n-m-2]) {
t = 0;
}
else {
t = 1;
}
}
}
else {
for (m=0; m < (n/2)-1; m++) {
if(word[m] != word[n-m-2]) {
t = 0;
}
else {
t = 1;
}
}
}
if(t == 1) {
return 1;
}
else {
return 0;
}
}
int main(void) {
char word[6] = "civic";
int arr_len = sizeof(word)/sizeof(char);
if (is_palin(word) == 1) {
printf("is palin\n");
}
else {
printf("is not palin\n");
}
printf(word);
printf("\n");
printf("%d\n", arr_len);
return 0;
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//way2
#include <stdio.h>
int read_char() { return getchar(); }
void read_string(char* s, int size) { fgets(s, size, stdin); }
void print_char(int c) { putchar(c); }
void print_string(char* s) { printf("%s", s); }
int is_palin(char word[]) {
int m = 1;
int input_length = sizeof(word);
int j = input_length-1;
int i = 0;
for(i=0; i <= j; i++) {
if(word[i] != word[j]) {
m = 0;
j--;
}
}
if(m == 1) {
return 1;
}
else {
return 0;
}
}
int main(void) {
char word[6] = "civic";
int input_length = sizeof(word);
if (is_palin(word) == 1) {
printf("is palin\n");
}
else {
printf("is not palin\n");
}
printf(word);
printf("\n");
printf("%d\n", input_length);
return 0;
}
Please try this, it works fine.
#include <stdio.h>
int main( )
{
int flag = 0;
int length = 0;
int len2 = 0;
int i = 0;
char name[130];
char p[130];
char q[130];
printf( "please enter a name or sentence\n" );
scanf( "%[^\n]", name );
length = strlen( name );
len2 = length;
strcpy( p, name );
memset( q, '.', length ); // handy to debug comparaison
q[length] = '\0';
for ( i = 0; i < length; i++ )
{
q[--len2] = p[i];
}
printf( "\n p==%s", p );
printf( "\n q==%s", q );
getchar( );
if ( !strcmp( p, q ) )
flag = 1;
if ( flag == 1 )
printf( "\npalindrome\n" );
else
printf( "\nnot a palindrome\n" );
return 0;
}
Take a look at this code, that's how I have implemented it (remember to #include <stdbool.h> or it will not work):
for(i = 0; i < string_length; i++)
{
if(sentence[i] == sentence[string_lenght-1-i])
palindrome = true;
else
{
palindrome = false;
break;
}
}
Doing that it will check if your sentence is palindrome and, at the first occurence this is not true it will break the for loop. You can use something like
if(palindrome)
printf(..);
else
printf(..);
for a simple prompt for the user.
Example :
radar is palindrome
abba is palindrome
abcabc is not palindrome
Please , pay attention to the fact that
Abba
is not recognized as a palindrome due to the fact that ' A ' and 'a' have different ASCII codes :
'A' has the value of 65
'a' has the value of 97
according to the ASCII table. You can find out more here.
You can avoid this issue trasforming all the characters of the string to lower case characters.
You can do this including the <ctype.h> library and calling the function int tolower(int c); like that :
for ( ; *p; ++p) *p = tolower(*p);
or
for(int i = 0; str[i]; i++){
str[i] = tolower(str[i]);
}
Code by Earlz, take a look at this Q&A to look deeper into that.
EDIT : I made a simple program to do this, see if it can help you
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
void LowerCharacters(char *word, int word_lenth);
int main(void){
char *word = (char *) malloc(10);
bool palindrome = false;
if(word == 0)
{
printf("\nERROR : Out of memory.\n\n");
return 1;
}
printf("\nEnter a word to check if it is palindrome or not : ");
scanf("%s", word);
int word_length = strlen(word);
LowerCharacters(word,word_length);
for(int i = 0; i < word_length; i++)
{
if(word[i] == word[word_length-1-i])
palindrome = true;
else
{
palindrome = false;
break;
}
}
palindrome ? printf("\nThe word %s is palindrome.\n\n", word) : printf("\nThe word %s is not palindrome.\n\n", word);
free(word);
return 0;
}
void LowerCharacters(char *word, int word_length){
for(int i = 0; i < word_length; i++)
word[i] = tolower(word[i]);
}
Input :
Enter a word to check if it is palindrome or not : RadaR
Output :
The word radar is palindrome.

how to find duplicate string in an array of strings

I have an array of string from which i have to find duplicate string and then remove that duplicate string like i have string
char aa[50]="Amit Hanish Mahesh Amit"
Now Amit is duplicate and have to remove it from string .
#include "string.h"
main()
{
char x[100] = "Amit Hanish Mahesh Amit";
char y[3][100];
int i = 0, k = 0, j = 0, c = 0, end, t;
int current = 1;
while (x[i] != '\0') {
if (x[i] != ' ') {
y[k][j] = x[i];
j++;
i++;
} else {
// c = c + 1;
i++;
k++;
j = 0;
}
y[k][j] = '\0';
}
for (end = 1; end <= 3; end++) {
for (t = 0; t < end; t++) {
if (strcmp(y[end], y[t]) == 0) break;
}
if (end == t) {
strcpy(y[current],y[t]);
current++;
}
}
y[current] = 0;
printf("%s",y);
}
I have written a smalll routine for it .Does not seems to be worked .Any one have any suggestion where i am going wrong?
The other answers you got work fine for a small number strings (your example code only has 4). But, if you're comparing a large number this will be quite slow since you're doing n^2 comparisons. I'd suggest first splitting the string into an array of strings, then sorting the array using qsort(). In a sorted array all duplicates are guaranteed to be adjacent. This reduces the time from n^2 to n log n -- the time required to sort.
I would split the string array using strtok (see the man page).
So I would have something like this
char x[100]="Amit Hanish Mahesh Amit";
/* Preparing the result string */
size_t sz_result = sizeof(char) * (strlen(x) + 1);
char* result = (char*) malloc( sz_result );
result[0] = '\0';
/* Parsing the string from one element to the other */
char* elm = strtok(x, " ");
while( (elm = strtok(NULL, " ")) != NULL )
{
...
You will have each element of the string to verify if they are unique.
Then I would use something like a hashmap (you can use the one from the glib) or I would put the read string element in a new string only if it is not already in.
Here is an example for the second solution:
...
/* Is the element already in the result string? */
if ( strstr(result, elm) == NULL )
{
/* Then we should add it */
snprintf( result, sz_result - 1, "%s %s", result, elm );
}
}
In the end if you want x to be modified, you simply copy result in x:
strncpy( x, result, 99 );
Here is a sample code (not optimised, not using the strn* primitives, etc.)
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char x[100]="Amit Hanish Mahesh Amit";
/* Preparing the result string */
size_t sz_result = sizeof(char) * (strlen(x) + 1);
char* result = (char*) malloc( sz_result );
result[0] = '\0';
/* Parsing the string from one element to the other */
char* elm = strtok(x, " ");
if (elm != NULL) strcpy(result, elm);
while( (elm = strtok(NULL, " ")) != NULL )
{
/* Is the element already in the result string? */
if ( strstr(result, elm) == NULL )
{
/* Then we should add it */
strcat( result, " " );
strcat( result, elm );
}
}
strcpy( x, result );
fprintf( stdout, "Result: %s\n", x );
}
To remove duplicates from an array without preserving the order of elements:
sort the array
copy unique elements to the beginning of the array
remove the tail with duplicate elements
int remove_duplicates(StringArray array) {
if (! (array and array->items)) return 0; // empty array or NULL
StringArray_sort(array); // sort
// unique_copy()
String result = array->items, last = array->items + array->size;
for (String first = array->items; first != last; ++result) {
String_copy(result, first); // copy first to result
for (String prev = first; ++first != last and String_cmp(prev, first) == 0;)
{ /* skip adjacent equal items */ }
}
// shrink
return StringArray_remove(array, result, last);
}
Example
int main() {
char text[] = "Mahesh Amit Hanish Amit";
StringArray array = split(text, sizeof(text));
StringArray_dump(array, "<"); // print array before removing duplicates
if (remove_duplicates(array) < 0)
perror("error remove_duplicates(), OS error if any");
StringArray_dump(array, ">"); // print it after
StringArray_destroy(array);
return 0;
}
Where split() is:
StringArray split(const char* text, size_t size) {
if (! (text and text[size-1] == '\0')) return NULL;
StringArray array = StringArray_create();
if (! array) return NULL;
size_t n = -1;
for (const char* p = text; p != text+size; p += n+1) {
n = strcspn(p, " \t\n"); // find index of the next whitespace
if (n == 0) continue; // skip consecutive whitespace
// append characters in range [p, p+n)
// as a string to the array
const String string = String_create(p, n);
if (StringArray_append(array, string) < 0) {
String_destroy(string);
StringArray_destroy(array);
return NULL;
}
String_destroy(string);
}
return array;
}
Output
Mahesh<Amit<Hanish<Amit<
Amit>Hanish>Mahesh>
Full source code
I'm pretty sure, that the following line is not intended (assignment, not comparison)
if (end = t) {
See what happens, if you code a == and come back, if you still have problems.
Hint: Always code blanks around operators, so expressions are easier to read.
It's always fun to try to solve this kind of simple problems in C as exercise. Here's my take.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* strstrn(const char *haystack, const char *needle, size_t needle_len)
{
while((haystack = strchr(haystack, *needle)))
{
if (strncmp(haystack, needle, needle_len) == 0)
return (char *) haystack;
haystack++;
}
return NULL;
}
char* find_duplicate(const char* str, size_t len, size_t dup_len)
{
for(size_t i = 0; i < (len - dup_len); i++)
{
char* r = strstrn(str + i + 1, str + i, dup_len);
if(r) return r;
}
return NULL;
}
int main(int argc, char** argv)
{
if(argc < 3)
{
fprintf(stderr, "Usage: %s haystack dup_size\n", argv[0]);
return 1;
}
char* haystack = argv[1];
size_t len = atoi(argv[2]);
char* r;
while((r = find_duplicate(haystack, strlen(haystack), len)))
{
strcpy(r, r + len);
}
puts(haystack);
return 0;
}
/*
* C Program to Find the Frequency of Every Word in a
* given String
*/
#include <stdio.h>
#include <string.h>
void main()
{
int count = 0, c = 0, i, j = 0, k, space = 0;
char str[100], p[50][100], str1[20], ptr1[50][100];
printf("Enter the string\n");
scanf(" %[^\n]s", str);
printf("string length is %d\n", strlen(str));
for (i = 0;i<strlen(str);i++)
{
if ((str[i] == ' ')||(str[i] == ', ')||(str[i] == '.'))
{
space++;
}
}
for (i = 0, j = 0, k = 0;j < strlen(str);j++)
{
if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))
{
p[i][k] = '\0';
i++;
k = 0;
}
else
p[i][k++] = str[j];
}
k = 0;
for (i = 0;i <= space;i++)
{
for (j = 0;j <= space;j++)
{
if (i == j)
{
strcpy(ptr1[k], p[i]);
k++;
count++;
break;
}
else
{
if (strcmp(ptr1[j], p[i]) != 0)
continue;
else
break;
}
}
}
for (i = 0;i < count;i++)
{
for (j = 0;j <= space;j++)
{
if (strcmp(ptr1[i], p[j]) == 0)
c++;
}
printf("%s -> %d times\n", ptr1[i], c);
c = 0;
}
}

Resources