The program's supposed to check if the entered string has embedded spaces (FYI, by the prof's definition, "Te xt" has embedded spaces, while " text" and "text " do not, hence the extra bits of code). It gives me a segmentation fault sometime between the first and second "check" printfs in HasEmbedded. My test input is Cookie monster, FYI, but that shouldn't matter.
If you're wondering, those "check" printfs are only tabbed so they stand out.
Code:
1 #include <stdio.h>
2 int HasEmbedded(char str[]);
3 int main() {
4 char string[31];
5 int space;
6 printf("Enter a string: ");
7 scanf("%30[^\n]", string);
8 printf("String: %s\n", string);
9 space = HasEmbedded(string);
10 if (space > 0) printf("%s does have embedded spaces\n", string);
11 else printf("%s does NOT have embedded spaces\n", string);
12 }
13
14 int HasEmbedded(char str[]){
15 int i = 0, k = 0, rv = 0;
16
17 printf("check\n");
18 //Ignore leading spaces
19 while(str[i]=' '){
20 i++;
21 }
22
23 printf("check\n");
24 /*
25 If there is a space, enusure that it's follwed by a character
26 (otherwise it's trailing, not embedded)
27 Tehcnically, the curly braces could be omitted, but I prefer them for
28 organizational purposes.
29 */
30 for(; str[i]; i++){
31 printf("check\n");
32 if(str[i] = ' '){
33 printf("check\n");
34 for(k=0; str[k]; k++){
35 printf("check\n");
36 if(str[k]!= ' '){
37 printf("check\n");
38 rv = 1;
39 printf("check\n");
40 }
41 }
42 }
43 }
44 printf("check\n");
45 return(rv);
46 }