Sorting string lexicographically in c - c

I want to sort words of a string in lexicographical order.
For Example:
I have a string: I am Apple
Output should be: am Apple I
Problem (output):
enter the string
hello shamsh
the sorted array:
hello
It's not sorting the string and whole string is not being shown in the output, can anyone help me out here. Thanks!
Program code:
#include<stdio.h>
#include<string.h>
void main()
{
char a[25][25],t[25];
char s[200];
char * pch;
int count = 0;
int i,j ,n;
printf("enter the string\n");
gets(s);
pch = strtok (s," ,.-");
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ')
count++;
}
count=count+1;
i=0;
while(pch != NULL)
{
strcpy(a[i],pch);
pch = strtok (NULL, " ,.-");
i++;
}
for(i=0;i<count-1;i++)
{
for(j=i+1;j<count;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(t,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],t);
}
}
}
printf("the sorted array:\n");
for(i=0;i<count;i++)
printf("%s\n",a[i]);
}

If you try to print your string after you pch = strtok (s," ,.-"), you'll notice that your string is broken up. That's because strtok() is destructive and breaks up the string into tokens so you need to count the number of white spaces before calling strtok():
printf("enter the string\n");
gets(s);
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ')
count++;
}
count=count+1;
i=0;
pch = strtok (s," ,.-");
Also like Weather Vane said, don't use gets(), use fgets() instead oand remove the '\n' from end of the string afterwards. Also you can use realloc() to assign more memory to a dynamic array instead of using a static array since you wouldn't know the number of words in a string beforehand.
#include <stdlib.h>
#include<stdio.h>
#include<string.h>
void main()
{
char** a = NULL;
char t[25];
char s[512];
char * pch;
int count = 0;
int i,j ,n;
printf("enter the string\n");
if(fgets(s,512, stdin)==NULL)
{
printf("failed to read string\n");
exit(-1);
}
/*remove '\n' from end of the string*/
char *pos;
if ((pos=strchr(s, '\n')) != NULL)
*pos = '\0';
pch = strtok(s, " ,.-");
while(pch)
{
a = realloc(a, sizeof(char*)*++count);
if(a==NULL)
{
perror("failed to allocate memory\n");
exit(-1);
}
a[count-1] = pch;
pch = strtok(NULL, " ,.-");
}
for(i=0;i<count;i++)
printf("%d: %s\n", i, a[i]);
///...compare array

Use qsort() for this sort of thing.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUF_SIZE 0x100
int strcmp_wrapper(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
int main () {
char buffer[BUF_SIZE], *tokens[BUF_SIZE / 2 + 1];
int i = 0, j = 0;
printf("Enter a string: ");
fgets(buffer, BUF_SIZE, stdin);
tokens[0] = strtok(buffer, " ,.-\n");
while ((tokens[++i] = strtok(NULL, " ,.-\n")));
qsort(tokens, i, sizeof(tokens[0]), strcmp_wrapper);
while (j < i)
printf("%s\n", tokens[j++]);
return 0;
}

below is a compact working way of doing what you want. It prints the words of each line, sorted and separated by one space, without repeating words being repeated (if you want them repeated for sure you will be able to touch the program to make it work)
$ cat pru799.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DELIMITERS " \t\n,.-()&%$\"\'[]{}+-*/;:##|!\\<>=?"
#define LINE_SIZE 1024
#define MAX_WORDS 256
int compare(const char **p, const char **q)
{
return strcmp(*p, *q);
}
int main()
{
char line[LINE_SIZE];
char *words[MAX_WORDS];
int n_words;
while (fgets(line, sizeof line, stdin)) { /* while not eof */
char *p;
int i;
/* first get the words */
n_words = 0;
for (p = strtok(line, DELIMITERS); p; p = strtok(NULL, DELIMITERS)) {
if (strlen(p) == 0) continue; /* word is zero length */
if (n_words >= MAX_WORDS) {
fprintf(stderr, "MAX_WORDS(%d) exceeded\n", MAX_WORDS);
exit(EXIT_FAILURE);
}
words[n_words++] = p;
} /* for */
/* now we have all the words in the array of strings words, sort it */
qsort(words, n_words, sizeof words[0], (int(*)(const void *, const void *))&compare);
/* now print the words */
for (i = 0; i < n_words; i++) {
if (i) { /* all but the first one */
/* don't repeat words */
if (!strcmp(words[i], words[i-1]))
continue;
printf(" "); /* print a space between words */
}
printf("%s", words[i]);
}
printf("\n");
} /* while */
} /* main */

Related

Pig Latin Implementation in C

I'm trying to implement question 8.13 from C How to program, which is simply shifting left from the second char of the string and concat the first char of the string with "ay". For example:
jump -> umpjay the -> hetay and so on.
My try is here:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *appender(char s);
void shiftLeft(char [], int);
int main()
{
char s[100], *lastThree;
fgets(s, 100, stdin);
s[strcspn(s, "\n")] = 0;
char *tokenPtr = strtok(s, " ");
while(tokenPtr != NULL){
lastThree = appender(tokenPtr[0]);
//printf("lastThree : %s\n", lastThree); //for debugging
shiftLeft(tokenPtr, strlen(tokenPtr));
sprintf(tokenPtr, "%s%s ", tokenPtr, lastThree); // concatenation
printf("tokenptr:%s ", tokenPtr);
tokenPtr = strtok(NULL, " ");
}
return 0;
}
char *appender(char s){
char *returned = (char*)malloc(sizeof(char)*4); // allocation
snprintf(returned, sizeof(returned), "%c%s",s,"ay"); // append ay
return returned;
}
void shiftLeft(char s[], int len){
int i;
for(i=0;i<len-1;i++){
s[i]=s[i+1];
}
s[i] = '\0';
}
But code works wrong. It evaluates tokenPtr as yaay even if the input is only a word.

Dividing up a char array to different values in struct

Through a tcp socket I get a string like this: "SmallTitle fromhost 9595".
Upon reciving it I would like to divide it in to a struct so it's easier to work with.
This is what I have come up with. But there are problems. 1 it is nasty. 2 When printing the struct in the end i recieve garbage output after the string. See output example at the bottom. Should not printf stop at '\0'? Then it might be missing such.
struct recieved {
char what[50];
char from[50];
int value;
};
int main(int argc, char const *argv[]) {
struct recieved leaderRec;
char *word;
char buf[] = "leadervalue host 9569"; //Example recieved input from socket.
word = (char *) malloc(sizeof(char) * 50);
int i = 0;
int count = 1;
do{
if((buf[i] == ' ') || (buf[i+1] == '\0')){
//End of word
//word[strlend(word)+1] = '\0';
//printf("%s\n", word);
if(count == 3){
//The value is here
//get the last number
strncat(word,&buf[i], 1);
leaderRec.value = atoi(word);
//printf("%d\n", value);
}
if(count == 1)
strncpy(leaderRec.what, word, strlen(word));
else if(count == 2)
strncpy(leaderRec.from, word, strlen(word));
free(word);
word = (char *) malloc(sizeof(char) * 50);
count++;
}
else {
//printf("%d\n",i );
//printf("%s\n", &buf[i]);
strncat(word,&buf[i], 1);
}
//printf("%s", &buf[i]);
//printf("%d -", i);
i++;
} while(buf[i] != '\0');
printf("%s\n", leaderRec.what);
printf("%s\n", leaderRec.from);
printf("%d\n", leaderRec.value);
return 0;
}
Output:
leadervalue
host__vdso_get
9569
Any better ways to do this?
How would I avoid the garbage output at the second print in the example output?
But there are problems. 1 it is nasty. 2 When printing the struct in
the end i recieve garbage output after the string.
The "garbage" prints, as have been noted in the comments, are because you are not properly null terminating your strings. For instance, you are not initializing word to an empty string (single null character) before calling strncat.
Any better ways to do this?
Most certainly. I'd suggest using strtok as so:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct received {
char what[50];
char from[50];
int value;
};
int main(void)
{
char buf[] = "leadervalue host 9569";
struct received leaderRec;
/* Initialize leaderRec here */
memset(&leaderRec, 0, sizeof(leaderRec));
char *s = strdup(buf);
char *p = strtok(s, " ");
if (p != NULL) {
strncpy(leaderRec.what, p, sizeof(leaderRec.what));
p = strtok(NULL, " ");
}
if (p != NULL) {
strncpy(leaderRec.from, p, sizeof(leaderRec.from));
p = strtok(NULL, " ");
}
if (p != NULL) {
leaderRec.value = atoi(p);
}
free(s);
printf("%s %s %d\n", leaderRec.what, leaderRec.from, leaderRec.value);
return 0;
}

Lowercase to Uppercase in C

(I'm posting this again with some changes.) I'm creating a program that would convert all the letters in the words (173528 from a text file) from lower case to upper case letters. Here's my program
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NWORDS 173528
typedef char String[29];
void
Cap(char *Words[] )
{
int i = 0;
while (Words[i] != '\0') {
Words[i] = Words[i] - 32;
i++;
}
}
void
Initialize(char *Words[])
{
int i;
String word;
char *pch;
for (i = 0; i < NWORDS; i++) {
scanf("%s", word);
pch = malloc(sizeof(char) * (strlen(word) + 1) );
if (pch == NULL) {
printf("Memory is no enough\n");
exit(1);
}
strcpy( pch, word);
Words[i] = pch;
}
}
void
Print(char *Words[])
{
}
void
Free(char *Words[])
{
}
int
main()
{
char *Words[NWORDS];
Initialize(Words);
Cap(Words);
Print(Words);
Free(Words);
return 0;
}
No compiler error but expected output wont display. Thank you in advance for your help!
while (Words[i] != '\0') {
Words[i] = Words[i] - 32;
The approach above is wrong. To access individual character, you need to use
Words[i][j] // i-th word, j-th letter in the word.

string operations - weird characters

Description:
I read userinput (e.g "ls -l /") with fgets() and invoke Parse() where it gets seperated ("ls" "-l" "\") for later usage.
The Problem is: the tokens from the the first cycle have weird characters(screenshot below) in it, but from thereon the output is fine.
I tried to initialize both Buffers with zeroes with no change in behaviour. Please explain what is happening in my first output.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
typedef char* string;
char inputBuffer[512];
string parse[256];
int j,parseCount;
void Parse(void);
void Parse(void)
{
char buffer[512];
string token;
token = " ";
strcpy(buffer, inputBuffer);
j=0;
parse[j] = strtok (buffer, token);
while (parse[j] != NULL)
{
j++;
parse[j] = strtok (NULL, token);
}
parseCount =j;
}
int main (void)
{
printf(">> ");
fgets(inputBuffer, 512, stdin); /* input buffer, max.Input(char), whereFrom?*/
Parse();
for (j=0;j<parseCount;j++){
printf("[%d] %s\n",j, parse[j]);
}
return main();
}
This line
parse[j] = strtok (buffer, token);
stores memory addresses in buffer, which is local to Parse(). The memory representing buffer is invalidated upon the return of Parse(), so also the addresses stored in parse aren't valid anymore when trying to be dereferenced to print what they refer to.
To fix this have the calling function create a temporary working buffer and pass down to `Parse() a reference to it:
char * parse[256] = 0;
char buffer[512] = "";
size_t parseCount = 0;
void Parse(char * buffer)
{
const char * token = " ";
size_t j = 0;
parse[j] = strtok(buffer, token);
while (parse[j] != NULL)
{
j++;
parse[j] = strtok(NULL, token);
}
parseCount = j;
}
int main(void)
{
fgets(buffer, 512, stdin);
{
char buffer_tmp[512];
strcpy(buffer_tmp, buffer);
Parse(buffer_tmp);
for (size_t j = 0; j < parseCount; j++)
{
printf("[%zu] %s\n", j, parse[j]);
}
}
return 0;
}
As I don't like the globals, I'd prefer the following:
#include <stdio.h>
#include <string.h>
size_t parse(char * buffer, char ** parse)
{
const char * token = " ";
size_t j = 0;
parse[j] = strtok(buffer, token);
while (parse[j] != NULL)
{
j++;
parse[j] = strtok(NULL, token);
}
return j;
}
#define IN_MAX (512 + 1 + 1)
int main(void)
{
char buffer[IN_MAX] = "";
if (NULL != fgets(buffer, IN_MAX, stdin))
{
char buffer_tmp[IN_MAX];
strcpy(buffer_tmp, buffer);
{
size_t parse_count = 0;
char * parse[IN_MAX/2 + 1] = 0;
size_t parse_count = parse(buffer_tmp, parse);
for (size_t j = 0; j < parse_count; j++)
{
printf("[%zu] %s\n", j, parse[j]);
}
}
}
else if (ferror())
{
fprintf(stderr, "Error reading from inout stream.\n");
}
return 0;
}

Using strtok in c

I need to use strtok to read in a first and last name and seperate it. How can I store the names where I can use them idependently in two seperate char arrays?
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="test string.";
char * test;
test = strtok (str," ");
while (test != NULL)
{
printf ("%s\n",test);
test= strtok (NULL, " ");
}
return 0;
}
Here is my take at a reasonably simple tokenize helper that
stores results in a dynamically growing array
null-terminating the array
keeps the input string safe (strtok modifies the input string, which is undefined behaviour on a literal char[], at least I think in C99)
To make the code re-entrant, use the non-standard strtok_r
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char** tokenize(const char* input)
{
char* str = strdup(input);
int count = 0;
int capacity = 10;
char** result = malloc(capacity*sizeof(*result));
char* tok=strtok(str," ");
while(1)
{
if (count >= capacity)
result = realloc(result, (capacity*=2)*sizeof(*result));
result[count++] = tok? strdup(tok) : tok;
if (!tok) break;
tok=strtok(NULL," ");
}
free(str);
return result;
}
int main ()
{
char** tokens = tokenize("test string.");
char** it;
for(it=tokens; it && *it; ++it)
{
printf("%s\n", *it);
free(*it);
}
free(tokens);
return 0;
}
Here is a strtok-free reimplementation of that (uses strpbrk instead):
char** tokenize(const char* str)
{
int count = 0;
int capacity = 10;
char** result = malloc(capacity*sizeof(*result));
const char* e=str;
if (e) do
{
const char* s=e;
e=strpbrk(s," ");
if (count >= capacity)
result = realloc(result, (capacity*=2)*sizeof(*result));
result[count++] = e? strndup(s, e-s) : strdup(s);
} while (e && *(++e));
if (count >= capacity)
result = realloc(result, (capacity+=1)*sizeof(*result));
result[count++] = 0;
return result;
}
Do you need to store them separately? Two pointers into a modified char array will yield two separate perfectly usable strings.
That is we transform this:
char str[] ="test string.";
Into this:
char str[] ="test\0string.";
^ ^
| |
char *s1 ----- |
char *s2 -----------
.
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="test string.";
char *firstname = strtok(str, " ");
char *lastname = strtok(NULL, " ");
if (!lastname)
lastname = "";
printf("%s, %s\n", lastname, firstname);
return 0;
}
What about using strcpy:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_NAMES 2
int main ()
{
char str[] ="test string.";
char *names[MAX_NAMES] = { 0 };
char *test;
int i = 0;
test = strtok (str," ");
while (test != NULL && i < MAX_NAMES)
{
names[i] = malloc(strlen(test)+1);
strcpy(names[i++], test);
test = strtok (NULL, " ");
}
for(i=0; i<MAX_NAMES; ++i)
{
if(names[i])
{
puts(names[i]);
free(names[i]);
names[i] = 0;
}
}
return 0;
}
It contains much clutter to maintain a complete program and clean its resources, but the main point is to use strcpy to copy each token into its own string.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char** split(const char *str, const char *delimiter, size_t *len){
char *text, *p, *first, **array;
int c;
char** ret;
*len = 0;
text=strdup(str);
if(text==NULL) return NULL;
for(c=0,p=text;NULL!=(p=strtok(p, delimiter));p=NULL, c++)//count item
if(c==0) first=p; //first token top
ret=(char**)malloc(sizeof(char*)*c+1);//+1 for NULL
if(ret==NULL){
free(text);
return NULL;
}
strcpy(text, str+(first-text));//skip until top token
array=ret;
for(p=text;NULL!=(p=strtok(p, delimiter));p=NULL){
*array++=p;
}
*array=NULL;
*len=c;
return ret;
}
void free4split(char** sa){
char **array=sa;
if(sa!=NULL){
free(array[0]);//for text
free(sa); //for array
}
}
int main(void){
char str[] ="test string.";
char **words;
size_t len=0;
int i;
words = split(str, " \t\r\n,.", &len);
/*
for(char **wk = words; *wk ;wk++){
printf("%s\n", *wk);
}
*/
for(i = 0;i<len;++i){
printf("%s\n", words[i]);
}
free4split(words);
return 0;
}
/* result:
test
string
*/
Copy the results from strtok to a new buffer using a function such as
/*
* Returns a copy of s in freshly allocated memory.
* Exits the process if memory allocation fails.
*/
char *xstrdup(char const *s)
{
char *p = malloc(strlen(s) + 1);
if (p == NULL) {
perror("memory allocation failed");
exit(1);
}
strcpy(p, s);
return p;
}
Don't forget to free the return values when you're done with them.
IMO, you don't need (and probably don't want) to use strtok at all (as in, "for this, or much of anything else"). I think I'd use code something like this:
#include <string.h>
#include <stdlib.h>
static char *make_str(char const *begin, char const *end) {
size_t len = end-begin;
char *ret = malloc(len+1);
if (ret != NULL) {
memcpy(ret, begin, len);
ret[len]='\0';
}
return ret;
}
size_t tokenize(char *tokens[], size_t max, char const *input, char const *delims) {
int i;
char const *start=input, *end=start;
for (i=0; *start && i<max; i++) {
for ( ;NULL!=strchr(delims, *start); ++start)
;
for (end=start; *end && NULL==strchr(delims, *end); ++end)
;
tokens[i] = make_str(start, end);
start = end+1;
}
return i;
}
#ifdef TEST
#define MAX_TOKENS 10
int main() {
char *tokens[MAX_TOKENS];
int i;
size_t num = tokenize(tokens, MAX_TOKENS, "This is a longer input string ", " ");
for (i=0; i<num; i++) {
printf("|%s|\n", tokens[i]);
free(tokens[i]);
}
return 0;
}
#endif
U can do something like this too.
int main ()
{
char str[] ="test string.";
char * temp1;
char * temp2;
temp1 = strtok (str," ");
temp2 = strchr(str, ' ');
if (temp2 != NULL)
temp2++;
printf ("Splitted string :%s, %s\n" , temp1 , temp2);
return
}

Resources