Currently creating a C program to reverse a name with the use of pointers and a function. I get a stack overflow error when attempting to use strlen() on char *name. I have not fully created the function yet as I'm currently hitting this problem with strlen().
#include <stdio.h>
#include <string.h>
void format_name();
int main(void)
{
char str[256+1];
char *name = &str[0];
printf("Enter the first and last name: ");
scanf("%[^\n]s", str);
printf("formatting name\n");
format_name (*name);
printf("Formatted name: %s\n", str);
return 0;
}
void format_name (char *name)
{
int spacepos = 0;
printf("Finding string length");
int length = strlen(name);
printf("Size of string = %d", length);
for(p = 0; p <= length - 1; p++)
{
printf("Checking position %d", p);
if(name[p] == ' ' && spacepos == 0)
{
spacepos = p;
printf("first space found at pos %d", p);
}
}
}
Related
I tried to replace a target word in sentence to another word but it doesn't work. Can you please help me with where I got wrong? It has a problem with strstr and strncopy. It says that *swap can be zero, which then makes strncpy stop. I tried to find way to solve this problem, but I couldn't. How can I fix this code?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *swap (char* data, const char* original, const char* change);
int main() {
char string[100];
char original[100];
char change[100];
printf("Input String : ");
fgets(string, 100, stdin);
printf("Find String : ");
fgets(original, 100, stdin);
printf("Replace String : ");
fgets(change, 100, stdin);
printf("%s", swap(string, change, original));
return 0;
}
char *swap(char* data, const char* original, const char* change) {
char* swap;
swap = strstr(data, original);
int num = strlen(change);
if (num == 0 || swap==0) return 0;
strncpy(swap, change, strlen(change));
printf("Result : %s", data);
return 0;
}
I have fixed your code and added a few tests to avoid buffer overflow vulnerability in your swap function.
My version take car of change and original being of different lengths.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *swap(char* data, int size, const char* original, const char* change);
int main()
{
char string[100];
char original[100];
char change[100];
printf("Input String : ");
fgets(string, sizeof(string), stdin);
string[strcspn(string, "\r\n")] = 0; // Remove EOL
printf("Find String : ");
fgets(original, sizeof(original), stdin);
original[strcspn(original, "\r\n")] = 0; // Remove EOL
printf("Replace String : ");
fgets(change, sizeof(change), stdin);
change[strcspn(change, "\r\n")] = 0; // Remove EOL
printf("%s\n", swap(string, sizeof(string), original, change));
return 0;
}
char *swap(char* data, int size, const char* original, const char* change)
{
if ((data == NULL) || (original == NULL) || (change == NULL))
return data; // Unspecified data
int datLen = strlen(data);
int oriLen = strlen(original);
if (oriLen == 0)
return data; // No original specified
char *swap = strstr(data, original);
if (swap == NULL)
return data; // Original not found in data
int chgLen = strlen(change);
if (size < (datLen + chgLen - oriLen))
return data; // Not enough space to store result
if (chgLen != oriLen)
memmove(swap + chgLen, swap + oriLen, 1 + datLen + oriLen - (swap - data));
memmove(swap, change, chgLen);
return data;
}
I have not changed that, but I think it is better to have swap return 0 or 1 if the swap took place or not. Returning data is not very useful as the swap is done in place.
This is a snippet of a program I am working on. I wanted to get information from the user, where the target variable cannot have predefined memory. For some reason my function does not work for the last section of the code. Any help will be appreciated.
Current Output:
Enter your name: Daniel
Hi Danie (expected as I put the limit to 5 characters)
Enter your phone number: 123456789
123456789
Enter your Note: (Here it breaks and does not print the contactNote variable.)
Here is the code:
char *getwords(int numberoftimes) {
char *words = malloc(sizeof(char));
int index = 0;
int character = EOF;
int size = 0;
int increase = 1;
int counter = 0;
char *temp;
while (character) {
character = getc(stdin);
if (numberoftimes == 0) {
if (character == '\n') {
character = 0;
}
} else if (counter == numberoftimes || character == '\n') {
character = 0;
}
if (size <= index) {
size += increase;
temp = realloc(words, size);
if (!temp) {
free(words);
words = NULL;
break;
}
words = temp;
}
words[index++] = character;
counter++;
}
return words;
}
int main() {
char *contactName, *contactNote;
long contactNumber;
printf("\nEnter your name: ");
char *getwords();
char *Name = getwords(5);
printf("\nHi %s\n", Name);
printf("\nEnter your phone number: ");
scanf("%lu", &contactNumber);
printf("\n%lu\n", contactNumber);
printf("\nEnter your Note: ");
char *getwords();
char *Note = getwords(0);
printf("\n%s\n", Note);
}
scanf() for the contact number leaves the ENTER sitting in the [keyboard] input buffer.
int main() {
char *contactName, *contactNote; // unused variables
long contactNumber;
printf("\nEnter your name: ");
char* getwords(); // unnecessary function declaration
char *Name = getwords(5);
printf("\nHi %s\n", Name);
printf("\nEnter your phone number: ");
scanf("%lu", &contactNumber); // leaves <ENTER> in buffer
printf("\n%lu\n", contactNumber);
printf("\nEnter your Note: ");
char* getwords(); // unnecessary function declaration
char *Note = getwords(0); // empty line because of previous <ENTER>
printf("\n%s\n", Note);
}
I suggest you also use getwords() for the number, then convert to long with strtol().
There are multiple problems in your code:
mixing getc() and scanf() is tricky as scanf() leaves the trailing newline pending in stdin so getc() reads this newline immediately. You should read all of your inputs with getwords().
the extra char *getwords(); declarations inside the body of main() are redundant and confusing.
getwords() is too complicated, especially for exiting the loop.
you do not always check for memory allocation error.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
char *getwords(const char *prompt, int maxlen) {
char *words = malloc(sizeof(char));
int index = 0;
int size = 0;
char *temp;
if (words == NULL)
return NULL;
printf("%s", prompt);
for (;;) {
int character = getc(stdin);
if (character == EOF || character == '\n')
break;
if (index < maxlen) {
if (index >= size) {
size += 1;
temp = realloc(words, size + 1);
if (!temp) {
free(words);
return NULL;
}
words = temp;
}
words[index++] = character;
}
}
words[index] = '\0';
return words;
}
int main() {
char *Name = getwords("\nEnter your name: ", 5);
if (!Name)
return 1;
printf("\nHi %s\n", Name);
char *contact = getwords("\nEnter your phone number: ", 20);
if (!contact)
return 1;
long contactNumber = 0;
sscanf(contact, "%lu", &contactNumber);
printf("\n%lu\n", contactNumber);
char *Note = getwords("\nEnter your Note: ", 10);
if (!Note)
return 1;
printf("\n%s\n", Note);
return 0;
}
I need to get all the characters before the first digit from an array.
I did this and it seems to work correctly:
#include <stdio.h>
int main() {
char temp[128] = {0};
char str_active[128] = {0};
sprintf(temp, "%s", "AB01");
printf("Complete string.: %s\n", temp);
int len = sizeof(temp) / sizeof(char);
int index = 0;
while (index < len) {
if (isdigit(temp[index])) {
break;
} else {
index++;
}
}
snprintf(str_active, index + 1, "%s\n", temp);
printf("String before first digit.: %s\n", str_active);
return 0;
}
I'm wondering if I could do the same with less instructions, so in a better way.
The function strcspn can do it for you:
The strcspn() function calculates the length of the initial segment of s which consists entirely of bytes not in reject.
#include <stdio.h>
#include <string.h>
int main() {
char temp[128] = {0};
char str_active[128] = {0};
sprintf(temp, "%s", "AB01");
printf("Complete string.: %s\n", temp);
strncpy(str_active, temp, strcspn(temp, "0123456789"));
printf("String before first digit.: %s\n", str_active);
return 0;
}
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 */
I'm trying to scan user input text for specific words and then, when those words occur, print them to the console.
#import <Foundation/Foundation.h>
#include <stdio.h>
int main(){
char cArray[] = "example";
char cInput[] = "";
char cOutput[] = "";
printf("\nType your message:\n");
for (int y=0; y<1; y++){
fgets(cInput, 120, stdin);
}
printf("\nInitialised character array:\n");
for (int x=0; x<1; x++){
if(strncmp(&cInput[x], &cArray[x], 120) == 0){
strncpy(cOutput, cArray, strnlen(cInput, +1));
printf("%s\n", cOutput);
break;
}
}
}
Output:
Type your message:
example
Initialised character array:
Program ended with exit code: 120
Appreciate any feedback as I'm still learning :)
Thanks.
The edited code:
#include <stdio.h>
#include <string.h>
#define MAX_STR_LEN 120
int main(){
char *cArray[MAX_STR_LEN] = {"example", "this"};
char cInput[MAX_STR_LEN] = "";
char cOutput[MAX_STR_LEN] = "";
printf("Type your message:\n");
for (int y=0; y<1; y++){
fgets(cInput, MAX_STR_LEN, stdin);
char * ptr = cInput;
while((ptr=strstr(ptr, *cArray)) != NULL){
strncpy(cOutput, ptr, strlen(*cArray));
printf("Initialised string array:\n%s\n", cOutput);
ptr++;
}
}
}
Works although I'm encountering a different problem now. The output only seems to register one word before it completes, thus only "example" is printed.
Output:
Type your message:
this is an example
Initialised string array:
example
Program ended with exit code: 0
char cInput[] = "";
The sizeof this array is 1.
fgets(cInput, 120, stdin);
This is array out of bound write which will lead to undefined behavior.
Have
char cInput[120] = "";
You need to take care of
char cOutput[120] = "";
also. Since you are trying to write to this array after comparing.
You need strstr function from string.h
const char * strstr ( const char * str1, const char * str2 );
the following gives you an example of usage:
#include <stdio.h>
#include <string.h>
#define MAX_STR_LEN 120
int main(){
char cArray[MAX_STR_LEN] = "example"; // string to be searched in the input string
char cInput[MAX_STR_LEN] = ""; // input string
char cOutput[MAX_STR_LEN] = ""; // buffer for found string
printf("\nType your message:\n");
for (int y=0; y<1; y++){ // this loop from original example looks strange, but it works
fgets(cInput, MAX_STR_LEN, stdin);
}
// search in the input string
char * ptr;
if( ( ptr=strstr(cInput, cArray) ) != NULL)
{
//copy the string to cOutput
strncpy(cOutput, ptr, strlen(cArray));
// output the found string
printf("String that was found: \n%s\n", cOutput);
}
else
{
printf("String was not found in the input!\n");
}
}
EDIT:
If you want to all the strings, use the following loop instead of if-else:
// search in the input string
char * ptr = cInput;
while( ( ptr=strstr(ptr, cArray) ) != NULL)
{
//copy the string to cOutput
strncpy(cOutput, ptr, strlen(cArray));
// output the found string
printf("String \"%s\" was found at position %d\n", cOutput, (int)(ptr - cInput + 1));
// find next string
ptr++;
}