c-strings

Modifying a c string [duplicate]

对着背影说爱祢 提交于 2019-12-20 06:17:49
问题 This question already has answers here : Access violation writing location when working with pointers to char (3 answers) Closed 2 years ago . I'm trying to implement tolower(char *) function, but I get access violation error. I came to know that this is because to compiler stores string literals in a read-only memory. Is this true? Here's some code: char* strToLower(char *str) { if(str == nullptr) return nullptr; size_t len = strlen(str); if(len <= 0) return nullptr; for(size_t i = 0; i <

scanf and strcmp with c string

烂漫一生 提交于 2019-12-20 04:04:10
问题 I found a nice example of how to use strcmp, but it's only working with fgets(), and i need to make it work with scanf. So, here's the code: int main(void) { char fruit[] = "apple\n"; char ans[80]; do { printf ("Guess my favorite fruit? "); scanf ("%s",ans); } while (strcmp (fruit, ans) != 0); puts ("Correct answer!"); return 0; } Even when I write the correct answear ("apple") it stays in the loop and keeps asking me what is the favorite fruit... I'm guessing it has something to do with the

How to write a better strlen function?

怎甘沉沦 提交于 2019-12-18 12:28:16
问题 I am reading "Write Great Code Volume 2" and it shows the following strlen impelementation: int myStrlen( char *s ) { char *start; start = s; while( *s != 0 ) { ++s; } return s - start; } the book says that this implementation is typical for an inexperienced C programmer. I have been coding in C for the past 11 years and i can't see how to write a function better than this in C(i can think of writing better thing in assembly). How is it possible to write code better than this in C? I looked

Are strtol, strtod unsafe?

余生颓废 提交于 2019-12-18 07:41:06
问题 It seems that strtol() and strtod() effectively allow (and force) you to cast away constness in a string: #include <stdlib.h> #include <stdio.h> int main() { const char *foo = "Hello, world!"; char *bar; strtol(foo, &bar, 10); // or strtod(foo, &bar); printf("%d\n", foo == bar); // prints "1"! they're equal *bar = 'X'; // segmentation fault return 0; } Above, I did not perform any casts myself. However, strtol() basically cast my const char * into a char * for me, without any warnings or

strncpy or strlcpy in my case

不羁岁月 提交于 2019-12-17 19:37:32
问题 what should I use when I want to copy src_str to dst_arr and why? char dst_arr[10]; char *src_str = "hello"; PS: my head is spinning faster than the disk of my computer after reading a lot of things on how good or bad is strncpy and strlcpy . Note: I know strlcpy is not available everywhere. That is not the concern here. 回答1: strncpy is never the right answer when your destination string is zero-terminated. strncpy is a function intended to be used with non-terminated fixed-width strings.

Can a std::string contain embedded nulls?

最后都变了- 提交于 2019-12-17 16:30:14
问题 For regular C strings, a null character '\0' signifies the end of data. What about std::string , can I have a string with embedded null characters? 回答1: Yes you can have embedded nulls in your std::string . Example: std::string s; s.push_back('\0'); s.push_back('a'); assert(s.length() == 2); Note: std::string 's c_str() member will always append a null character to the returned char buffer; However, std::string 's data() member may or may not append a null character to the returned char

Replacing a character in a c string

前提是你 提交于 2019-12-14 03:35:31
问题 I'm having trouble replacing characters in my c string. I have a c string called bits initialized to a sixteen bit string of 0's and 1's. What I'm trying to do is convert the strings into their twos complement versions. What I've learned is a simple assignment such as int twosComplement(int number,char *binary){ printf("searching for twos complement\n"); int temp=number * -1; if(temp<-32768) return 0; printf("%d\n",temp); char bits[17]=""; int i; int x=0; int y; for(i=15;i>=0;i--){ y=pow(2,i)

concatenation of character arrays in c

喜夏-厌秋 提交于 2019-12-14 03:34:07
问题 I am trying to concatenate 2 character arrays but when I try it does not work and my o/p console hangs and does not print anything. char *str[2]; str[0] = "Hello "; str[1] = "World"; strcat(str[0],str[1]); printf("%s\n",str[0]); I even tried the below code which fails as well char *str1 = "Hello "; char *str2 = "World"; strcat(str1,str2); printf("%s\n",str1); Can someone explain this? TIA. 回答1: char *str1 = "Hello "; char *str2 = "World"; strcat(str1,str2); printf("%s\n",str1); Here you have

Remove adjacent duplicates in a string in C

大憨熊 提交于 2019-12-13 23:07:23
问题 How to remove all adjacent duplicates in a string in C. say for example..if "caaabbcdd" is the given string then it should remove sequentially as 1. cbbcdd 2. ccdd 3. dd thus an empty string is returned in the end. Time complexity can be O(n^2) for starting.Can anyone help. so far this i what i have done void recursiven2(char *str) { int i,j,k,len; len=strlen(str); for(i=0;i<len-1;i++) { if(str[i]==str[i+1]) { for(j=i;j<len-2;j++) str[j]=str[j+2]; str[j]='\0'; } } } 回答1: You can refer to this

C Check duplicate string entries

不问归期 提交于 2019-12-13 23:06:37
问题 I need to check if in my file there are duplicates entries, in C. Sample file: /proc/proc1 1000 /proc/proc2 2000 /proc/proc1 3000 I need to solve like this: /proc/proc1 1000 3000 /proc/proc2 2000 The path (/proc/proc*) can include spaces likes: /proc/proc hello/foo Here I wrote something to handle /proc/ and their pids, but now I'm stuck on this problem.. 回答1: #include <stdio.h> #include <string.h> int main(void){ char str[]= "/proc/proc hello/foo 4000"; char path[256]; char pid[10]; char *p;